-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary-tree.cpp
156 lines (122 loc) · 2.93 KB
/
Binary-tree.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
#include <bits/stdc++.h>
using namespace std;
class TreeNode {
public:
char val;
TreeNode *lc, *rc;
TreeNode *fa;
int depth;
TreeNode() {
val = depth = 0;
lc = rc = nullptr;
fa = nullptr;
}
};
void pre_order_traversal(TreeNode *root) {
cout << "visit:" << root->val << endl;
if (root->lc != nullptr)
pre_order_traversal(root->lc);
if (root->rc != nullptr)
pre_order_traversal(root->rc);
}
void in_order_traversal(TreeNode *root) {
if (root->lc != nullptr)
in_order_traversal(root->lc);
cout << "visit:" << root->val << endl;
if (root->rc != nullptr)
in_order_traversal(root->rc);
}
void post_order_traversal(TreeNode *root) {
if (root->lc != nullptr)
post_order_traversal(root->lc);
if (root->rc != nullptr)
post_order_traversal(root->rc);
cout << "visit:" << root->val << endl;
}
TreeNode *longest = nullptr;
void dfs(TreeNode *root, int dep = 1) {
root->depth = dep;
if (longest == nullptr || root->depth > longest->depth) longest = root;
if (root->lc != nullptr)
dfs(root->lc, dep + 1);
if (root->rc != nullptr)
dfs(root->rc, dep + 1);
}
void Getlongest(TreeNode *root) {
dfs(root);
stack<TreeNode *> s;
while (!s.empty()) s.pop();
TreeNode *now = longest;
while (now != nullptr && now != NULL) {
s.push(now);
now = now->fa;
}
cout << "the longest way is:";
while (!s.empty()) {
if (s.top() != NULL && s.top() != nullptr)
cout << ((*(s.top())).val) << " ";
s.pop();
}
cout << endl;
return;
}
void GetLeaf(TreeNode *root) {
if (root == nullptr || root == NULL) return;
if (root->lc == nullptr && root->rc == nullptr)
cout << root->val << " is a leaf" << endl;
else {
if (root->rc != nullptr)
GetLeaf(root->lc);
if (root->rc != nullptr)
GetLeaf(root->rc);
}
}
TreeNode a[30];
void Init() {
a[1].val = 'A';
a[2].val = 'B';
a[3].val = 'C';
a[4].val = 'D';
a[5].val = 'E';
a[6].val = 'F';
a[7].val = 'G';
a[8].val = 'H';
a[9].val = 'I';
a[1].lc = &a[2];
a[1].rc = &a[7];
a[1].fa = nullptr;
a[2].lc = &a[3];
a[2].rc = &a[5];
a[2].fa = &a[1];
a[3].lc = nullptr;
a[3].rc = &a[4];
a[3].fa = &a[2];
a[4].lc = nullptr;
a[4].rc = nullptr;
a[4].fa = &a[3];
a[5].lc = nullptr;
a[5].rc = &a[6];
a[5].fa = &a[2];
a[6].lc = nullptr;
a[6].rc = nullptr;
a[6].fa = &a[5];
a[7].lc = &a[8];
a[7].rc = &a[9];
a[7].fa = &a[1];
a[6].lc = nullptr;
a[6].rc = nullptr;
a[6].fa = &a[7];
a[6].lc = nullptr;
a[6].rc = nullptr;
a[6].fa = &a[7];
}
int main() {
Init();
pre_order_traversal(&a[1]);
cout << endl;
in_order_traversal(&a[1]);
cout << endl;
Getlongest(&a[1]);
cout << endl;
GetLeaf(&a[1]);
}