1. 題目:
用給遞歸方法遍歷二叉樹。
2. 實驗過程及結果:
得到需要輸入的二叉樹:
那麼,按先序序列輸入二叉樹的時候,需要輸入: ab#d##c#e## (注意,輸入後,按enter鍵即可)
實驗結果:
3. 源代碼:
注:遞歸方法的代碼如下,如果要改爲非遞歸的,可以查看實驗六博文:
https://blog.csdn.net/sinat_29891353/article/details/102747205
或者把#define NORECURSIVE這一行註釋掉皆可。
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 typedef struct Binnode
5 {
6 char data;
7 struct Binnode *lchild;
8 struct Binnode *rchild;
9 } Binnode, *Bintree;
10
11 typedef struct {
12 Bintree link;
13 int flag;
14 }stacktype;
15 #define MAXNODE 100
16 #define NORECURSIVE
17 void Create_Bintree(Bintree *root)
18 {
19 char ch;
20
21 if ((ch = getchar()) == '#') {
22 *root = NULL;
23 } else {
24 *root = (Binnode *)malloc(sizeof(Binnode));
25 (*root)->data = ch;
26 Create_Bintree(&(*root)->lchild);
27 Create_Bintree(&(*root)->rchild);
28 }
29 }
30
31 void Preorder(Bintree *t)
32 {
33 #ifdef NORECURSIVE
34 Bintree stack[MAXNODE], p;
35 int top;
36 if (*t == NULL)
37 return;
38 top = 0;
39 p = *t;
40 while(!(p == NULL && top == 0)){
41 while(p != NULL){
42 printf("%c", p->data); //訪問節點的數據域
43 if (top<(MAXNODE-1)){ //將當前指針p入棧
44 stack[top] = p;
45 top++;
46 } else {
47 printf("error\n");
48 return;
49 }
50 p = p->lchild; //指向p的左孩子
51 }
52 if (top <= 0)
53 return; //棧空時結束
54 else {
55 top--;
56 p = stack[top]; //從棧中彈出棧頂元素
57 p = p->rchild; //指針指向p的右孩子節點
58 }
59 }
60 #else
61 if (*t != NULL){
62 printf("%c", ((*t)->data));
63 Preorder(&(*t)->lchild);
64 Preorder(&(*t)->rchild);
65 }
66 #endif
67 }
68
69 void Inorder(Bintree *t)
70 {
71 #ifdef NORECURSIVE
72 Bintree stack[MAXNODE], p;
73 int top;
74 if (*t == NULL)
75 return;
76 top = 0;
77 p = *t;
78 while(!(p == NULL && top == 0)){
79 while(p != NULL){
80 if (top<(MAXNODE-1)){
81 stack[top] = p;
82 top++;
83 } else {
84 printf("error\n");
85 return;
86 }
87 p = p->lchild;
88 }
89 if (top <= 0){
90 return;
91 } else {
92 top--;
93 p = stack[top];
94 printf("%c", p->data);
95 p = p->rchild;
96 }
97 }
98 #else
99 if (*t != NULL){
100 Inorder(&(*t)->lchild);
101 printf("%c", (*t)->data);
102 Inorder(&(*t)->rchild);
103 }
104 #endif
105 }
106
107 void Posorder(Bintree *t)
108 {
109 #ifdef NORECURSIVE
110 stacktype stack[MAXNODE];
111 Bintree p;
112 int top, sign;
113 if (*t == NULL)
114 return;
115 top = -1; //棧頂位置初始化
116 p = *t;
117 while (!(p == NULL && top == -1)){
118 if (p != NULL){
119 top++; //節點第一次進棧
120 stack[top].link = p;
121 stack[top].flag = 1;
122 p = p->lchild; //找該節點的左節點
123 } else {
124 p = stack[top].link;
125 sign = stack[top].flag;
126 top--;
127 if (sign == 1){ //節點第二次進棧
128 top++;
129 stack[top].link = p;
130 stack[top].flag = 2; //標記第二次出棧
131 p = p->rchild;
132 } else {
133 printf("%c", p->data); //訪問該節點數據閾值
134 p = NULL;
135 }
136 }
137 }
138 #else
139 if (*t != NULL){
140 Posorder(&(*t)->lchild);
141 Posorder(&(*t)->rchild);
142 printf("%c", (*t)->data);
143 }
144 #endif
145 }
146
147 int main()
148 {
149 Bintree T;
150 printf("Tree Create Start!\n");
151 Create_Bintree(&T);
152 printf("Tree Create OK!\n");
153 printf("先序遍歷:\n");
154 Preorder(&T);
155 printf("\n中序遍歷:\n");
156 Inorder(&T);
157 printf("\n後序遍歷:\n");
158 Posorder(&T);
159 printf("\n");
160 return 0;
161 }