-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.cpp
194 lines (169 loc) · 4.45 KB
/
BinaryTree.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
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
191
192
193
194
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
template<typename T>
struct BSTreeNode{
T value;
BSTreeNode* lchild;
BSTreeNode* rchild;
BSTreeNode(const T& value_):value(value_),lchild(NULL),rchild(NULL){}
};
template<typename T>
class BSTree{
typedef BSTreeNode<T> node_type;
public:
BSTree():root(NULL){}
~BSTree();
public:
BSTree<T> &operator=(const BSTree<T>&);
void insert_elem(const T&); //插入元素
bool remove_elem(const T&); //删除元素
void inoreder_traverse()const;
T find_min()const; //查找最左边的元素 即为最小
T find_max()const; //查找最右边的元素 即为最大
node_type* search_elem(const T&)const; //查找某个元素
protected:
node_type* copy_tree(node_type*);
void insert_elem(node_type* &,const T&);
bool remove_elem(node_type* &,const T&);
void inoreder_traverse(node_type*)const;
void destory_tree(node_type* &);
T find_max(node_type*) const;
T find_min(node_type*) const;
node_type* search_elem(node_type*,const T&)const;
private:
node_type * root; //每个对象都有一个根节点
};
// public interface
//销毁树
template<typename T>
BSTree<T>::~BSTree(){
destory_tree(root);
}
template<typename T>
void BSTree<T>::insert_elem(const T& val){
insert_elem(root,val);
}
template<typename T>
bool BSTree<T>::remove_elem(const T& val){
remove_elem(root,val);
}
template<typename T>
void BSTree<T>::inoreder_traverse() const{
inoreder_traverse(root);
}
template<typename T>
T BSTree<T>::find_min() const{
return find_min(root);
}
template<typename T>
T BSTree<T>::find_max()const{
return find_max(root);
}
template<typename T>
BSTreeNode<T>* BSTree<T>::search_elem(const T& target) const{
return search_elem(root,target);
}
//protected interface
template<typename T>
BSTreeNode<T>* BSTree<T>::copy_tree(node_type* t){
node_type* tmp;
if (t!=NULL){
tmp=new node_type(t->value);
tmp->lchild=copy_tree(t->lchild);
tmp->rchild=copy_tree(t->rchild);
}
return tmp;
}
// void insert_elem(node_type* &,const T&);
template<typename T>
void BSTree<T>::insert_elem(node_type* &t,const T& val){
if (t==NULL){
t=new node_type(val);
return;
}
if (t->value>val){
return insert_elem(t->lchild,val);
}
else if (t->value<val){
return insert_elem(t->rchild,val);
}
else return;
}
template<typename T>
bool BSTree<T>::remove_elem(node_type* &t,const T& val){
if (t==NULL) return false;
if (t->value>val){
remove_elem(t->lchild,val);
}
else if (t->value<val){
remove_elem(t->rchild,val);
}
else{
if (t->rchild!=NULL&&t->lchild!=NULL){
node_type* tmp=t;
tmp=t->rchild;
while(tmp->lchild!=NULL) tmp=tmp->lchild;
t->value=tmp->value;
//将右子树中最小的值,放到被删节点 然后在继续在被删几点的右子树中删除 这个值
//这时 又变成了前面的几种情况
remove_elem(t->rchild,t->value);
}
else{
node_type* tmp=t;
if (t->lchild!=NULL)
t=t->lchild;
else
t=t->rchild;
delete tmp;
}
return true;
}
}
template<typename T>
void BSTree<T>::destory_tree(node_type*& t){
if (t!=NULL){
destory_tree(t->lchild);
destory_tree(t->rchild);
delete t;
}
}
template<typename T>
T BSTree<T>::find_min(node_type * t)const{
while (t->lchild!=NULL) t=t->lchild;
return t->value;
}
template<typename T>
T BSTree<T>::find_max(node_type *t)const{
while (t->rchild!=NULL) t=t->rchild;
return t->value;
}
template<typename T>
BSTreeNode<T>* BSTree<T>::search_elem(node_type *t,const T& val)const{
if (t==NULL) return NULL;
else if (t->value>val) search_elem(t->lchild,val);
else if (t->value<val) search_elem(t->rchild,val);
else return t;
}
template<typename T>
void BSTree<T>::inoreder_traverse(node_type* t)const{
if (t!=NULL){
cout<<t->value<<' ';
inoreder_traverse(t->lchild);
inoreder_traverse(t->rchild);
}
}
int main(){
BSTree<int> bst;
int A[]={46,20,23,60,50,55,70};
for (int i=0;i<7;i++){
bst.insert_elem(A[i]);
}
bst.inoreder_traverse();
cout<<"before\n";
bst.remove_elem(46);
cout<<"after\n";
bst.inoreder_traverse();
}