FORWARD!  
  1 #include <stdlib.h>
  2 #include <stdio.h>
  3 #include <malloc.h>
  4 #include <assert.h>
  5 
  6 #define INIT_STACK_SIZE 100
  7 #define ADD_SIZE 10
  8 
  9 typedef char DataType;
 10 
 11 typedef struct Node {
 12     DataType data;/*数据域*/
 13     struct Node *lchild;/*左子树指针*/
 14     struct Node *rchild;/*右子树指针*/
 15 } BiTNode, *BiTree; /*结点的结构体定义*/
 16 
 17 typedef BiTNode ElemType;
 18 typedef struct {
 19     BiTree *top;
 20     BiTree *base;
 21     int stacksize;
 22 } TStack;
 23 
 24 void InitStack(TStack *TS) {
 25     TS->base = (BiTree *)malloc(INIT_STACK_SIZE * sizeof(BiTree));
 26 
 27     if(!TS->base) {
 28         printf("Apply for memory error,new node cannot be created!\n");
 29         exit(EXIT_FAILURE);
 30     }
 31     TS->top = TS->base;
 32     TS->stacksize = INIT_STACK_SIZE;
 33 }
 34 void Push(TStack *TS, BiTNode *e) {
 35     if(TS->top - TS->base >= TS->stacksize) {
 36         //如果栈空间已满,重新分配空间
 37         TS->base = (BiTree *)realloc(TS->base, (TS->stacksize + ADD_SIZE) * sizeof(BiTree));
 38         if(!TS->base) {
 39             printf("Apply for memory failed!\n");
 40             exit(EXIT_FAILURE);
 41         }
 42 
 43         TS->top = TS->base + TS->stacksize;
 44         TS->stacksize += ADD_SIZE;
 45     }
 46 
 47     *TS->top++ = e;
 48 }
 49 void Pop(TStack *TS, BiTNode **e) {
 50     if(TS->base != TS->top)
 51         *e = *--TS->top;
 52 }
 53 int GetTop(TStack TS, BiTNode *e) {
 54     if(TS.base == TS.top)
 55         return -1;
 56 
 57     e = *(TS.top - 1);
 58     return 1;
 59 }
 60 int StackEmpty(TStack TS) {
 61     return TS.base == TS.top;
 62 }
 63 
 64 /*初始化创建二叉树的头结点*/
 65 void Initiate(BiTNode **root) {
 66     *root = (BiTNode *)malloc(sizeof(BiTNode));
 67     (*root)->lchild = NULL;
 68     (*root)->rchild = NULL;
 69 }
 70 
 71 void Destroy(BiTNode **root) {
 72     if((*root) != NULL && (*root)->lchild != NULL)
 73         Destroy(&(*root)->lchild);
 74 
 75     if((*root) != NULL && (*root)->rchild != NULL)
 76         Destroy(&(*root)->rchild);
 77 
 78     free(*root);
 79 }
 80 
 81 /*若当前结点curr非空,在curr的左子树插入元素值为x的新结点*/
 82 /*原curr所指结点的左子树成为新插入结点的左子树*/
 83 /*若插入成功返回新插入结点的指针,否则返回空指针*/
 84 BiTNode *InsertLeftNode(BiTNode *curr, DataType x) {
 85     BiTNode *s, *t;
 86     if(curr == NULL)
 87         return NULL;
 88 
 89     t = curr->lchild;/*保存原curr所指结点的左子树指针*/
 90     s = (BiTNode *)malloc(sizeof(BiTNode));
 91     s->data = x;
 92     s->lchild = t;/*新插入结点的左子树为原curr的左子树*/
 93     s->rchild = NULL;
 94 
 95     curr->lchild = s;/*新结点成为curr的左子树*/
 96     return curr->lchild;/*返回新插入结点的指针*/
 97 }
 98 
 99 /*若当前结点curr非空,在curr的右子树插入元素值为x的新结点*/
100 /*原curr所指结点的右子树成为新插入结点的右子树*/
101 /*若插入成功返回新插入结点的指针,否则返回空指针*/
102 BiTNode *InsertRightNode(BiTNode *curr, DataType x) {
103     BiTNode *s, *t;
104 
105     if(curr == NULL)
106         return NULL;
107 
108     t = curr->rchild;/*保存原curr所指结点的右子树指针*/
109     s = (BiTNode *)malloc(sizeof(BiTNode));
110     s->data = x;
111     s->rchild = t;/*新插入结点的右子树为原curr的右子树*/
112     s->lchild = NULL;
113 
114     curr->rchild = s;/*新结点成为curr的右子树*/
115     return curr->rchild;/*返回新插入结点的指针*/
116 }
117 
118 
119 
120 void PreOrder(BiTNode *t, int visit(DataType item))
121 //使用visit(item)函数前序遍历二叉树t
122 {
123     if(t != NULL) {
124         //此小段有一处错误
125         visit(t->data);
126         PreOrder(t-> lchild, visit);
127         PreOrder(t-> rchild, visit);
128     }
129 }
130 
131 void InOrder(BiTNode *t, int visit(DataType item))
132 //使用visit(item)函数中序遍历二叉树t
133 {
134     if(t != NULL) {
135         //此小段有一处错误
136         InOrder(t->lchild, visit);
137         visit(t->data);
138         InOrder(t->rchild, visit);
139     }
140 }
141 
142 void PostOrder(BiTNode *t, int visit(DataType item))
143 //使用visit(item)函数后序遍历二叉树t
144 {
145     if(t != NULL) {
146         //此小段有一处错误
147         PostOrder(t->lchild, visit);
148         PostOrder(t->rchild, visit);
149         visit(t->data);
150     }
151 }
152 ////////////////////////////////////////////////////////
153 int MyInOrder(BiTNode *T, int visit(DataType item)) {
154     TStack TS;
155 
156     BiTNode *temp = T;
157     InitStack(&TS);
158     while(!StackEmpty(TS) || temp) {
159 //           //中序遍历
160 //        if(temp){
161 //            Push(&TS, temp);
162 //            temp = temp->lchild;
163 //        }else{
164 //            Pop(&TS, &temp);
165 //            if(!visit(temp->data))
166 //                return -1;
167 //            temp = temp->rchild;
168 //        }
169 //前序遍历,当所有的节点遍历完的时候,栈中剩下的全为根节点
170         if(temp) {
171             if(!visit(temp->data)){
172                 return -1;
173             }else{
174                 Push(&TS, temp);
175                 temp = temp->lchild;
176             }
177         }
178         else{
179             Pop(&TS, &temp);
180             temp = temp->rchild;
181         }
182     }
183 
184     return 1;
185 }
186 
187 int Visit(DataType item) {
188     printf("%c ", item);
189     return 1;
190 }
191 
192 BiTNode *Search(BiTNode *root, DataType x) { //需找元素x是否在二叉树中
193     BiTNode *find = NULL;
194     if(root != NULL) {
195         if(root->data == x)
196             find = root;
197         else {
198             find = Search(root->lchild, x);
199             if(find == NULL)
200                 find = Search(root->rchild, x);
201         }
202     }
203     return find;
204 }
205 
206 int main() {
207     BiTNode *root, *p, *pp, *find;
208     char x = 'E';
209 
210     Initiate(&root);
211     p = InsertLeftNode(root, 'A');
212     p = InsertLeftNode(p, 'B');
213     p = InsertLeftNode(p, 'D');
214     p = InsertRightNode(p, 'G');
215     p = InsertRightNode(root->lchild, 'C');
216     pp = p;
217     InsertLeftNode(p, 'E');
218     InsertRightNode(pp, 'F');
219 
220     printf("前序遍历:");
221     PreOrder(root->lchild, Visit);
222     printf("\n中序遍历:");
223     InOrder(root->lchild, Visit);
224     printf("\n后序遍历:");
225     PostOrder(root->lchild, Visit);
226 
227     printf("\n\n现在开始非递归遍历!\n");
228     MyInOrder(root->lchild, Visit);
229 
230     find = Search(root, x);
231     if(find != NULL)
232         printf("\n数据元素%c在二叉树中 \n", x);
233     else
234         printf("\n数据元素%c不在二叉树中 \n", x);
235 
236     Destroy(&root);
237 
238     return 0;
239 }

 

posted on 2018-12-05 22:34  psland  阅读(683)  评论(0编辑  收藏  举报