1 #include <IOSTREAM>
2 #include <QUEUE>
3 using namespace std;
4
5 //结点的结构体
6 //内涵三个参数,关键字Key,左孩子指针,右孩子指针
7 typedef struct _NODE_
8 {
9 int Key;
10 _NODE_* pLeft;
11 _NODE_* pRight;
12 }Node,*pNode;
13
14 //中序遍历
15 void InOrderTraves(pNode T);
16
17
18 //层状遍历
19 void LevelTraves(pNode T);
20
21 //搜索结点
22 bool SearchNode(pNode T, int Key, pNode& f, pNode& p);
23
24 //插入结点
25 bool InsertNode(pNode& T,int Key);
26
27 //删除结点
28 void DeleteNode(pNode& T,int Key);
29
30 //销毁树
31 void DestroyTree(pNode& T);
32
33 //测试函数
34 int main()
35 {
36 pNode BST = NULL;
37 int Key = 0;
38 cout<<"Create a BST: ";
39 cin>>Key;
40 while (Key != -1)
41 {
42 InsertNode(BST,Key);
43 cin>>Key;
44 }
45
46 InOrderTraves(BST);
47 cout<<endl;
48 LevelTraves(BST);
49 cout<<endl;
50
51 DestroyTree(BST);
52 return 0;
53 }
54
55 void InOrderTraves(pNode T)
56 {
57 if (T != NULL)
58 {
59 InOrderTraves(T->pLeft);
60 cout<<T->Key<<" ";
61 InOrderTraves(T->pRight);
62 }
63 }
64
65 //使用STL 的 queue队列
66 void LevelTraves(pNode T)
67 {
68 if (T == NULL)
69 {
70 return;
71 }
72
73 pNode p = T;
74
75 queue<pNode> Que;
76 Que.push(p);
77
78 while (!Que.empty())
79 {
80 p = Que.front();
81 Que.pop();
82 cout<<p->Key<<" ";
83
84 if (p->pLeft != NULL)
85 {
86 Que.push(p->pLeft);
87 }
88 if (p->pRight != NULL)
89 {
90 Que.push(p->pRight);
91 }
92 }
93
94 }
95
96
97 // 查找函数
98 // 参数:
99 // 树根T, 待查找的关键字Key,
100 // 指针f(用于存放找到结点的父节点)
101 // 指针p(用于存放找到结点)
102 bool SearchNode(pNode T, int Key, pNode& f, pNode& p)
103 {
104 p = T;
105 if (p == NULL)
106 {
107 return false;
108 }
109 while (p!=NULL)
110 {
111 if (p->Key == Key)
112 {
113 return true;
114 }
115 if (p->Key > Key)
116 {
117 f = p;
118 p = p->pLeft;
119 }
120 if (p->Key < Key)
121 {
122 f = p;
123 p = p->pRight;
124 }
125 }
126
127 return false;
128 }
129
130
131 bool InsertNode(pNode& T,int Key)
132 {
133
134 //如果根结点不存在,动态申请内存空间
135 if (T == NULL)
136 {
137 T = (pNode)malloc(sizeof(Node));
138
139 T->Key = Key;
140 T->pLeft = T->pRight = NULL;
141
142 return true;
143 }
144 //在保持BST的性质的情况下进行插入
145 else
146 {
147 if (T->Key == Key)
148 {
149 cout<<"Error"<<endl;
150 return false;
151 }
152 if (T->Key > Key)
153 {
154 InsertNode(T->pLeft,Key);
155 }
156 else
157 {
158 InsertNode(T->pRight,Key);
159 }
160 }
161
162 return false;
163 }
164
165
166 //有四种情况
167
168 /*
169 * 情况一: 待删结点为叶子结点(无左孩子也无又孩子)
170 * 直接将其父节点的左(或者右)孩子的值赋为空
171 *
172 * 情况二: 待删结点只有左子树
173 * 将其父节点的左子树赋为待删结点的左子树
174 *
175 * 情况三: 待删结点只有右子树
176 * 将其父节点的右子树赋为待删结点的右子树
177 *
178 * 情况四: 待删结点既有右子树,右有左子树
179 * 找到待删结点的前驱结点,替换待删结点。
180 */
181 void DeleteNode(pNode& T,int Key)
182 {
183
184 pNode p = NULL;
185 pNode q = NULL;
186 pNode s = NULL;
187 pNode f = NULL;
188
189 if(SearchNode(T,Key,f,p))
190 {
191 if (p->pLeft && p->pRight)
192 {
193 q = p;
194 //找到p的直接前驱
195 s = s->pLeft;
196 while (s->pRight != NULL)
197 {
198 q = s;
199 s = s->pRight;
200 }
201
202 //将值和直接前驱调换
203 p->Key = s->Key;
204
205 //这种情况是一般情况,q 和 p不相等
206 if (q != p)
207 {
208 q->pRight = s->pLeft;
209 }
210 //特殊情况
211 else
212 {
213 q->pLeft =s->pLeft;
214 }
215 }
216 else if(p->pLeft == NULL && p->pRight == NULL)
217 {
218 if (f->pRight == p)
219 {
220 f->pRight = NULL;
221 }
222 else
223 {
224 f->pLeft = NULL;
225 }
226 }
227 else if (p->pLeft != NULL && p->pRight == NULL)
228 {
229 f->pLeft = p->pLeft;
230 }
231 else
232 {
233 f->pRight = p->pRight;
234 }
235 }
236
237 }
238
239 void DestroyTree(pNode& T)
240 {
241 if (T != NULL)
242 {
243 if (T->pLeft != NULL)
244 {
245 DestroyTree(T->pLeft);
246 }
247 if (T->pRight != NULL)
248 {
249 DestroyTree(T->pRight);
250 }
251
252 free(T);
253 }
254 }