抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

代码实现广义表还原二叉树 (栈原理)

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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*************************************************************************
> File Name: 广义表转二叉树.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年01月18日 星期二 14时33分25 秒
************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
typedef struct Node {
char data;
struct Node *lchild, *rchild;
} Node;

typedef struct Tree {
Node *root;
int length;
} Tree;

typedef struct Stack {
Node **data;
int top, size;
} Stack;

Node *getNewNode(char val) {
Node *p = (Node *)malloc(sizeof(Node));
p->data = val;
p->lchild = p->rchild = NULL;
return p;
}

Tree *getNewTree() {
Tree *tree = (Tree *)malloc(sizeof(Tree));
tree->root = NULL;
tree->length = 0;
return tree;
}

Stack *init_stack(int n) {
Stack *s = (Stack *)malloc(sizeof(Stack));
s->data = (Node **)malloc(sizeof(Node *) * n);
s->top = -1;
s->size = n;
return s;
}

Node *top(Stack *s) {
return s->data[s->top];
}

int empty(Stack *s) {
return s->top == -1;
}

int push(Stack *s, Node *val) {
if(s == NULL) return 0;
if(s->size - 1 == s->top) return 0;
s->data[++(s->top)] = val;
return 1;
}

int pop(Stack *s) {
if(s == NULL) return 0;
if(empty(s)) return 0;
s->top--;
return 1;
}

void clear_stack(Stack *s) {
if(s == NULL) return ;
free(s->data);
free(s);
return ;
}
void clear_node(Node *root) {
if(root == NULL) return ;
clear_node(root->lchild);
clear_node(root->rchild);
free(root);
return ;
}

void clear_tree(Tree *tree) {
if(tree == NULL) return ;
clear_node(tree->root);
free(tree);
return ;
}

Node *build(const char *str, int *node_num) {
Stack *s = init_stack(strlen(str));
int falg = 0;
Node *temp = NULL, *p =NULL;
while(str[0]) {
switch(str[0]) {
case '(' : {
push(s, temp);
falg = 0;
} break;
case ')' : {
p = top(s);
pop(s);
} break;
case ',' : falg = 1; break;
case ' ' : break;
default :
temp = getNewNode(str[0]);
if(!empty(s) && !falg) {
top(s)->lchild = temp;
} else if(!empty(s) && falg){
top(s)->rchild = temp;
}
++(*node_num);
}
++str;
}
clear_stack(s);
if(temp && p == NULL) p = temp;
return p;
}

// 前 根 左 右
void pre_order_node(Node *root) {
if(root == NULL) return ;
printf("%c ", root->data);
pre_order_node(root->lchild);
pre_order_node(root->rchild);
return ;
}

// 前
void pre_order(Tree *tree) {
if(tree == NULL) return ;
printf("pre_order (%d) : ", tree->length);
pre_order_node(tree->root);
printf("\n");
return ;
}

// 中 左 根 右
void in_order_node(Node *root) {
if(root == NULL) return ;
in_order_node(root->lchild);
printf("%c ", root->data);
in_order_node(root->rchild);
return ;
}

// 中
void in_order(Tree *tree) {
if(tree == NULL) return ;
printf("in_order (%d) : ", tree->length);
in_order_node(tree->root);
printf("\n");
return ;
}

// 后 左 右 根
void post_order_node(Node *root) {
if(root == NULL) return ;
in_order_node(root->lchild);
in_order_node(root->rchild);
printf("%c ", root->data);
return ;
}

// 后
void post_order(Tree *tree) {
if(tree == NULL) return ;
printf("post_order (%d) : ", tree->length);
post_order_node(tree->root);
printf("\n");
return ;
}

int main() {
char str[1000] = {0};
int node_num = 0;
scanf("%[^\n]s", str);
getchar();
Tree *tree = getNewTree();
tree-> root = build(str, &node_num);
tree->length = node_num;
pre_order(tree);
in_order(tree);
post_order(tree);
return 0;
}

image-20220118154618267

评论