二叉树的非递归遍历(定长顺序栈)
二叉树的抽象数据类型
1 typedef struct Node 2 {/*二叉树*/ 3 char data; 4 struct Node* Lchild; 5 struct Node* Rchild; 6 }BTNode;
先序遍历
1 void PreOrder(BTNode* A) 2 {/*先序非递归遍历*/ 3 SeqStack* B = InitStack(); 4 while (A != NULL || !IsEmptyStack(B)) 5 {/*当前结点指针及栈均空,则结束*/ 6 if(A != NULL) 7 {/*访问根节点,根指针进栈,进入左子树*/ 8 printf("%c", A->data); 9 PushStack(B, A); 10 A = A->Lchild; 11 } 12 else 13 {/*根指针退栈,进入其右子树*/ 14 PopStack(B, &A); 15 A = A->Rchild; 16 } 17 } 18 }
中序遍历
1 void InOrder(BTNode* A) 2 {/*中序非递归遍历*/ 3 SeqStack* B = InitStack(); 4 while (A != NULL || !IsEmptyStack(B)) 5 { 6 if (A != NULL) 7 { 8 PushStack(B, A); 9 A = A->Lchild; 10 } 11 else 12 { 13 PopStack(B, &A); 14 printf("%c", A->data); 15 A = A->Rchild; 16 } 17 } 18 }
后序遍历
1 void PostOrder(BTNode* A) 2 { 3 SeqStack* B = InitStack(); 4 BTNode* P = NULL; 5 while (A != NULL || !IsEmptyStack(B)) 6 { 7 if (A != NULL) 8 { 9 PushStack(B, A); 10 A = A->Lchild; 11 } 12 else 13 { 14 TopStack(B, &A); 15 if (A->Rchild == NULL || A->Rchild == P) 16 {/*判断栈顶结点的右子树是否为空,右子树是否刚访问过*/ 17 PopStack(B, &A); 18 printf("%c", A->data); 19 P = A; 20 A = NULL; 21 } 22 else 23 { 24 A = A->Rchild; 25 } 26 } 27 } 28 }
源代码

1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #define MAXSIZE 100 5 6 typedef struct Node 7 {/*二叉树*/ 8 char data; 9 struct Node* Lchild; 10 struct Node* Rchild; 11 }BTNode; 12 13 typedef struct 14 {/*定长顺序栈*/ 15 BTNode* elem[MAXSIZE]; 16 int top; 17 }SeqStack; 18 19 SeqStack* InitStack(); 20 int IsEmptyStack(SeqStack* A); 21 int PushStack(SeqStack* A, BTNode* B); 22 int PopStack(SeqStack* A, BTNode** B); 23 int TopStack(SeqStack* A, BTNode** B); 24 25 BTNode* CreateBinaryTree(); 26 void PreOrder(BTNode* A); 27 void InOrder(BTNode* A); 28 void PostOrder(BTNode* A); 29 30 int main(void) 31 { 32 BTNode* A; 33 34 printf("请输入该二叉树先序遍历序列(用^表示空子树,叶子结点要跟两个^):\n"); 35 A = CreateBinaryTree(); 36 37 printf("先序非递归遍历:"); 38 PreOrder(A); 39 40 printf("\n中序非递归遍历:"); 41 InOrder(A); 42 43 printf("\n后序非递归遍历:"); 44 PostOrder(A); 45 46 system("pause"); 47 return 0; 48 } 49 50 SeqStack* InitStack() 51 { 52 SeqStack* A; 53 A = (SeqStack*)calloc(1, sizeof(SeqStack)); 54 A->top = -1; 55 return A; 56 } 57 58 int IsEmptyStack(SeqStack* A) 59 { 60 if (A->top == -1) 61 {/*空栈*/ 62 return 1; 63 } 64 else 65 { 66 return 0; 67 } 68 } 69 70 int PushStack(SeqStack* A, BTNode* B) 71 { 72 if (A->top == MAXSIZE - 1) 73 { 74 return 1; 75 } 76 else 77 { 78 A->top++; 79 A->elem[A->top] = B; 80 /*printf("\n结点%c进栈了\n", A->elem[A->top]->data);*/ 81 return 0; 82 } 83 } 84 85 86 int PopStack(SeqStack* A , BTNode** B) 87 { 88 if (IsEmptyStack(A)) 89 { 90 return 1; 91 } 92 else 93 { 94 (* B) = A->elem[A->top]; 95 /*printf("\n结点%c出栈了\n", (*B)->data);*/ 96 A->top--; 97 return 0; 98 } 99 } 100 101 int TopStack(SeqStack* A, BTNode** B) 102 { 103 if (IsEmptyStack(A)) 104 { 105 return 1; 106 } 107 else 108 { 109 (*B) = A->elem[A->top]; 110 return 0; 111 } 112 } 113 114 BTNode* CreateBinaryTree() 115 { 116 BTNode* A = NULL; 117 char ch = '\0'; 118 119 ch = getchar(); 120 if (ch == '^') 121 { 122 return NULL; 123 } 124 else 125 { 126 A = (BTNode*)calloc(1, sizeof(BTNode)); 127 A->data = ch; 128 A->Lchild = CreateBinaryTree(A->Lchild); 129 A->Rchild = CreateBinaryTree(A->Rchild); 130 return A; 131 } 132 } 133 134 void PreOrder(BTNode* A) 135 {/*先序非递归遍历*/ 136 SeqStack* B = InitStack(); 137 while (A != NULL || !IsEmptyStack(B)) 138 {/*当前结点指针及栈均空,则结束*/ 139 if(A != NULL) 140 {/*访问根节点,根指针进栈,进入左子树*/ 141 printf("%c", A->data); 142 PushStack(B, A); 143 A = A->Lchild; 144 } 145 else 146 {/*根指针退栈,进入其右子树*/ 147 PopStack(B, &A); 148 A = A->Rchild; 149 } 150 } 151 } 152 153 void InOrder(BTNode* A) 154 {/*中序非递归遍历*/ 155 SeqStack* B = InitStack(); 156 while (A != NULL || !IsEmptyStack(B)) 157 { 158 if (A != NULL) 159 { 160 PushStack(B, A); 161 A = A->Lchild; 162 } 163 else 164 { 165 PopStack(B, &A); 166 printf("%c", A->data); 167 A = A->Rchild; 168 } 169 } 170 } 171 172 void PostOrder(BTNode* A) 173 { 174 SeqStack* B = InitStack(); 175 BTNode* P = NULL; 176 while (A != NULL || !IsEmptyStack(B)) 177 { 178 if (A != NULL) 179 { 180 PushStack(B, A); 181 A = A->Lchild; 182 } 183 else 184 { 185 TopStack(B, &A); 186 if (A->Rchild == NULL || A->Rchild == P) 187 {/*判断栈顶结点的右子树是否为空,右子树是否刚访问过*/ 188 PopStack(B, &A); 189 printf("%c", A->data); 190 P = A; 191 A = NULL; 192 } 193 else 194 { 195 A = A->Rchild; 196 } 197 } 198 } 199 }