-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathmirror_of_binary_tree.cpp
More file actions
161 lines (146 loc) · 3.62 KB
/
mirror_of_binary_tree.cpp
File metadata and controls
161 lines (146 loc) · 3.62 KB
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
/*
* Copyright(c) 2019 Jiau Zhang
* For more information see <https://github.com/JiauZhang/algorithms>
*
* This repo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation
*
* It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with THIS repo. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
using namespace std;
struct BinaryTree
{
BinaryTree(): p_Left(NULL), p_Right(NULL) {}
int m_Value;
BinaryTree* p_Left;
BinaryTree* p_Right;
};
//此函数不进行安全检查
BinaryTree* find_father(BinaryTree* root, int value)
{
BinaryTree* father = root;
while(true) {
if(father->m_Value<value && father->p_Right)
father = father->p_Right;
else if(father->m_Value>=value && father->p_Left)
father = father->p_Left;
else
break;
}
return father;
}
void add_node(BinaryTree** root, int value)
{
if(root) {
if(*root) {
BinaryTree* father = find_father(*root, value);
BinaryTree* son = new BinaryTree();
if(father->m_Value<value) {
father->p_Right = son;
son->m_Value = value;
} else {
father->p_Left = son;
son->m_Value = value;
}
} else {
*root = new BinaryTree();
(*root)->m_Value = value;
}
}
}
//先左子节点,然后根节点,最后右子节点
void print_inorder(BinaryTree* root)
{
if(root->p_Left)
print_inorder(root->p_Left);
cout << root->m_Value << ' ';
if(root->p_Right)
print_inorder(root->p_Right);
}
//先根节点,然后左子节点,最后右子节点
void print_preorder(BinaryTree* root)
{
cout << root->m_Value << ' ';
if(root->p_Left)
print_preorder(root->p_Left);
if(root->p_Right)
print_preorder(root->p_Right);
}
//先左子节点,然后右子节点,最后根节点
void print_postorder(BinaryTree* root)
{
if(root->p_Left)
print_postorder(root->p_Left);
if(root->p_Right)
print_postorder(root->p_Right);
cout << root->m_Value << ' ';
}
void mirror_of_binary_tree(BinaryTree* root)
{
if(root) {
if(root->p_Left || root->p_Right) {
BinaryTree* temp = root->p_Left;
root->p_Left = root->p_Right;
root->p_Right = temp;
mirror_of_binary_tree(root->p_Left);
mirror_of_binary_tree(root->p_Right);
}
}
}
void Test(BinaryTree* root)
{
cout << "basic tree" << endl;
print_preorder(root);
cout << endl;
print_inorder(root);
cout << endl;
print_postorder(root);
cout << endl;
cout << "mirror 1" << endl;
mirror_of_binary_tree(root);
print_preorder(root);
cout << endl;
print_inorder(root);
cout << endl;
print_postorder(root);
cout << endl;
cout << "mirror 2" << endl;
mirror_of_binary_tree(root);
print_preorder(root);
cout << endl;
print_inorder(root);
cout << endl;
print_postorder(root);
cout << endl;
}
int main(int argc, char** argv)
{
BinaryTree* root_test1 = NULL;
//完全二叉树
add_node(&root_test1, 5);
add_node(&root_test1, 3);
add_node(&root_test1, 7);
add_node(&root_test1, 1);
add_node(&root_test1, 4);
add_node(&root_test1, 6);
add_node(&root_test1, 9);
BinaryTree* root_test2 = NULL;
add_node(&root_test2, 5);
BinaryTree* root_test3 = NULL;
add_node(&root_test3, 5);
add_node(&root_test3, 4);
add_node(&root_test3, 3);
add_node(&root_test3, 2);
Test(root_test1);
Test(root_test2);
Test(root_test3);
return 0;
}