码迷,mamicode.com
首页 > 其他好文 > 详细

顺序二叉树实现

时间:2017-12-06 12:58:38      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:clu   scan   delete   arch   style   while   printf   let   输入   

  1 #include"stdio.h"  
  2 #include"stdlib.h"  
  3 #include"iostream"
  4 
  5 typedef struct bstnode{
  6     int data;
  7     struct bstnode *lchild;
  8     struct bstnode *rchild;
  9 }bstnode, *BSTree; //结构体类型,BSTree为结构体指针
 10 
 11 //函数声明区
 12 BSTree create_BSTree(int *arr, int len);
 13 BSTree search(BSTree pTree, int key);
 14 void in_traverse(BSTree pTree);
 15 bool search_BSTree(BSTree pTree, int key, BSTree parent, BSTree &p);
 16 bool insert(BSTree &pTree, int key);
 17 
 18 //根据所给的长为len的arr数组,按数组中元素的顺序构建一棵二叉排序树
 19 BSTree create_BSTree(int *arr, int len) {
 20     BSTree pTree = NULL;  //初始化树为空树
 21     int i;
 22     for (i = 0; i<len; i++)
 23         insert(pTree, arr[i]);  //
 24     return pTree;   //将数组中的数插入后返回
 25 }
 26 
 27 void in_traverse(BSTree pTree) {
 28     if (pTree){      //如果树不为空则中序递归的打印出树中的元素
 29         if (pTree->lchild)
 30             in_traverse(pTree->lchild);
 31         printf("%d ", pTree->data);
 32         if (pTree->rchild)
 33             in_traverse(pTree->rchild);
 34     }
 35 }
 36 
 37 BSTree search(BSTree pTree, int key){
 38     if (!pTree || pTree->data == key)   //如果树不为空,
 39         return pTree;
 40     else if (key < pTree->data)        //如果key的值小于data,则往左搜索,否则往右,递归的进行
 41         return search(pTree->lchild, key);
 42     else                              
 43         return search(pTree->rchild, key);
 44 }
 45 
 46 bool search_BSTree(BSTree pTree, int key, BSTree parent, BSTree &p){     //用于查找到inset函数要用到的待插入元素位置的指针
 47     if (!pTree){     //如果树中没有元素,直接返回根节点
 48         p = parent;
 49         return false;
 50     }
 51     else {
 52         if (key == pTree->data){
 53             p = pTree;   //p保存访问的最后一个结点指针
 54             return true;
 55         }
 56         else if (key < pTree->data)
 57             return search_BSTree(pTree->lchild, key, pTree, p);
 58         else                       
 59             return search_BSTree(pTree->rchild, key, pTree, p);
 60     }
 61 }
 62 
 63 bool insert(BSTree &pTree, int key){
 64     BSTree p;
 65     if (!search_BSTree(pTree, key, NULL, p)){
 66         BSTree pNew = (BSTree)malloc(sizeof(bstnode));
 67         pNew->data = key;
 68         pNew->lchild = pNew->rchild = NULL;   //分配一个新节点
 69         if (!p)                        
 70             pTree = pNew;
 71         else if (key < p->data)         
 72             p->lchild = pNew;          
 73         else
 74             p->rchild = pNew;
 75     }
 76     else
 77         return false;
 78 }
 79 void delete_bstnode1(BSTree &p){
 80     BSTree q, s;
 81     if (!p->lchild){  
 82         q = p;
 83         p = p->rchild;
 84         free(q);
 85     }
 86     else if (!p->rchild){  
 87         q = p;
 88         p = p->lchild;
 89         free(q);
 90     }
 91     else{     
 92         s = p->lchild;      
 93         while (s->rchild)
 94             s = s->rchild;
 95         s->rchild = p->rchild;  
 96         q = p;
 97         p = p->lchild;    
 98         free(q);
 99     }
100 }
101 
102 bool delete_BSTree(BSTree &pTree, int key){
103     if (!pTree)
104         return false;
105     else{
106         if (key == pTree->data){
107             delete_bstnode1(pTree);
108             return true;
109         }
110         else if (key < pTree->data)  
111             return delete_BSTree(pTree->lchild, key);
112         else                       
113             return delete_BSTree(pTree->rchild, key);
114     }
115 }
116 
117 void main(){
118     int i;
119     int num;
120     printf("请输入节点个数:");
121     scanf_s("%d", &num);
122     int *arr = (int *)malloc(num * sizeof(int));
123     printf("请依次输入这%d个整数(必须互不相等):", num);
124     for (i = 0; i<num; i++)
125         scanf_s("%d", arr + i);
126     BSTree pTree = create_BSTree(arr, num);
127     printf("中序遍历该二叉排序树的结果:");
128     in_traverse(pTree);
129     printf("\n");
130     int key;
131     printf("请输入要查找的整数:");
132     scanf_s("%d", &key);
133     if (search(pTree, key))
134         printf("查找成功,%d存在\n",key);
135     else
136         printf("查找不到该整数\n");
137     printf("请输入要插入的整数:");
138     scanf_s("%d", &key);
139     if (insert(pTree, key)){
140         printf("插入成功,插入后的中序遍历结果:");
141         in_traverse(pTree);
142         printf("\n");
143     }
144     else
145         printf("插入失败,该二叉排序树中已经存在整数%d\n", key); 
146     printf("请输入要删除的整数:");
147     scanf_s("%d", &key);
148     if (delete_BSTree(pTree, key)){
149         printf("删除成功,插入后的中序遍历结果:");
150         in_traverse(pTree);
151         printf("\n");
152     }
153     else
154         printf("删除失败,该二叉排序树中不存在整数%d\n", key);
155     system("pause");
156 }

 

顺序二叉树实现

标签:clu   scan   delete   arch   style   while   printf   let   输入   

原文地址:http://www.cnblogs.com/ixummer/p/7991794.html

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