码迷,mamicode.com
首页 > 编程语言 > 详细

5.3 二叉排序树

时间:2016-07-19 09:06:36      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

5-4 BinarySortTree.c

  1 #include <stdio.h>
  2 #define ARRAYLEN 10
  3 int source[]={54,90,6,69,12,37,92,28,65,83};
  4 typedef struct bst
  5 {
  6     int data;
  7     struct bst *left;
  8     struct bst * right;
  9 }BSTree;
 10 void InsertBST(BSTree *t,int key)//在二叉排序树中插入查找关键字key
 11 {
 12     BSTree *p,*parent,*head;
 13     if(!(p=(BSTree *)malloc(sizeof(BSTree *)))) //申请内存空间 
 14     {
 15         printf("申请内存出错!\n");
 16         exit(0); 
 17     }
 18     p->data=key; //保存结点数据 
 19     p->left=p->right=NULL; //左右子树置空    
 20     head=t;
 21     while(head) //查找需要添加的父结点 
 22     {
 23         parent=head;
 24         if(key<head->data) //若关键字小于结点的数据 
 25             head=head->left; //在左子树上查找 
 26         else                 //若关键字大于结点的数据 
 27             head=head->right;  //在右子树上查找                             
 28     }
 29     //判断添加到左子树还是右子树 
 30     if(key<parent->data) //小于父结点 
 31         parent->left=p; //添加到左子树
 32     else           //大于父结点 
 33         parent->right=p; //添加到右子树 
 34 }
 35 BSTree *SearchBST(BSTree *t,int key)
 36 {
 37     if(!t || t->data==key) //结点为空,或关键字相等 
 38         return t;          //返回结点指针
 39     else if(key>t->data) //关键字大于结点数据
 40         return(SearchBST(t->right,key));
 41     else
 42         return(SearchBST(t->left,key));
 43 }
 44 void CreateBST(BSTree *t,int data[],int n)//n个数据在数组d中,tree为二叉排序树根
 45 {
 46     int i;
 47     t->data=data[0];
 48     t->left=t->right=NULL;
 49     for(i=1;i<n;i++)
 50     {
 51         InsertBST(t,data[i]);
 52     }
 53 }
 54 void BST_LDR(BSTree *t)  //中序遍历 
 55 {
 56      if(t)//树不为空,则执行如下操作 
 57      {
 58          BST_LDR(t->left); //中序遍历左子树
 59          printf("%d ",t->data); //输出结点数据 
 60          BST_LDR(t->right); //中序遍历右子树/
 61      }
 62      return; 
 63 } 
 64 //删除结点
 65 void BST_Delete(BSTree *t,int key)
 66 {
 67     BSTree *p,*parent,*l,*l1;
 68     int child=0;//0表示左子树,1表示右子树 
 69     if(!t) return;     //二叉排序树为空,则退出
 70     p=t;
 71     parent=p;
 72     while(p)           //二叉排序树有效 
 73     {        
 74         if(p->data==key)
 75         {
 76             if(!p->left && !p->right) //叶结点(左右子树都为空) 
 77             {
 78                 if(p==t) //被删除的是根结点 
 79                 {
 80                     free(p);//释放被删除结点 
 81                 }
 82                 else if(child==0) //父结点为左子树 
 83                 {
 84                     parent->left=NULL; //设置父结点左子树为空 
 85                     free(p); //释放结点空间 
 86                 }
 87                 else //父结点为右子树 
 88                 {
 89                     parent->right=NULL; //设置父结点右子树为空 
 90                     free(p); //释放结点空间 
 91                 }
 92             }
 93             else if(!p->left) //左子树为空,右子树不为空
 94             {
 95                 if(child==0) //是父结点的左子树
 96                     parent->left=p->right;
 97                 else //是父结点的右子树             
 98                     parent->left=p->left;
 99                 free(p); //释放被删除结点
100             }
101             else if(!p->right)//右子树为空,左子树不为空
102             {
103                  if(child==0) //是父结点的左子树
104                     parent->right=p->right;
105                 else //是父结点的右子树             
106                     parent->right=p->left;
107                 free(p); //释放被删除结点       
108             }
109             else  //左右子树都不为空 
110             {
111                 l1=p; //保存左子树的父结点 
112                 l=p->right; //从当前结点的右子树进行查找 
113                 while(l->left) //左子树不为空 
114                 {
115                     l1=l;
116                     l=l->left; //查找左子树 
117                 }
118                 p->data=l->data; //将左子树的数据保存到被删除结点
119                 l1->left=NULL; //设置父结点的左子树指针为空 
120                 free(l1); //释放左子树占的内存空间
121             }
122             p=NULL;
123         }
124         else if(key<p->data) //需删除记录的关键字小于结点的数据 
125         {
126             child=0;//标记在当前结点左子树查找
127             parent=p; //保存当前结点作父结点 
128             p=p->left; //查找右子树 
129         }
130         else //需删除记录的关键字大于结点的数据 
131         {
132             child=1;//标记在当前结点右子树查找
133             parent=p;//保存当前结点作父结点 
134             p=p->right; //查找右子树 
135         }    
136     } 
137 }
138 int main()
139 {
140     int i,key;
141     BSTree bst,*pos; //保存二叉排序树根结点
142     printf("原数据:"); 
143     for(i=0;i<ARRAYLEN;i++)
144         printf("%d ",source[i]);
145     printf("\n");
146     CreateBST(&bst,source,ARRAYLEN);
147     printf("遍历二叉排序树:"); 
148     BST_LDR(&bst);
149     BST_Delete(&bst,37);
150     printf("\n删除结点后的结点:"); 
151     BST_LDR(&bst);    
152     printf("\n请输入关键字:");
153     scanf("%d",&key); 
154    pos=SearchBST(&bst,key);
155     if(pos)
156        printf("查找成功,该结点的地址:%x\n",pos);
157     else
158         printf("查找失败!\n");
159     getch();
160     return 0;
161 }

 

5.3 二叉排序树

标签:

原文地址:http://www.cnblogs.com/wozixiaoyao/p/5683172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!