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

Posted hi3254014978

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉查找树的建立,插入,删除例程相关的知识,希望对你有一定的参考价值。

  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!!!
");
 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
", T->value);
 56 }
 57 
 58 TreeNode* FinMin(TreeNode* T)
 59 {
 60     
 61     if (T == NULL)
 62     {
 63         printf("the tree is empty!
");
 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!!!
");
 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:
");
121     //scanf_s("%d", &val);
122     T = Insert(T, 5);
123 
124     InOrderTraversal(T, 0);
125 
126     T2 = FinMin(T);
127     printf("
%d
", T2->value);
128 
129     printf("
After deleting:
");
130     Delete(T, 6);
131     InOrderTraversal(T, 0);
132 
133     return 0;
134 }

 

以上是关于二叉查找树的建立,插入,删除例程的主要内容,如果未能解决你的问题,请参考以下文章

用C语言实现二叉排序的建立。查询。删除。插入

平衡二叉树的定义及基本操作(查找插入删除)及代码实现

重温数据结构:二叉排序树的查找插入删除

搜索二叉树

二叉查找树中元素的删除操作

二叉排序树的定义及基本操作(构造查找插入删除)递归及非递归算法