首页 > 其他 > 详细

二叉查找树的建立,插入,删除例程

时间:2018-08-22 23:54:55      阅读:234      评论:0      收藏:0      [点我收藏+]
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 typedef struct TreeNode{
  5     int value;
  6     struct TreeNode* Left;
  7     struct TreeNode* Right;
  8 }TreeNode;
  9 
 10 void printTree(TreeNode* T, int depth);
 11 
 12 TreeNode *Insert(TreeNode* T, int val)
 13 {
 14     if (!T)
 15     {
 16         TreeNode * T = (TreeNode*)malloc(sizeof(TreeNode));
 17         //int val;
 18         if (T == NULL)
 19         {
 20             printf("out of space!!!\n");
 21             return NULL;
 22         }
 23         T->value = val;
 24         T->Left = T->Right = NULL;
 25         return T;
 26     }
 27     else
 28      
 29         if (val < T->value)
 30             T->Left = Insert(T->Left, val);
 31         else if (val > T->value)
 32             T->Right = Insert(T->Right, val);
 33         return T;
 34     
 35     
 36 
 37     
 38 
 39 }
 40 
 41 void InOrderTraversal(TreeNode* T, int depth)
 42 {
 43     if (T)
 44     {
 45         InOrderTraversal(T->Left, depth + 1);
 46         printTree(T, depth);
 47         InOrderTraversal(T->Right, depth + 1);
 48     }
 49 }
 50 
 51 void printTree(TreeNode* T, int depth)
 52 {
 53     while (depth--)
 54         printf("  ");
 55     printf("%d\n", T->value);
 56 }
 57 
 58 TreeNode* FinMin(TreeNode* T)
 59 {
 60     
 61     if (T == NULL)
 62     {
 63         printf("the tree is empty!\n");
 64         return NULL;
 65     }
 66     while (T->Left)
 67         T = T->Left;
 68     return T;
 69 }
 70 
 71 TreeNode *Delete(TreeNode *T, int val)
 72 {
 73     TreeNode * temp;
 74     if (T == NULL)
 75     {
 76         printf("The tree is empty!!!\n");
 77         return NULL;
 78     }
 79     else
 80     if (val > T->value)
 81         T->Right = Delete(T->Right, val);
 82     else
 83     if (val < T->value)
 84         T->Left = Delete(T->Left, val);
 85     else if (T->Left &&T->Right)
 86     {
 87         temp = FinMin(T -> Right);
 88         T->value = temp->value;
 89         T->Right = Delete(T->Right, temp->value);
 90     }
 91     else
 92     {
 93         temp = T;
 94         if (!T->Left)
 95             T = T->Right;
 96         else
 97 
 98         if (!T->Right)
 99             T = T->Left;
100         free(temp);
101 
102     }
103     return T;
104 }
105 
106 int main()
107 {
108     TreeNode *T = NULL;
109     TreeNode *T2 = NULL;
110     int val;
111     /*scanf_s("%d", &val);
112     T->value = val;*/
113     //T->Left = T->Right = NULL;
114     for (int i = 0; i < 6; i++)
115     {
116         scanf_s("%d", &val);
117         T = Insert(T, val);
118     }
119     InOrderTraversal(T, 0);
120     printf("After Insertintg:\n");
121     //scanf_s("%d", &val);
122     T = Insert(T, 5);
123 
124     InOrderTraversal(T, 0);
125 
126     T2 = FinMin(T);
127     printf("\n%d\n", T2->value);
128 
129     printf("\nAfter deleting:\n");
130     Delete(T, 6);
131     InOrderTraversal(T, 0);
132 
133     return 0;
134 }

 

二叉查找树的建立,插入,删除例程

原文:https://www.cnblogs.com/hi3254014978/p/9520812.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!