-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadedBiTree.c
98 lines (91 loc) · 2.12 KB
/
ThreadedBiTree.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
#define MAX_NODE 50
typedef enum{
Link,//表示指针
Thread//表示线索
}PointerTag;
typedef int ElemType;
typedef struct BiThrNode
{
ElemType data;
struct BiThrNode *Lchild, *Rchild;
PointerTag Ltag, Rtag;
}BiThrNode;
void preorder_Threading(BiThrNode *T){
BiThrNode *stack[MAX_NODE];
BiThrNode *last = NULL, *p;
int top = 0;
if (T != NULL)
{
stack[++top] = T;
while (top > 0)
{
p = stack[top--];
if (p->Lchild != NULL)
{
p->Ltag = 0;
}else
{
p->Ltag = 1;
p->Lchild = last;
}
if (last != NULL)
{
if (last->Rchild != NULL)
{
last->Rtag = 0;
}else
{
last->Rtag = 1;
last->Rchild = p;
}
}
last = p;
if (p->Rchild != NULL)
{
stack[++top] = p->Rchild;
}
if (p->Lchild != NULL)
{
stack[++top] = p->Lchild;
}
}
}
}
void inorder_Threading(BiThrNode *T){
BiThrNode *stack[MAX_NODE];
BiThrNode *last = NULL, *p;
int top = 0;
while (p != NULL || top > 0)
{
if (p != NULL)
{
stack[++top] = p;
p = p->Lchild;
}else
{
p = stack[top--];
if (p->Lchild != NULL)
{
p->Ltag = 0;
}else
{
p->Ltag = 1;
p->Lchild = last;
}
if (last != NULL)
{
if (p->Rchild != NULL)
{
p->Rtag = 0;
}else
{
p->Rtag = 1;
p->Rchild = last;
}
}
last = p;
p = p->Rchild;
}
last->Rtag = 1;
}
}