-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreeTraversing.c
156 lines (144 loc) · 3.63 KB
/
BinaryTreeTraversing.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* 首先定义简单的二叉树的结点类型
* 程序中使用的指向结点类型的指针类型形式参数,其实也可以直接生命成BTree类型
*/
#define MaxSize 100
typedef char ElemType;
typedef struct node
{
ElemType data;
struct node *lchild;
struct node *rchild;
}BTNode, *BTree;
/**
* 以下将递归非递归放在一起
*/
void PreOrder(BTNode *b){
if(b != NULL){
printf("%c", b->data);
PreOrder(b->lchild);
PreOrder(b->rchild);
}
}
//非递归,借助一个栈,每次都是栈顶先出栈,即栈顶是最先访问的数据
//先序遍历的思想是先根,再左孩子,再右孩子
//此处及以后栈简单地使用数组表示
void IPreOrder(BTNode *b){
BTNode *St[MaxSize], *p;
int top = -1;
if(b != NULL){
top++;
St[top] = b;
while (top > -1)
{
p = St[top];
top--;
printf("%c", p->data);
if(p->rchild) St[top++] = p->rchild;
if(p->lchild) St[top++] = p->lchild;
}
}
printf("\n");
}
void InOrder(BTNode *b){
if(b != NULL){
InOrder(b->lchild);
printf("%c", b->data);
InOrder(b->rchild);
}
}
void IInOrder(BTNode *b){
BTNode *St[MaxSize], *p;
int top = -1;
if (b != NULL)
{
p = b;
while (top > -1 || p != NULL)
{
while (p != NULL)
{
top++;
St[top] = p;
p = p->lchild;
}
if(top > -1){
p = St[top];
top--;
printf("%c", p->data);
p = p->rchild;
}
}
printf("\n");
}
}
void PostOrder(BTNode *b){
if(b != NULL){
PostOrder(b->lchild);
PostOrder(b->rchild);
printf("%c", b->data);
}
}
void IPostOrder(BTNode *b){
BTNode *St[MaxSize], *p;
int top = -1;
int flag;
if (b != NULL)
{
do
{
while (b != NULL) // send all left child into stack
{
top++;
St[top] = b;
b = b->lchild;
}
p = NULL; // a pointer to the previous visited node of current node
flag = 1; // note the node of b has been visited
while (top != -1 && flag)
{
b = St[top]; // get the current node
if (b->rchild == p) // if right child is not existed or has been visited, then visit the current node
{
printf("%c", b->data);
top--;
p = b;
}else{
b = b->rchild; // b point to the right child
flag = 0; //set not visited
}
}
} while (top != -1);
}
}
/**
* 层次遍历
*/
void TraverseLevel(BTNode *b){
BTNode *Qu[MaxSize];
int front, rear;
front = rear = 0;
if (b != NULL)
{
printf("%c", b->data);
}
rear++;
Q[rear] = b;
while (rear != front)
{
front = (front + 1) % MaxSize;
b = Qu[front];
if (b->lchild != NULL)
{
printf("%c", b->lchild->data);
rear = (rear + 1) % MaxSize;
Qu[rear] = b->lchild;
}
if (b->rchild != NULL)
{
printf("%c", b->rchild->data);
rear = (rear + 1) % MaxSize;
Qu[rear] = b->rchild;
}
}
printf("\n");
}