二叉树的遍历方法

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #define N 100
  5 
  6 typedef char datatype;
  7 void visit(datatype data);
  8 
  9 typedef struct bnode
 10 {
 11     datatype data;
 12     struct bnode *lchild, *rchild;
 13 } BTNode, *BTree, **B_BTree;
 14 
 15 typedef struct stack
 16 {
 17     BTree node[N];
 18     //int tag[N];
 19     int top;
 20 } Stack;
 21 
 22 void push(Stack *s, BTree T)
 23 {
 24     s->node[++s->top] = T;
 25 }
 26 
 27 BTree get_top(Stack *s)
 28 {
 29     return s->node[s->top];
 30 }
 31 
 32 BTree pop(Stack *s)
 33 {
 34     if(s->top != -1)
 35     {
 36         s->top--;
 37         return s->node[s->top + 1];
 38     }
 39     else
 40     {
 41         return NULL;
 42     }
 43 }
 44 
 45 void visit(datatype data)
 46 {
 47     printf("%c", data);
 48 }
 49 
 50 void preorder(BTree T)
 51 {
 52     if(T)
 53     {
 54         visit(T->data);
 55         preorder(T->lchild);
 56         preorder(T->rchild);
 57     }
 58 }
 59 
 60 void inorder(BTree T)
 61 {
 62     if(T)
 63     {
 64         inorder(T->lchild);
 65         visit(T->data);
 66         inorder(T->rchild);
 67     }
 68 }
 69 
 70 void postorder(BTree T)
 71 {
 72     if(T)
 73     {
 74         postorder(T->lchild);
 75         postorder(T->rchild);
 76         visit(T->data);
 77     }
 78 }
 79 
 80 void cur_preorder(BTree t)
 81 {
 82     Stack s;
 83     BTree temp = t;
 84     s.top = -1;
 85     while(temp || s.top != -1)
 86     {
 87         while(temp)
 88         {
 89             visit(temp->data);
 90             push(&s, temp);
 91             temp = temp->lchild;
 92         }
 93         if(s.top > -1)
 94         {
 95             temp = pop(&s);
 96             temp = temp->rchild;
 97         }
 98     }
 99 }
100 
101 void cur_inorder(BTree t)
102 {
103     Stack s;
104     BTree temp = t;
105     s.top = -1;
106     while(temp || s.top != -1)
107     {
108         while(temp)
109         {
110             push(&s, temp);
111             temp = temp->lchild;
112         }
113         if(s.top > -1)
114         {
115             temp = pop(&s);
116             visit(temp->data);
117             temp = temp->rchild;
118         }
119     }
120 }
121 
122 void cur_postorder(BTree t)
123 {
124     Stack s;
125     BTree temp, pre = NULL;
126     temp = t;
127     s.top = -1;
128     while(temp || s.top != -1)
129     {
130         while(temp)
131         {
132             push(&s, temp);
133             temp = temp->lchild;
134         }
135         temp = get_top(&s);
136         if(temp->rchild == NULL
137            || temp->rchild == pre)
138            {
139                visit(temp->data);
140                pre = temp;
141                temp = NULL;
142                pop(&s);
143            }
144            else
145            {
146                temp = temp->rchild;
147            }
148     }
149 }
150 
151 /*层次遍历*/
152 typedef struct
153 {
154     BTree node[N];
155     int front;
156     int rear;
157 }   queue;
158 
159 void init_queue(queue *Q)
160 {
161     Q->front = Q->rear = 0;
162 }
163 
164 void en_queue(queue *Q, BTree e)
165 {
166     Q->node[Q->rear] = e;
167     Q->rear = (Q->rear + 1) % N;
168 }
169 
170 void de_queue(queue *Q, B_BTree e)
171 {
172     *e = Q->node[Q->front];
173     Q->front = (Q->front + 1) % N;
174 }
175 
176 int is_empty(queue *Q)
177 {
178     return Q->rear == Q->front;
179 }
180 
181 void level_order(BTree t)
182 {
183     queue Q;
184     init_queue(&Q);
185     BTree p = t;
186     if(p)
187     {
188         en_queue(&Q, p);
189         while(!is_empty(&Q))
190         {
191             de_queue(&Q, &p);
192             visit(p->data);
193             if(p->lchild)
194             {
195                 en_queue(&Q, p->lchild);
196             }
197             if(p->rchild)
198             {
199                 en_queue(&Q, p->rchild);
200             }
201         }
202     }
203 }
204 
205 void create_tree(B_BTree T)
206 {
207     char temp;
208     temp = getchar();
209     if(temp == ' ')
210     {
211         *T = NULL;
212     }
213     else
214     {
215         (*T) = (BTNode *)malloc(sizeof(BTNode));
216         (*T)->data = temp;
217         create_tree(&(*T)->lchild);
218         create_tree(&(*T)->rchild);
219     }
220 }
221 
222 int main()
223 {
224     BTree T;
225     printf("请按次数顺序输入字符:\n");
226     create_tree(&T);
227     preorder(T);
228     printf("\n");
229     cur_preorder(T);
230     printf("\n");
231     inorder(T);
232     printf("\n");
233     cur_inorder(T);
234     printf("\n");
235     postorder(T);
236     printf("\n");
237     cur_postorder(T);
238     printf("\n");
239     level_order(T);
240     printf("\n");
241     return 0;
242 }
posted @ 2012-08-31 10:03  山路水桥  阅读(172)  评论(0编辑  收藏  举报