二叉樹排序樹的建立,遍歷和刪除

ZHUO_SIR發表於2018-06-13
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3.   
  4. typedef struct bst{  
  5.     int data;  
  6.     struct bst *left;  
  7.     struct bst *right;  
  8. }BSTree;  
  9.   
  10. void Insert(BSTree *t,int key){  
  11.     BSTree *p;  
  12.     if(!p=((BSTree *)malloc(sizeof(BSTree))))  
  13.         printf("申請記憶體失敗\n");  
  14.     p->data=key;  
  15.     p->left=p->right=NULL;  
  16.     head=t;  
  17.     while(head){  
  18.         parent=head;  
  19.         if(key<head->data)  
  20.             head=head->left;  
  21.         else  
  22.             head=head->right;  
  23.     }  
  24.     if(key<parent->data)  
  25.         parent->left=head;  
  26.     else  
  27.         parent->right=head;  
  28. }  
  29.   
  30. //中序遍歷  
  31. void LDR(BSTree *t){  
  32.     if(bt){  
  33.         LDR(t->left);  
  34.         printf("%d",t->data);  
  35.         LDR(t->right);  
  36.     }  
  37.     return;  
  38. }  
  39.   
  40. void construct(BSTree *t,int data,int n){  
  41.     t->data=data[0];  
  42.     t->left=t->right=NULL;  
  43.     for(i=1;i<n;i++){  
  44.         Insert(t,data[i]);  
  45.     }  
  46. }  
  47.   
  48. BSTree *Search(BSTree *t,int key){  
  49.     if(!t || t->data==key)  return t;  
  50.     else if(key<t->data)    return (Search(t->left,key));  
  51.     else return (Search(t->right,key));  
  52. }  
  53.   
  54. /****************************************************  
  55.  * 刪除操作首先需要找到關鍵字  
  56.  * 然後根據關鍵字所在的位置更改二叉樹的結構  
  57.  *   
  58.  * 1. 如果關鍵字所在位置沒有左右子節點,也就是說是葉子節點  
  59.  *    那麼就直接刪除。在刪除的時候需要判斷這個節點是其父節點的左子樹(parent->left=NULL)還是右子樹(parent->right=NULL)  
  60.  * 2. 如果關鍵字所在位置只有右子樹,沒有左子樹,這個也很容易,就直接把其右子樹提上來就可以了  
  61.  *    在這之前需要判斷關鍵字所在位置是其父節點的左子樹還是右子樹  
  62.  * 3. 關鍵字所在位置只有左子樹,沒有右子樹  
  63.  *    同上面  
  64.  * 4. 關鍵字所在位置左右子樹都有  
  65.  *    這種情況的重點就是將關鍵字所在的位置替換為關鍵字的右子樹裡面的最小值  
  66.  * **************************************************/  
  67. void Delet(BSTree *t,int key){  
  68.     BSTree *p,*parent;  
  69.     p=t;  
  70.     while(p){  
  71.         if(p->data==key){  
  72.             //沒有左右子節點  
  73.             if(!p->left && !p->right){  
  74.                 if(p==t) free(p);  
  75.                 else if(p=parent->left){  
  76.                     parent->left=NULL;  
  77.                     free(p);  
  78.                 }else{  
  79.                     parent->right=NULL;  
  80.                     free(p);  
  81.                 }  
  82.             }  
  83.             // 只有右子樹  
  84.             else if(!p->left){  
  85.                 if(parent->left=p)  
  86.                     parent->left=p->right;  
  87.                 else      
  88.                     parent->right=p->right;  
  89.                 free(p);  
  90.             }  
  91.             //只有左子樹  
  92.             else if(!p->right){  
  93.                 if(parent->left=p)  
  94.                     parent->left=p->left;  
  95.                 else      
  96.                     parent->right=p->left;  
  97.                 free(p);  
  98.             }  
  99.             //左右子樹都有  
  100.             else{  
  101.                 BSTree *s,*sp;  
  102.                 sp=p;  
  103.                 s=p->right;  
  104.                 while(s->left){  
  105.                     sp=s;  
  106.                     s=s->left;  
  107.                 }  
  108.                 p->data=s->data;  
  109.                 sp->left=NULL;  
  110.                 free(sp);  
  111.             }  
  112.   
  113.         }else if(key<p->data){  
  114.             parent=p;  
  115.             p=p->left;  
  116.         }else{  
  117.             parent=p;  
  118.             p=p->right;  
  119.         }  
  120.     }  
  121. }  

相關文章