-
Notifications
You must be signed in to change notification settings - Fork 1
/
二叉树的建立与基本操作.cpp
161 lines (138 loc) · 3.07 KB
/
二叉树的建立与基本操作.cpp
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
//
// main.cpp
// 数据结构
//
// Created by 刘冠中 on 2016/10/18.
// Copyright © 2016年 刘冠中. All rights reserved.
//
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int INF = 0x7f7f7f7f;
const int N = 1e3 + 10;
struct Node
{
char data;
Node *left, *right;
Node(): left(NULL), right(NULL) {}
Node(char data, Node *left = NULL, Node *right = NULL):
data(data), left(left), right(right) {}
};
Node *build(char s[])
{
if (strlen(s) == 0 || s[0] == '#') return NULL;
Node *T = new Node(s[0]);
queue<Node *> que;
que.push(T);
int len = strlen(s);
int i = 1;
while (i < len && !que.empty())
{
Node *p = que.front(); que.pop();
if (p == NULL) continue;
if (s[i] != '#')
{
p->left = new Node(s[i]);
que.push(p->left);
}
i++;
if (i < len && s[i] != '#')
{
p->right = new Node(s[i]);
que.push(p->right);
}
i++;
}
return T;
}
void indent(Node *T, int deep);
void pre_order(Node *T);
void in_order(Node *T);
void post_order(Node *T);
int count_leaf(Node *T);
void swap_node(Node *T);
char s[N];
int main()
{
scanf("%s", s);
int len = strlen(s);
if (s[len - 1] != '#' && s[len - 2] != '#'
&& s[len - 3] == '#' && s[len - 4] == '#'
&& s[len - 5] == '#' && s[len - 6] == '#'
&& s[len - 7] == '#' && s[len - 8] == '#')
{
s[len - 6] = s[len - 2];
s[len - 5] = s[len - 1];
s[len - 4] = '\0';
}
Node *T = build(s);
printf("BiTree\n");
indent(T, 0);
printf("pre_sequence : ");
pre_order(T);
printf("\n");
printf("in_sequence : ");
in_order(T);
printf("\n");
printf("post_sequence : ");
post_order(T);
printf("\n");
printf("Number of leaf: %d\n", count_leaf(T));
swap_node(T);
printf("BiTree swapped\n");
indent(T, 0);
printf("pre_sequence : ");
pre_order(T);
printf("\n");
printf("in_sequence : ");
in_order(T);
printf("\n");
printf("post_sequence : ");
post_order(T);
printf("\n");
return 0;
}
void indent(Node *T, int deep)
{
if (T == NULL) return;
for (int i = 1; i <= deep; i++) printf(" ");
printf("%c\n", T->data);
indent(T->left, deep + 1);
indent(T->right, deep + 1);
}
void pre_order(Node *T)
{
if (T == NULL) return;
printf("%c", T->data);
pre_order(T->left);
pre_order(T->right);
}
void in_order(Node *T)
{
if (T == NULL) return;
in_order(T->left);
printf("%c", T->data);
in_order(T->right);
}
void post_order(Node *T)
{
if (T == NULL) return;
post_order(T->left);
post_order(T->right);
printf("%c", T->data);
}
int count_leaf(Node *T)
{
if (T == NULL) return 0;
if (T->left == NULL && T->right == NULL) return 1;
return count_leaf(T->left) + count_leaf(T->right);
}
void swap_node(Node *T)
{
if (T == NULL) return;
swap(T->left, T->right);
swap_node(T->left);
swap_node(T->right);
}