-
Notifications
You must be signed in to change notification settings - Fork 0
/
main13.c
90 lines (77 loc) · 1.7 KB
/
main13.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
//
// Created by ge on 2023/12/13.
//
#include <stdlib.h>
#include <stdio.h>
// 后序 非递归
typedef struct TreeNode {
char data;
struct TreeNode* lchild;
struct TreeNode* rchild;
int ltag;
int rtag;
}TreeNode;
typedef struct StackNode {
TreeNode *data;
struct StackNode *next;
}StackNode;
void createTree(TreeNode** T, char * data, int *index){
char ch;
ch = data[*index];
*index +=1;
if (ch == '#') {
*T = NULL;
}else {
*T = (TreeNode *) malloc(sizeof(TreeNode));
(*T)->data = ch;
(*T)->rtag = ch;
(*T)->ltag = ch;
createTree(&((*T)->lchild), data, index);
createTree(&((*T)->rchild), data, index);
}
}
void inThreadTree(TreeNode* T, TreeNode** pre){
if (T) {
inThreadTree(T->lchild, pre);
// do something
if (T->lchild == NULL){
T->ltag = 1;
T->lchild = *pre;
}
if (*pre != NULL && (*pre)->rchild == NULL){
(*pre)->rtag = 1;
(*pre)->rchild = T;
}
*pre = T;
inThreadTree(T->rchild,pre);
}
}
TreeNode *getFirst(TreeNode *T){
while (T->ltag == 0){
T = T->lchild;
}
return T;
}
TreeNode *getNext(TreeNode *T){
if (T->rtag == 1){
return T->rchild;
}else {
return getFirst(T->rchild);
}
}
StackNode *initStack() {
StackNode *S = (StackNode*) malloc(sizeof(StackNode));
S->data = NULL;
S->next = NULL;
return S;
}
int main(int argc, char* argv[]) {
TreeNode *T;
TreeNode *pre = NULL;
int index = 0;
createTree(&T,argv[1],&index);
inThreadTree(T,&pre);
pre->rtag = 1;
pre->rchild = NULL;
return 0;
}