二叉排序樹的應用(基於二叉排序樹的個人通信錄)

在日常生活中,個人通信錄是我們不可少的,不管是紙式的個人通信錄還是我們手機中的個人通信錄,查尋是其最基本的操作,幾乎所有的操作都是在查尋的基礎上進行的,所以,查尋時間的快慢很大程度上決定了整個通信錄的性能。所以,一個有着良好界面、查尋速快的通信錄,是人們所追求的。

本設計應用折半查尋法的技術思想進行查尋,從本思想出發,可以有兩種數據組織方式:一是應用鏈表進行組織數據,由於折半查尋法的特殊性,所要進行查尋的數據列必須是有序的數據列,這樣要求對數據列進行排序。出於系統實時查尋的考慮,每次對通信錄進行改變後都得進行重新排序,這樣才能保證數據列是實時有序的。這樣當操作量大時,排序所消耗的時間對整個系統有很大的影響。

二是應用二叉排序樹來組織數據,由於二叉排序樹是應用折半查尋法思想進行對數據進行存儲的,所以,其左孩子大於雙親結點、右孩子小於雙親結點(或者左孩子小於雙親結點、右孩子大於雙親結點),這樣就可以應用折半查尋法的思想進行查尋,從而減少對排序時所消耗的時間。

本設計採用第二種方法,即應用二叉排序樹進行組織數據,在此基礎上進行對個人通信錄的各種操作。由於刪除操作是本設計的重點,刪除操作的成功與否直接影響到整個系統的成敗,所以在此進行詳細分析一下刪除操作的實現。

此功能函數主要應用於刪除將要進行刪除的記錄,此操作較其它幾個操作難一點,同時也是此次設計的重點,所以,本函數的成敗可以直接影響到本次設計的成敗,同時,在進行二叉排序樹進行組織時,如果不從系統的整體進行考慮,只想到簡單地實現刪除功能,將會出現錯誤。

在設計初期,由於沒有考慮所有可能的情況,所以在進行刪除最後一個結點時,總會出現內存不能引用的錯誤。最後想到應用浪費一個結點空間的技術進行處理此問題,就是根結點用來保存二叉排序樹的某些信息,而不保存記錄,與我們單鏈表中的頭結點一樣,這樣做解決了上面所說的問題,同時在進行查尋雙親結點時也帶來了很大的方便。

有關二叉排序樹的刪除的有關問題,請讀者參考相關的參考文獻,在此不再進行說明,本節重點在於說明本課程設計中有關刪除的問題。

在本設計中,刪除的首要條件是找到將要進行刪除結點的雙親結點,由於根結點不用於存儲記錄,所以,可以不用進行判斷根結點的情況,進行查尋雙親結點時,從根結點開始,首先進行判斷根結點的左右子樹是否爲空,如果根結點的左右子樹爲空,則返回NULL,如果根結點的左子樹(右子樹)不爲空,則將其左子樹(右子樹)的記錄的學號與將要進行刪除的學號進行比較,如果相等,則返回根結點;否則進行比較,如果左子樹(右子樹)不爲空,且左子樹(右子樹)的學號大於(小於)要進行刪除的學號,則進行遞歸在左子樹(右子樹)中進行查尋,直到查尋到或者當前結點的左右子樹爲空時結束。如果當前結點的學號與要進行刪除的學號不相等,且當前結點的左右子樹爲空,則返回空,結束查尋過程。

經過對雙親結點的查尋,如果沒有此記錄,則進行提示,否則進行刪除操作[1] [5],在進行刪除時,有以下幾種可能,以下操作中假設每次把要進行刪除結點進行刪除後,同時也釋放了此結點所佔用的內存空間,防止內存在運行過程中丟失。

第一:要進行刪除的結點爲葉結點,直接把其從二叉排序樹中進行刪除。

第二:此結點只有左子樹或者右子樹,這種情況下只需將只需把此結點的左子樹或者右子樹替換爲雙親結點的左子樹。

第三:此結點有左子樹同時有也右子樹,此種操作比較複雜,其中有兩種方法進行刪除,本課程設計中應用的方法是從某子樹中找出一個結點(假設爲Temp),將其值代替要進行刪除結點的值,再把Temp結點進行刪除。

這是 本人在大二的時候做的一個課程設計,見到論壇上有很多對二 叉樹,特別是二叉排序樹的不瞭解,所以,把它分享出來,如果需要每個功能模塊的說明,設計文檔,請給我發郵件或者到我的下載頁面去下載,由於本人不在乎資 源分,所以下載頁面所需資源分只1分就可以下載整個設計,包括詳細的設計報告,不過本人把它轉爲PDF文檔。同時,發表顯示與編輯顯示不太一樣,本人對這玩意兒很少用,還是請看原版吧,有需要原版請與我聯繫。

我的 郵箱:[email protected]     [email protected]

下面爲詳細代碼,希讀者能看懂,由於本人一直專注於嵌入式底層驅動程序開發,所以,對數據結構與算法只能做爲加強自己C語言的一個途徑,也許在算法上與數據結構的組織上不太合理,請原諒。

  1. /**********************************************************************************
  2. *                            基於二叉排序樹的個人通信錄                                   
  3. *                               
  4. *                                   
  5. *
  6. *                                    
  7. *
  8. * 文    件: 基於二叉排序樹樹的個人通信錄.C
  9. * 作    者: soon
  10. * 部    門: 長沙理工大學 
  11. * 編寫日期: 2009.9.10
  12. * 模塊版本: 
  13. * 修改記錄:
  14. * ================================================================================
  15. * 版 本|  日  期  | 修改人 |                 
  16. * ================================================================================
  17. *      |          |        |
  18. *      |          |        |
  19. *      |          |        |
  20. * ================================================================================
  21. *
  22. *
  23. *          
  24. **********************************************************************************/ 
  25. #include <stdio.h> 
  26. #include <stdlib.h> 
  27. #include <assert.h> 
  28. #include <string.h> 
  29. FILE *input;                                     /*定認讀取文件指針*/ 
  30. FILE *output;                                    /*定義保存文件指針*/ 
  31. /********************定義個人記錄結構**********************/ 
  32. typedef struct Node 
  33.     char name[30];                               /*用於保存姓名*/ 
  34.     char code[30];                               /*用於保存學號*/ 
  35.     int age;                                     /*用於保年齡*/ 
  36. }NODE; 
  37. /************************定義二叉樹結構體******************/ 
  38. typedef struct Tree 
  39.     NODE *student;                               /*個人通信記錄*/ 
  40.     struct Tree *lchild; 
  41.     struct Tree *rchild; 
  42. }TREE; 
  43. static int TREESIZE=sizeof(TREE);                /*進行求二叉樹所佔的內存空間*/ 
  44. static int NODESIZE=sizeof(NODE);                /*進行求個人記錄結構的內存空間*/ 
  45. TREE *Create_Tree(void );                         /*聲明創建空二叉樹的函數*/ 
  46. void Insert_Information(TREE *root,NODE *node);          /*聲明進行插入函數*/ 
  47. void Look_Through_Information(TREE *root);                       /*聲明瀏覽函數*/ 
  48. void Save_Information(TREE *root);                        /*聲明保存函數*/ 
  49. int Find_Information(TREE *root,char *code);                 /*聲明查尋函數*/ 
  50. int Rework_Information(TREE *root,char *code);               /*聲明修改函數*/ 
  51. void Menu(void );                                 /*聲明界面函數*/ 
  52. TREE *Find_Father_Node(TREE *root,char *code);           /*聲明查尋雙親結點的函數*/ 
  53. void Delete_Information(TREE *root,char *code);          /*聲明刪除函數*/ 
  54. void Free_EMS_memory(TREE *root);                        /*聲明釋放內存空間函數*/ 
  55. /**********************主函數******************************/ 
  56. void main(void ) 
  57.     TREE *root; 
  58.     NODE *node; 
  59.     TREE *temp; 
  60.     char b[30]; 
  61.     int a; 
  62.     printf("*************************基於排序二叉樹的個人通信錄*****************************/n/n/n"); 
  63.     root = Create_Tree(); 
  64.     input = fopen("shuwen.txt","r");               /*打開文件*/ 
  65.     assert(input != NULL); 
  66.     if(!feof(input))                            /*文件不空,進行讀取一條記錄*/ 
  67.     { 
  68.         node = (NODE *)malloc(NODESIZE); 
  69.         assert(node != NULL); 
  70.         fscanf(input,"%s %s %d",node->code,node->name,&node->age);/*進行讀取*/ 
  71.         temp=(TREE *)malloc(TREESIZE); 
  72.         assert(temp != NULL); 
  73.         temp->rchild = NULL; 
  74.         temp->lchild = NULL; 
  75.         temp->student = node; 
  76.         root->lchild = temp; 
  77.     } 
  78.     node = (NODE *)malloc(NODESIZE); 
  79.     assert(node != NULL); 
  80.     while((fscanf(input,"%s %s %d",node->code,node->name,&node->age))!=EOF)/*讀取文件中所有的記錄,只到文件爲空*/ 
  81.     { 
  82.         Insert_Information(root->lchild,node);           /*將讀取的文件進行插入*/ 
  83.         node=(NODE *)malloc(NODESIZE); 
  84.         assert(node!=NULL);  
  85.     } 
  86.     fclose(input);                               /*關閉讀取文件指針*/ 
  87.     while(1) 
  88.     { 
  89.         Menu(); 
  90.         /*輸入選擇*/ 
  91.         printf("/n"); 
  92.         printf("請進行選擇:"); 
  93.         scanf("%d",&a); 
  94.         printf("/n/n"); 
  95.         switch(a)                                /*進行選擇*/ 
  96.         { 
  97.         case 1:                                  /*插入*/ 
  98.             { 
  99.                 printf("請輸入要進行添加信息的學號,姓名,年齡/n"); 
  100.                 node=(NODE *)malloc(NODESIZE); 
  101.                 assert(node!=NULL); 
  102.                 scanf("%s %s %d",node->code,node->name,&node->age); 
  103.                 if(root->lchild==NULL)           /*如果記錄爲空時*/ 
  104.                 { 
  105.                     temp=(TREE *)malloc(TREESIZE); 
  106.                     assert(temp!=NULL); 
  107.                     temp->rchild=NULL; 
  108.                     temp->lchild=NULL; 
  109.                     temp->student =node; 
  110.                     root->lchild =temp; 
  111.                 } 
  112.                 else                             /*記錄不爲空*/ 
  113.                 { 
  114.                   Insert_Information(root->lchild,node); /*進行插入*/ 
  115.                 } 
  116.                 printf("/n/n添加成功/n"); 
  117.             }break; 
  118.         case 2:                                  /*瀏覽*/ 
  119.             { 
  120.                 if(root->lchild ==NULL)          /*爲空不能進行瀏*/ 
  121.                     printf("記錄爲空,不能進行瀏覽!/n"); 
  122.                 else                             /*進行瀏覽*/ 
  123.                 { 
  124.                    Look_Through_Information(root->lchild); 
  125.                 } 
  126.             }break; 
  127.         case 3:                                  /*保存*/ 
  128.             { 
  129.                 if(root->lchild ==NULL)          /*記錄爲空不能進行保存*/ 
  130.                     printf("記錄爲空,不能進行保存!/n"); 
  131.                 else 
  132.                 { 
  133.                    output=fopen("shuwen.txt","w");/*打開文件*/ 
  134.                    Save_Information(root->lchild);        /*進行保存*/ 
  135.                    printf("保存成功!"); 
  136.                    fclose(output);               /*關閉文件*/ 
  137.                 } 
  138.             }break; 
  139.         case 4:                                  /*查尋*/ 
  140.             { 
  141.                 if(root->lchild==NULL)           /*爲空不能進行查尋*/ 
  142.                     printf("記錄爲空,不能進行查尋!/n"); 
  143.                 else 
  144.                 { 
  145.                    printf("請輸入你要進行查找的學號:"); 
  146.                    scanf("%s",b); 
  147.                    printf("/n/n"); 
  148.                    if((Find_Information(root->lchild ,b))==0)/*沒有找到進行提示*/ 
  149.                      printf("沒有找到你要進行查找的記錄!/n"); 
  150.                 } 
  151.             }break;  
  152.         case 5:                                  /*修改*/ 
  153.             { 
  154.                 if(root->lchild==NULL)           /*爲空不能進行修改*/ 
  155.                     printf("記錄爲空,不能進行修改!/n"); 
  156.                 else                             /*進行查尋*/ 
  157.                 { 
  158.                    printf("請輸入你要進行修改的學號:"); 
  159.                    scanf("%s",b); 
  160.                    printf("/n/n"); 
  161.                    if((Rework_Information(root->lchild ,b))==0)/*沒有查尋到提示*/ 
  162.                       printf("沒有找到你要進行修改的記錄!/n"); 
  163.                 } 
  164.             }break; 
  165.         case 6:                                  /*刪除*/ 
  166.             { 
  167.                 if(root->lchild ==NULL)          /*爲空不能進行刪除*/ 
  168.                     printf("記錄爲空,不能進行刪除"); 
  169.                 else 
  170.                 { 
  171.                    printf("請輸入你要進行刪除的學號:"); 
  172.                    scanf("%s",b); 
  173.                    Delete_Information(root,b);         /*進行刪除*/ 
  174.                 } 
  175.             }break; 
  176.         case 7: exit(0);break; 
  177.         } 
  178.         printf("/n/n"); 
  179.     } 
  180.     Free_EMS_memory(root); 
  181. /**********************************************************************************
  182. *                               菜單函數
  183. *描述:該函數用於對屏幕進行初始化。
  184. *
  185. *參數:  無
  186. *
  187. *返回值:無
  188. *
  189. *注意事項:
  190. **********************************************************************************/ 
  191. void Menu(void ) 
  192.     printf("*************   1 添加  *************/n"); 
  193.     printf("*************   2 瀏覽  *************/n"); 
  194.     printf("*************   3 保存  *************/n"); 
  195.     printf("*************   4 查尋  *************/n"); 
  196.     printf("*************   5 修改  *************/n"); 
  197.     printf("*************   6 刪除  *************/n"); 
  198.     printf("*************   7 結束  *************/n"); 
  199. /**********************************************************************************
  200. *                              創建空二叉樹函數
  201. *描述:該函數用於創建空二叉樹。
  202. *
  203. *參數:  無
  204. *
  205. *返回值:根結點
  206. *
  207. *注意事項:該結點不進行保存數據。
  208. **********************************************************************************/ 
  209. TREE *Create_Tree(void ) 
  210.     /*根結點不進行保存記錄,只用於做爲真正的根結點*/ 
  211.     TREE *root; 
  212.     root=(TREE *)malloc(TREESIZE); 
  213.     assert(root !=NULL); 
  214.     root->student=NULL; 
  215.     root->rchild=NULL; 
  216.     root->lchild=NULL; 
  217.     return root; 
  218. /**********************************************************************************
  219. *                               插入個人信息函數
  220. *描述:該函數用於插入個人信息。
  221. *
  222. *參數:  根結點、記錄有個人信息的結點
  223. *
  224. *返回值:無
  225. *
  226. *注意事項:
  227. **********************************************************************************/ 
  228. void Insert_Information(TREE *root,NODE *node) 
  229.     TREE *Node; 
  230.     if((strcmp(root->student->code,node->code))>0)/*插入到左子樹*/ 
  231.     { 
  232.         if(root->lchild==NULL)                   /*左孩子爲空,直接插入*/ 
  233.         { 
  234.             Node=(TREE *)malloc(TREESIZE); 
  235.             assert(Node !=NULL); 
  236.             Node->rchild=NULL; 
  237.             Node->lchild=NULL; 
  238.             Node->student=node; 
  239.             root->lchild=Node; 
  240.         } 
  241.         else                                     /*進行遞歸插入*/ 
  242.            Insert_Information(root->lchild,node); 
  243.     } 
  244.     else                                         /*插入到右子樹*/ 
  245.     { 
  246.         if(root->rchild==NULL)                  /*爲空直接插入*/ 
  247.         { 
  248.             Node=(TREE *)malloc(TREESIZE); 
  249.             assert(Node!=NULL); 
  250.             Node->rchild=NULL; 
  251.             Node->lchild=NULL; 
  252.             Node->student=node; 
  253.             root->rchild=Node; 
  254.         } 
  255.         else                                     /*遞歸插入*/ 
  256.             Insert_Information(root->rchild,node); 
  257.     } 
  258. /**********************************************************************************
  259. *                               瀏覽函數
  260. *描述:該函數用於進行瀏覽所有的個人記錄。
  261. *
  262. *參數:  根結點
  263. *
  264. *返回值:無
  265. *
  266. *注意事項:
  267. **********************************************************************************/ 
  268. void Look_Through_Information(TREE *root)                        /*中序遍歷法輸出*/ 
  269.     if(root!=NULL) 
  270.     { 
  271.         Look_Through_Information(root->lchild); 
  272.         printf("%s %s %d/n",root->student->code,root->student->name,root->student->age); 
  273.         Look_Through_Information(root->rchild); 
  274.     } 
  275. /**********************************************************************************
  276. *                               保存函數
  277. *描述:該函數用於將所要進行保存的記錄進行保存。
  278. *
  279. *參數:  根結點
  280. *
  281. *返回值:無
  282. *
  283. *注意事項:本處應用前序遍歷法進行遍歷二叉樹,防止再次進行初始化時產生斜樹
  284. **********************************************************************************/  
  285. void Save_Information(TREE *root) 
  286.     if(root!=NULL) 
  287.     { 
  288.         fprintf(output,"%s %s %d/n",root->student->code,root->student->name,root->student->age); 
  289.         Save_Information(root->lchild); 
  290.         Save_Information(root->rchild); 
  291.     } 
  292. /**********************************************************************************
  293. *                               查尋函數
  294. *描述:該函數用於進行查尋記錄。
  295. *
  296. *參數:  根結點、學號
  297. *
  298. *返回值:是否找到進行查尋的記錄
  299. *        1:表找到     0:表沒有找到
  300. *
  301. *注意事項:
  302. **********************************************************************************/ 
  303. int Find_Information(TREE *root,char *code) 
  304.     if(root==NULL) 
  305.         return 0; 
  306.     else if((strcmp(root->student ->code ,code))==0)/*找到*/ 
  307.     { 
  308.         printf("找到你要進行查錄的記錄,記錄如下/n"); 
  309.         printf("%s %s %d/n",root->student ->name ,root->student ->code ,root->student ->age ); 
  310.         return 1; 
  311.     } 
  312.     else if((strcmp(root->student ->code ,code))>0)/*在左子樹中進行查找*/ 
  313.         Find_Information(root->lchild ,code); 
  314.     else                                         /*在右子樹中進行查尋*/ 
  315.         Find_Information(root->rchild,code); 
  316. /**********************************************************************************
  317. *                               修改函數
  318. *描述:該函數用於修改要進行修改的記錄。
  319. *
  320. *參數:  根結點、學號
  321. *
  322. *返回值:是否修改成功
  323. *        1:修改成功     0:沒有該記錄
  324. *
  325. *注意事項:
  326. **********************************************************************************/ 
  327. int Rework_Information(TREE *root,char *code) 
  328.     if(root==NULL)                               /*沒有找到要進行修改的結點*/ 
  329.        return 0; 
  330.     else if((strcmp(root->student ->code ,code))==0)/*找到*/ 
  331.     { 
  332.         char name[30]; 
  333.         int age; 
  334.         printf("請輸入你要進行修改的姓名,年齡:"); 
  335.         scanf("%s %d",name,&age); 
  336.         /*進行修改*/ 
  337.         root->student ->age =age; 
  338.         strcpy(root->student ->name ,name); 
  339.         printf("修改成功/n"); 
  340.         return 1; 
  341.     } 
  342.     else if((strcmp(root->student ->code ,code))>0)/*在左子樹中進行查尋*/ 
  343.         Rework_Information(root->lchild ,code); 
  344.     else                                         /*在右子樹中進行查尋*/ 
  345.        Rework_Information(root->rchild ,code); 
  346. /**********************************************************************************
  347. *                               查尋要進刪除結點的雙親結點
  348. *描述:該函數用於查尋將要進行刪除結點的雙親結點。
  349. *
  350. *參數:  根結點、學號
  351. *
  352. *返回值:雙親結點
  353. *
  354. *注意事項:
  355. **********************************************************************************/ 
  356. TREE *Find_Father_Node(TREE *root,char *code) 
  357.     if(root->rchild==NULL && root->lchild==NULL) /*沒有找到*/ 
  358.         return NULL; 
  359.     else if((root->lchild!=NULL)&&((strcmp(root->lchild->student ->code,code))==0))/*找到爲左結點*/ 
  360.         return root; 
  361.     else if((root->rchild!=NULL) &&((strcmp(root->rchild->student ->code,code))==0))/*找到爲右結點*/ 
  362.         return root; 
  363.     else 
  364.     { 
  365.         if((root->lchild!=NULL) &&((strcmp(root->student ->code,code))>0))/*在左子樹中進行查尋*/ 
  366.             return (Find_Father_Node(root->lchild,code)); 
  367.         else if((root->rchild!=NULL) &&((strcmp(root ->rchild->student ->code,code))<0))/*在右子樹中進行查尋*/ 
  368.             return (Find_Father_Node(root->rchild,code)); 
  369.         else 
  370.             return NULL; 
  371.     } 
  372. /**********************************************************************************
  373. *                               刪除個人記錄
  374. *描述:該函數用於刪除個人記錄。
  375. *
  376. *參數:  根結點、學號
  377. *
  378. *返回值:無
  379. *
  380. *注意事項:
  381. **********************************************************************************/ 
  382. void Delete_Information(TREE *root,char *code) 
  383.     TREE *node; 
  384.     TREE *per; 
  385.     TREE *p; 
  386.     TREE *s;  
  387.     if((strcmp(root->lchild ->student ->code ,code))==0)                          /*是根結點情況*/ 
  388.     { 
  389.         if(root->lchild ->lchild ==NULL && root->lchild ->rchild  ==NULL) 
  390.         { 
  391.             node=root->lchild ; 
  392.             root->lchild =NULL; 
  393.             free(node); 
  394.         } 
  395.         else if(root->lchild ->lchild ==NULL && root->lchild ->rchild !=NULL) 
  396.         { 
  397.             node=root->rchild ; 
  398.             root->lchild=root->lchild ->rchild ; 
  399.             free(node); 
  400.         } 
  401.         else if(root->lchild ->rchild ==NULL && root->lchild ->lchild !=NULL) 
  402.         { 
  403.             node=root->lchild ; 
  404.             root->lchild=root->lchild ->lchild ; 
  405.             free(node); 
  406.         } 
  407.         else 
  408.         { 
  409.             node=root->lchild ; 
  410.             s=node ->rchild ; 
  411.             if(s->lchild ==NULL) 
  412.                 node->rchild =s->rchild ; 
  413.             else 
  414.             { 
  415.               per=node; 
  416.               while(s->lchild !=NULL) 
  417.               { 
  418.                per=s; 
  419.                s=s->lchild ; 
  420.               } 
  421.               per->lchild =s->rchild ; 
  422.             } 
  423.             node->student  =s->student ;  
  424.             free(s); 
  425.         } 
  426.         printf("刪除成功!"); 
  427.     } 
  428.     else 
  429.     { 
  430.         node=Find_Father_Node(root->lchild ,code);                 /*得到要進行刪除結點的父結點*/ 
  431.         if(node==NULL)                                     /*沒有找到*/ 
  432.             printf("沒有找到你要進行刪除的數據!/n"); 
  433.         else 
  434.         { 
  435.             if(node->lchild!=NULL && (strcmp(node->lchild->student ->code,code))==0)/*在父結點的左子樹*/ 
  436.             { 
  437.                 p=node->lchild; 
  438.                 if(p->lchild ==NULL && p->rchild ==NULL)/*葉子情況*/ 
  439.                 { 
  440.                     node->lchild=NULL; 
  441.                     free(p); 
  442.                 } 
  443.                 else if(p->lchild !=NULL && p->rchild ==NULL)/*只有左孩子*/ 
  444.                 { 
  445.                     node->lchild =p->lchild ; 
  446.                     free(p); 
  447.                 } 
  448.                 else if(p->rchild !=NULL && p->lchild ==NULL)/*只有右孩子*/ 
  449.                 { 
  450.                     node->lchild =p->rchild ; 
  451.                     free(p); 
  452.                 } 
  453.                 else                                      /*有左右孩子*/ 
  454.                 { 
  455.                     per=p; 
  456.                     s=p->rchild ; 
  457.                     while(s->lchild !=NULL) 
  458.                     { 
  459.                         per=s; 
  460.                         s=s->lchild ; 
  461.                     } 
  462.                     p->student  =s->student ; 
  463.                     if(per==s) 
  464.                         per->rchild =s->rchild ; 
  465.                     else 
  466.                         per->lchild =s->rchild ; 
  467.                     free(s); 
  468.                 }  
  469.             } 
  470.             else                                 /*在右子樹*/ 
  471.             { 
  472.                 p=node->rchild; 
  473.                 if(p->lchild ==NULL && p->rchild ==NULL) 
  474.                 { 
  475.                     node->rchild=NULL; 
  476.                     free(p); 
  477.                 } 
  478.                 else if(p->lchild !=NULL && p->rchild ==NULL) 
  479.                 { 
  480.                     node->rchild =p->lchild ; 
  481.                     free(p); 
  482.                 } 
  483.                 else if(p->rchild !=NULL && p->lchild ==NULL) 
  484.                 { 
  485.                     node->rchild =p->rchild ; 
  486.                     free(p); 
  487.                 } 
  488.                 else 
  489.                 { 
  490.                     per=p; 
  491.                     s=p->rchild ; 
  492.                     while(s->lchild !=NULL) 
  493.                     { 
  494.                         per=s; 
  495.                         s=s->lchild ; 
  496.                     } 
  497.                     p->student  =s->student  ; 
  498.                     if(per==s) 
  499.                         per->rchild =s->rchild ; 
  500.                     else 
  501.                         per->lchild =s->rchild ; 
  502.                     free(s); 
  503.                 }  
  504.             }  
  505.             printf("刪除成功!");  
  506.         } 
  507.     } 
  508. /**********************************************************************************
  509. *                              釋放內存函數
  510. *描述:該函數用於釋放內存。
  511. *
  512. *參數:  根結點
  513. *
  514. *返回值:無
  515. *
  516. *注意事項:
  517. **********************************************************************************/ 
  518. void Free_EMS_memory(TREE *root) 
  519.     if(root!=NULL) 
  520.     { 
  521.         Free_EMS_memory(root->lchild); 
  522.         Free_EMS_memory(root->rchild); 
  523.         free(root); 
  524.     } 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章