二叉树排序树的创建,遍历和删除

  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. }  

猜你喜欢

转载自blog.csdn.net/ZHUO_SIR/article/details/80685960