二叉树--C语言实现

数据结构与算法分析 笔记

searchtree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <stdio.h>;
#include <stdlib.h>;
#define FatalError(str) fprintf(stderr, "%s\n", str),exit(1);
#define Error(str) FatalError(str);

struct TreeNode;
typedef struct TreeNode *Position;
typedef struct TreeNode *SearchTree;
typedef int ElementType;

SearchTree MakeEmpty(SearchTree T);
Position Find(ElementType X, SearchTree T);
Position FindMin(SearchTree);
Position FindMax(SearchTree);
SearchTree Insert(ElementType X, SearchTree T);
SearchTree Delete(ElementType X, SearchTree T);
ElementType Retrieve(Position P);
ElementType DeleteMin(SearchTree T);

struct TreeNode {
ElementType Element;
Position Left;
Position Right;
};

SearchTree MakeEmpty(SearchTree T)
{
if(T != NULL){
T-&gt;Element = NULL;
MakeEmpty(T-&gt;Left);
MakeEmpty(T-&gt;Right);
}
return NULL;
}

Position Find(ElementType X, SearchTree T)
{
if(T == NULL){
return NULL;
}else
if(X &lt; T-&gt;Element){
return Find(X, T-&gt;Left);
}else
if(X &gt; T-&gt;Element){
return Find(X, T-&gt;Right);
}else{
return T;
}
}

Position FindMin(SearchTree T)
{
if(T == NULL){
return NULL;
}else
if(T-&gt;Left == NULL){
return T;
}else{
return FindMin(T-&gt;Left);
}
}

Position FindMax(SearchTree T)
{
if (T != NULL){
while(T-&gt;Right != NULL){
T = T-&gt;Right;
}
}
return T;
}

// Position FindMax(SearchTree T)
// {
// if(T == NULL){
// return NULL;
// }else
// if(T-&gt;Right == NULL){
// return T;
// }else{
// return FindMax(T-&gt;Right);
// }
// }

SearchTree Insert(ElementType X, SearchTree T)
{
if(T == NULL)
{
T = malloc(sizeof(struct TreeNode));
if( T == NULL)
{
FatalError("Out of space!!!");
}
else
{
T-&gt;Element = X;
T-&gt;Left = T-&gt;Right = NULL;
}
}
else
if(X &lt; T-&gt;Element)
{
T-&gt;Left = Insert(X, T-&gt;Left);
}
else
if(X &gt; T-&gt;Element)
T-&gt;Right = Insert(X, T-&gt;Right);
return T;
}

ElementType Retrieve(Position P)
{
return P-&gt;Element;
}

SearchTree Delete(ElementType X, SearchTree T)
{
Position P;
if(T == NULL)
{
Error("Element not found");
}
else
if(X &lt; T-&gt;Element)
{
T = T-&gt;Left;
return Delete(X, T);
}
else
if(X &gt; T-&gt;Element)
{
T = T-&gt;Right;
return Delete(X, T);
}
else
if(T-&gt;Left &amp;&amp; T-&gt;Right)
{
T-&gt;Element = DeleteMin(T-&gt;Right);
}
else
{
P = T;
if(T-&gt;Left == NULL)
T = T-&gt;Right;
else
if(T-&gt;Right == NULL)
T = T-&gt;Left;
free(P);
}
return T;
}

ElementType DeleteMin(SearchTree T)
{
Position P;
ElementType X;
P = FindMin(T);
X = P-&gt;Element;
free(P);
return X;
}

void PrintSearchTree(SearchTree T)
{
if(T == NULL)
{
Error("Element not found");
}
if(T-&gt;Left == NULL &amp;&amp; T-&gt;Right == NULL)
{
printf("%d ", T-&gt;Element);
}else{
if(T-&gt;Left != NULL)
{
PrintSearchTree(T-&gt;Left);
}
printf("%d ", T-&gt;Element);
if(T-&gt;Right != NULL)
{
PrintSearchTree(T-&gt;Right);
}
}
}

void SearchTreeTest()
{
SearchTree T;
T = MakeEmpty(T);
T = Insert(1,T);
T = Insert(3,T);
T = Insert(2,T);
T = Insert(6,T);
T = Insert(4,T);
PrintSearchTree(T);
}

`</pre>

> main.c

<pre class="line-numbers prism-highlight" data-start="1">`#include "searchtree.c"

int main(void)
{
SearchTreeTest();

return 0;
}