二叉树的节点结构为:
1 struct NODE
2 {
3 char ch;
4
5 struct NODE *lChild;
6 struct NODE *rChild;
7 };
按照前序的顺序递归建立一颗二叉树:
1 void PreOrderCreatBinary(struct NODE **root)
2 {
3 // 函数入口检验
4 if (NULL == root)
5 {
6 printf ("Invalid Parameter(s)!\n");
7 return;
8 }
9
10 char ch;
11
12 printf ("Input NODE value, '#' equal NULL:");
13 scanf ("%c", &ch);
14
15 // 去掉输入结束按下的回车符
16 getchar();
17
18 if ('#' == ch)
19 {
20 *root = NULL;
21 return;
22 }
23
24 if (NULL == (*root = (struct NODE *)malloc(sizeof(struct NODE))))
25 {
26 printf ("Fail to malloc space for *root!\n");
27 return;
28 }
29
30 (*root)->ch = ch;
31
32 // 建立其左子树
33 printf ("Create %c's left child.\n", (*root)->ch);
34 PreOrderCreatBinary (&((*root)->lChild));
35
36 // 建立其右子树
37 printf ("Create %c's right child.\n", (*root)->ch);
38 PreOrderCreatBinary (&((*root)->rChild));
39 }
前序递归遍历二叉树:
1 void RecursionPreOrder(struct NODE *root)
2 {
3 if (NULL != root)
4 {
5 // 访问根节点
6 printf ("%c ", root->ch);
7
8 // 访问左子树
9 RecursionPreOrder (root->lChild);
10
11 // 访问右子树
12 RecursionPreOrder (root->rChild);
13 }
14 }
第一种非递归前序遍历二叉树的方式:
1 void NonRecursivePreOrder(struct NODE *root)
2 {
3 if (NULL == root)
4 {
5 printf ("Empty Binary Tree!\n");
6 return;
7 }
8
9 struct NODE *Stack[MAX];
10 int nTop = -1;
11 struct NODE *Temp = NULL;
12
13 // 根节点首先入栈
14 Stack[++nTop] = root;
15 while (nTop >= 0)
16 {
17 // 取出栈顶节点并访问
18 Temp = Stack[nTop--];
19 printf ("%c ", Temp->ch);
20
21 // 左子树要先于右子树被访问,则右子树先入栈
22 if (NULL != (Temp->rChild))
23 {
24 Stack[++nTop] = Temp->rChild;
25 }
26
27 // 然后左子树入栈
28 if (NULL != (Temp->lChild))
29 {
30 Stack[++nTop] = Temp->lChild;
31 }
32 }
33 }
第二种非递归前序遍历二叉树的方式:
1 void NonRecursivePreOrder2(struct NODE *root)
2 {
3 if (NULL == root)
4 {
5 printf ("Empty Binary Tree!\n");
6 return;
7 }
8
9 struct NODE *Stack[MAX];
10 int nTop = -1;
11 struct NODE *Temp = root;
12
13 // 访问当前节点及其全部左子树
14 while (NULL != Temp)
15 {
16 printf ("%c ", Temp->ch);
17
18 Stack[++nTop] = Temp;
19 Temp = Temp->lChild;
20 }
21
22 while (nTop >= 0)
23 {
24 // 取得栈顶元素的右子树
25 Temp = Stack[nTop]->rChild;
26 --nTop;
27
28 // 同样访问当前节点及其全部左子树
29 while (NULL != Temp)
30 {
31 printf ("%c ", Temp->ch);
32
33 Stack[++nTop] = Temp;
34 Temp = Temp->lChild;
35 }
36 }
37 }
中序递归遍历二叉树:
1 void RecursionInOrder(struct NODE *root)
2 {
3 if (NULL != root)
4 {
5 // 访问左子树
6 RecursionInOrder (root->lChild);
7
8 // 访问根节点
9 printf ("%c ", root->ch);
10
11 // 访问右子树
12 RecursionInOrder (root->rChild);
13 }
14 }
中序非递归遍历二叉树:
1 void NonRecursionInOrder(struct NODE *root)
2 {
3 if (NULL == root)
4 {
5 printf ("Empty Binary Tree!\n");
6 return;
7 }
8
9 struct NODE *Stack[MAX];
10 int nTop = -1;
11 struct NODE *curr = root->lChild;
12
13 Stack[++nTop] = root;
14
15 while ((NULL != curr) || (nTop >= 0))
16 {
17 // 根节点及其左子树全部入栈
18 while (NULL != curr)
19 {
20 Stack[++nTop] = curr;
21 curr = curr->lChild;
22 }
23
24 // 访问栈顶节点
25 curr = Stack[nTop--];
26 printf ("%c ", curr->ch);
27
28 curr = curr->rChild;
29 }
30 }
后序递归遍历二叉树:
1 void RecursionPostOrder(struct NODE *root)
2 {
3 if (NULL != root)
4 {
5 // 访问左子树
6 RecursionPostOrder (root->lChild);
7
8 // 访问右子树
9 RecursionPostOrder (root->rChild);
10
11 // 访问根节点
12 printf ("%c ", root->ch);
13 }
14 }
第一种后序非递归遍历二叉树的方式:
1 void NonRecursionPostOrder(struct NODE *root)
2 {
3 if (NULL == root)
4 {
5 printf ("Empty Binary Tree!\n");
6 return;
7 }
8
9 struct NODE *Stack[MAX];
10 int nTop = -1;
11 struct NODE *curr = root;
12 struct NODE *Previewed = NULL;
13
14 while ((NULL != curr) || (nTop >= 0))
15 {
16 while (NULL != curr)
17 {
18 Stack[++nTop] = curr;
19 curr = curr->lChild;
20 }
21
22 // 获取当前栈顶节点
23 curr = Stack[nTop];
24
25 // 判断当前节点的右子树状态然后做出相应的操作
26 if ((NULL == (curr->rChild)) || (Previewed == (curr->rChild)))
27 {
28 printf ("%c ", curr->ch);
29 Previewed = curr;
30 curr = NULL;
31 --nTop;
32 }
33 else
34 {
35 curr = curr->rChild;
36 }
37 }
38 }
第二种后序非递归遍历二叉树的方式:
1 void NonRecursionPostOrder2(struct NODE *root)
2 {
3 if (NULL == root)
4 {
5 printf ("Empty Binary Tree!\n");
6 return;
7 }
8
9 struct NODE *Stack1[MAX];
10 int nTop1 = -1;
11 struct NODE *Stack2[MAX];
12 int nTop2 = -1;
13 struct NODE *Temp = NULL;
14
15 // 根节点先入栈1
16 Stack1[++nTop1] = root;
17 while (nTop1 >= 0)
18 {
19 Temp = Stack1[nTop1--];
20 Stack2[++nTop2] = Temp;
21
22 // 左子树要先在栈2中被访问,则左子树需先入栈1
23 if (NULL != (Temp->lChild))
24 {
25 Stack1[++nTop1] = Temp->lChild;
26 }
27
28 if (NULL != (Temp->rChild))
29 {
30 Stack1[++nTop1] = Temp->rChild;
31 }
32 }
33
34 // 按2栈出栈顺序打印即是后序顺序
35 while (nTop2 >= 0)
36 {
37 Temp = Stack2[nTop2--];
38 printf ("%c ", Temp->ch);
39 }
40 }
测试主函数:
1 #include <stdio.h>
2 #include <malloc.h>
3 #define MAX 100
4
5 int main(void)
6 {
7 struct NODE *root = NULL;
8
9 // 前序建立二叉树
10 PreOrderCreatBinary (&root);
11
12 printf ("\n建立的二叉树的前序递归遍历序列为:\n");
13 RecursionPreOrder (root);
14 printf ("\n");
15
16 printf ("\n建立的二叉树的前序第一种非递归方式遍历序列为:\n");
17 NonRecursivePreOrder (root);
18 printf ("\n");
19
20 printf ("\n建立的二叉树的前序第二种非递归方式遍历序列为:\n");
21 NonRecursivePreOrder2 (root);
22 printf ("\n");
23
24 printf ("\n建立的二叉树的中序递归遍历序列为:\n");
25 RecursionInOrder (root);
26 printf ("\n");
27
28 printf ("\n建立的二叉树的中序非递归遍历序列为:\n");
29 NonRecursionInOrder (root);
30 printf ("\n");
31
32 printf ("\n建立的二叉树的后序递归遍历序列为:\n");
33 RecursionPostOrder (root);
34 printf ("\n");
35
36 printf ("\n建立的二叉树的后序第一种非递归方式遍历序列为:\n");
37 NonRecursionPostOrder (root);
38 printf ("\n");
39
40 printf ("\n建立的二叉树的后序第二种非递归方式遍历序列为:\n");
41 NonRecursionPostOrder2 (root);
42 printf ("\n");
43
44 return(0);
45 }
结果: