树和二叉树相关算法(一) c/c++
2021-02-16 12:17
标签:eof 中序遍历 输出 sizeof code ase 非递归算法 bool 保存 树和二叉树相关算法(一) c/c++ 标签:eof 中序遍历 输出 sizeof code ase 非递归算法 bool 保存 原文地址:https://www.cnblogs.com/cway/p/12708266.html 1 //双亲储存结构
2 typedef struct{
3 ElemType data;
4 int parent;
5 }PTree[MaxSize];
6
7 //孩子链储存结构
8 const int MaxSons = 10;
9 typedef struct node{
10 ElemType data;
11 struct node* sons[MaxSons];
12 }TSonNode;
13
14 //孩子兄弟链储存结构
15 typedef struct tnode{
16 ElemType data;
17 struct tnode* hp;
18 struct tnode* vp;
19 }TSBNode;
20
21 //二叉树顺序储存结构
22 typedef ElemType SqBinTree[MaxSize];
23
24 //二叉树链式储存结构
25 typedef struct bnode{
26 ElemType data;
27 struct bnode* lchild;
28 struct bnode* rchild;
29 }BTNode;
30
31 //二叉树三叉链表
32 typedef struct btnode{
33 ElemType data;
34 struct btnode* lchild;
35 struct btnode* parent;
36 struct btnode* rchild;
37 }BTTNode;
38
39 //孩子兄弟链求树的高度
40 int TreeHeight2(TSBNode *t)
41 {
42 TSBNode *p;
43 int maxh = 0;
44 int h = 0;
45 if(t == NULL)
46 return 0;
47 else
48 {
49 p = t;
50 while(p != NULL)
51 {
52 h = TreeHeight2(p->vp);
53 if(h > maxh) maxh = h;
54 p = p->hp;
55 }
56 return maxh+1;
57 }
58 }
59
60 //用括号表达式创建二叉树
61 void CreateBTree(BTNode *&b,char *str)
62 {
63 BTNode *St[MaxSize];int top = -1;
64 BTNode *p;
65 int k = 1;
66
67 b = NULL;
68 while(*str != ‘\0‘)
69 {
70 switch(*str)
71 {
72 case ‘(‘:
73 k = 1;
74 St[++top] = p;
75 break;
76 case ‘)‘:
77 top--;
78 break;
79 case ‘,‘:
80 k = 2;
81 break;
82 default:
83 p = (BTNode*)malloc(sizeof(BTNode));
84 p->data = *str;
85 p->lchild = NULL;
86 p->rchild = NULL;
87 if(b == NULL)
88 {
89 b = p;
90 }
91 else
92 {
93 if(k == 1)
94 St[top]->lchild = p;
95 else
96 St[top]->rchild = p;
97 }
98 }
99 str++;
100 }
101 }
102 //销毁二叉树
103 void DestroyBTree(BTNode *b)
104 {
105 if(b != NULL)
106 {
107 DestroyBTree(b->lchild);
108 DestroyBTree(b->rchild);
109 free(b);
110 }
111 }
112 //查找结点(基于先序遍历)
113 BTNode *FindNode(BTNode *b,ElemType e)
114 {
115 BTNode *p;
116 if(b == NULL)
117 return NULL;
118 else if(b->data == e)
119 return b;
120 else
121 {
122 p = FindNode(b->lchild,e);
123 if(p != NULL)
124 return p;
125 else
126 return FindNode(b->rchild,e);
127 }
128 }
129 //找孩子结点
130 BTNode *LchildNode(BTNode *b)
131 {
132 return b->lchild;
133 }
134 BTNode *RchildNode(BTNode *b)
135 {
136 return b->rchild;
137 }
138
139 //求二叉树的高度
140 int BTHeight(BTNode *b)
141 {
142 int lchildh,rchildh;
143 if(b == NULL)
144 return 0;
145 else
146 {
147 lchildh = BTHeight(b->lchild);
148 rchildh = BTHeight(b->rchild);
149 return lchildh>rchildh?:lchildh+1,rchildh+1;
150 }
151 }
152
153 //输出二叉树
154 void DispBTree(BTNode *b)
155 {
156 if(b != NULL)
157 {
158 coutdata;
159 if(b->lchild != NULL||b->rchild != NULL)
160 {
161 cout"(";
162 DispBTree(b->lchild);
163 if(b->rchild != NULL)cout",";
164 DispBTree(b->rchild);
165 cout")";
166 }
167 }
168 }
169
170 //递归算法遍历二叉树
171 void PreOrder(BTNode *b)
172 {
173 if( b!= NULL)
174 {
175 coutdata;
176 PreOrder(b->lchild);
177 PreOrder(b->rchild);
178 }
179 }
180
181 void InOrder(BTNode *b)
182 {
183 if(b != NULL)
184 {
185 InOrder(b->lchild);
186 coutdata;
187 InOrder(b->rchild);
188 }
189 }
190
191 void PostOrder(BTNode *b)
192 {
193 if(b != NULL)
194 {
195 PostOrder(b->lchild);
196 PostOrder(b->rchild);
197 coutdata;
198 }
199 }
200
201 //求给定二叉树结点个数
202
203 int Nodes(BTNode *b)
204 {
205 int num = 0;
206 if(b == NULL)
207 return 0;
208 else
209 return Nodes(b->lchild) + Nodes(b->rchild) + 1;
210 }
211
212 //输出所有叶子结点
213 void DispLeaf(BTNode *b)
214 {
215 if(b != NULL)
216 {
217 if(b->lchild == NULL && b->rchild == NULL)
218 coutdata;
219 else
220 {
221 DispLeaf(b->lchild);
222 DispLeaf(b->rchild);
223 }
224 }
225 }
226
227 //返回结点值为x的结点的深度
228 int Level(BTNode *b,ElemType e,int h)
229 {
230 int l;
231 if(b == NULL)
232 return 0;
233 else if(b->data == e)
234 return h;
235 else
236 {
237 l = Level(b->lchild,e,h+1);
238 if( l == 0)
239 return Level(b->rchild,e,h+1);
240 else
241 return l;
242 }
243 }
244
245 //求第k层结点个数
246 int Lnodenum(BTNode *b,int k,int h)
247 {
248 int num = 0;
249 if(b == NULL)
250 return 0;
251 if(h == k)
252 num++;
253 else if(h k)
254 {
255 num += Lnodenum(b->lchild,k,h+1);
256 num += Lnodenum(b->rchild,k,h+1);
257 }
258 return num;
259 }
260
261 //判断两棵树是否相似
262 bool Like(BTNode *b1,BTNode *b2)
263 {
264 bool like1,like2;
265 if(b1 == NULL && b2 == NULL)
266 return true;
267 else if(b1 == NULL || b2 == NULL)
268 return false;
269 else
270 {
271 like1 = Like(b1->lchild,b2->lchild);
272 like2 = Like(b1->rchild,b2->rchild);
273 return like1 && like2;
274 }
275 }
276
277 //输出值为x的结点的所有祖先
278 bool ancestor(BTNode *b,ElemType e)
279 {
280 if(b == NULL)
281 return false;
282 else if(b->lchild != NULL && b->lchild->data == e
283 || b->rchild != NULL && b->rchild->data == e)
284 {
285 coutdata;
286 return true;
287 }
288 else if(ancestor(b->lchild,e) || ancestor(b->rchild,e))
289 {
290 coutdata;
291 return true;
292 }
293 else
294 return false;
295 }
296
297 //非递归算法遍历二叉树 算法一
298 void PreOrder1(BTNode *b)
299 {
300 BTNode *St[MaxSize];int top=-1; //创建栈并初始化
301 BTNode *p; //结点p为当前处理的结点
302 if(b != NULL) //b不为空时
303 {
304 p = b; //p指向b
305 St[++top] = p; //p入栈
306 while(top!=-1) //栈不空时,说明有正在处理的结点
307 {
308 p = St[top]; //把当前处理的结点拿出来,避免丢失
309 coutdata; //访问结点并且出栈
310 top--;
311 if(p->rchild != NULL) //栈后进先出,所以先让右结点进栈
312 St[++top] = p->rchild;
313 if(p->lchild != NULL) //左结点进栈
314 St[++top] = p->lchild;
315 }
316 }
317 }
318
319 //非递归算法遍历二叉树 算法二
320 void PreOrder2(BTNode *b)
321 {
322 BTNode *St[MaxSize];int top = -1; //定义一个栈保存访问过的结点
323 BTNode *p; //p为当前处理的树的根节点指针
324
325 p = b; //p指向b
326 while(top != -1 || p != NULL) //栈不空或者p不为空
327 {
328 while(p != NULL) //p不为空,访问p结点和其所有左下结点并进栈
329 {
330 coutdata; //访问结点p
331 St[++top] = p; //p结点入栈
332 p = p->lchild; //移动到左孩子
333 }
334 if(top != -1) //处理完左下结点之后转向右节点
335 {
336 p = St[top--]->rchild; //转向栈顶结点的右孩子并且栈顶出栈( 因为栈顶结点处理完毕,不出栈会死循环)
337 }
338 }
339 }
340
341 //非递归算法中序遍历二叉树
342 void InOrder1(BTNode *b)
343 {
344 BTNode *St[MaxSize],*p;int top = -1;
345
346 p = b;
347 while(top != -1 || p != NULL)
348 {
349 while(p != NULL)
350 {
351 St[++top] = p;
352 p = p->lchild;
353 }
354 if(top != -1)
355 {
356 coutdata;
357 p = St[top--]->rchild;
358 }
359 }
360
361 }
362
363 //非递归算法后序遍历二叉树
364 void PostOrder1(BTNode *b)
365 {
366 BTNode *St[MaxSize];BTNode *p;int top = -1;
367 BTNode *r;
368 bool flag;
369
370 p = b;
371 do
372 {
373 while(p != NULL)
374 {
375 St[++top] = p;
376 p = p->lchild;
377 }
378 r = NULL;
379 flag = true;
380 while(top != -1 && flag)
381 {
382 p = St[top];
383 if(p->rchild == r)
384 {
385 coutdata;
386 r = p;
387 top--;
388 }
389 else
390 {
391 p = p->rchild;
392 flag = false;
393 }
394 }
395 }while(top != -1);
396 }
397
398 //输出从根节点到每个叶子结点的路径逆序列(非递归算法)
399 void AllPath1(BTNode *b)
400 {
401 BTNode *Path[MaxSize];BTNode *p;int top = -1;
402 BTNode *pre;
403 bool flag;
404
405 p = b;
406 do
407 {
408 while(p != NULL)
409 {
410 Path[++top] = p;
411 p = p->lchild;
412 }
413 pre = NULL;
414 flag = true;
415 while(top != -1 && flag)
416 {
417 p = Path[top];
418 if(p -> rchild == pre)
419 {
420 if(p -> lchild == NULL && p->rchild == NULL)
421 {
422 coutdata " : ";
423 for(int i = 0;i )
424 coutdata;
425 coutendl;
426 }
427 pre = p;
428 top--;
429 }
430 else
431 {
432 p = p->rchild;
433 flag = false;
434 }
435 }
436 }while(top != -1);
437 }
438
文章标题:树和二叉树相关算法(一) c/c++
文章链接:http://soscw.com/index.php/essay/56080.html