-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_tree.c
102 lines (75 loc) · 1.64 KB
/
binary_tree.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdbool.h>
typedef int TDataType;
typedef struct TBinaryTree {
TDataType data;
struct TBinaryTree *lLink;
struct TBinaryTree *rLink;
}TBinaryTree, *TPBinaryTree;
#define SIZE 10
typedef TPBinaryTree TStackElementType;
typedef struct TSeqStack {
TStackElementType stack[SIZE];
int top;
}TSeqStack;
bool Stack_isEmpty(TSeqStack *stack_);
bool Stack_isFull(TSeqStack *stack_);
void BaniryTree_middelSearch(TPBinaryTree T);
bool Stack_isEmpty(TSeqStack *stack_) {
return (stack_->top == -1)?true:false;
}
bool Stack_isFull(TSeqStack *stack_) {
return ((stack_->top + 1) >= SIZE)?true:false;
}
void BaniryTree_middelSearch(TPBinaryTree T) {
TPBinaryTree P;
TSeqStack A = {
{(TStackElementType)0},
-1
};
/* 初始化 */
P = T;
T2:
if (P == NULL) {
if(Stack_isEmpty(&A)) {
return;
}
/* 出栈 */
P = A.stack[A.top--];
/* visit NODE(P) */
printf("%d ", P->data);
P = P->rLink;
goto T2;
}
else {
if (Stack_isFull(&A)) {
return;
}
/* 入栈 */
A.stack[++A.top] = P;
P = P->lLink;
goto T2;
}
}
int main(void) {
TBinaryTree lleft = {
-2, NULL, NULL
};
TBinaryTree lright = {
0, NULL, NULL
};
TBinaryTree leftNode = {
-1, &lleft, &lright
};
TBinaryTree rightNode = {
2, NULL, NULL
};
TBinaryTree root = {
1, &leftNode, &rightNode
};
BaniryTree_middelSearch(&root);
return 0;
}