forked from yuanzoudetuzi/binaryTree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.cpp
More file actions
executable file
·183 lines (153 loc) · 4.5 KB
/
BinaryTree.cpp
File metadata and controls
executable file
·183 lines (153 loc) · 4.5 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// Created by Administrator on 2017/4/6.
//
#include <iostream>
#include "BinaryTree.h"
template< typename T>
BinaryTree<T>::BinaryTree(const BinaryTree &bt) {
if( nullptr != bt.root ) {
this->root = clone(bt.root);
} else {
root = nullptr;
}
};
template< typename T>
BinaryTree<T>::BinaryTree(const T &theElement) {
root = new BinaryNode(theElement);
/*root->element = theElement;
root->leftNode = nullptr;
root->rightNode = nullptr;*/
}
template< typename T>
BinaryTree<T>::~BinaryTree() {
makeEmpty();
/* root->~BinaryNode();*/
};
template< typename T>
void BinaryTree<T>::insert(const T &theElement) {
insert(theElement, root );
};
template< typename T>
void BinaryTree<T>::remove(const T &theElement) {
remove(theElement,root);
};
template< typename T>
void BinaryTree<T>::makeEmpty() {
makeEmpty(root);
};
template< typename T>
bool BinaryTree<T>::isFind(const T &theElement) const {
isFind(theElement,root);
};
template< typename T>
void BinaryTree<T>::preOrder() const {
preOrder(root);
};
template< typename T>
void BinaryTree<T>::inOrder() const {
inOrder(root);
};
template< typename T>
void BinaryTree<T>::postOrder() const {
postOrder(root);
};
template< typename T>
typename BinaryTree<T>::BinaryNode* BinaryTree<T>::clone(const BinaryNode *r) {
if ( nullptr == r ) {
return nullptr;
} else {
return new BinaryNode(r->element, clone(r->leftNode), clone(r->rightNode));
}
}
template< typename T>
void BinaryTree<T>::insert(const T &theElement, BinaryNode * &t ) {
if ( nullptr == t ){
t = new BinaryNode (theElement);
} else if ( theElement < t->element ) {
insert( theElement, t->leftNode );
} else if ( theElement > t->element ) {
insert ( theElement, t->rightNode );
} else {//重复的数据不添加到树中
}
};
template< typename T>
void BinaryTree<T>::remove(const T &theElement, BinaryNode * &t ) {
if( nullptr == t ) {
return;
} else {
if ( theElement < t->element) {
remove(t->leftNode);
} else if ( theElement > t->element ) {
remove (t->rightNode);
} else if (nullptr != t->leftNode && nullptr != t->rightNode ) { //需要删除的节点两个儿子
t->element = findMin(t->rightNode)->element;
remove(t->element, t->rightNode);
} else {
BinaryNode * oldNode = t;
t = ( nullptr!= t->leftNode) ? t->leftNode : t->rightNode;
delete oldNode;
}
}
};
template< typename T>
void BinaryTree<T>::makeEmpty( BinaryNode * &t ) {
if ( nullptr !=t ) {
makeEmpty(t->leftNode);
makeEmpty(t->rightNode);
std::cout << "delete: " << t->element << std::endl;
delete t;
}
t = nullptr;
};
template< typename T>
bool BinaryTree<T>::isFind(const T &theElement, BinaryNode * t ) const {
if ( nullptr == t ){
return false;
} else if ( theElement < t->element ) {
return isFind( theElement, t->leftNode );
} else if ( theElement > t->element ) {
return isFind ( theElement, t->rightNode );
} else { //匹配
return true;
}
};
template< typename T>
typename BinaryTree<T>::BinaryNode * BinaryTree<T>::findMin(BinaryNode *bNode) const {
if ( nullptr!= bNode) {
while( nullptr != bNode->leftNode) {
bNode = bNode->leftNode;
}
}
return bNode;
}
template< typename T>
typename BinaryTree<T>::BinaryNode * BinaryTree<T>::findMax(BinaryNode *bNode) const {
if ( nullptr!= bNode) {
while( nullptr != bNode->rightNode) {
bNode = bNode->rightNode;
}
}
return bNode;
}
template< typename T>
void BinaryTree<T>::preOrder( BinaryNode *bNode ) const {
if( nullptr != bNode ) {
std::cout << bNode->element << " " ;
preOrder(bNode->leftNode);
preOrder(bNode->rightNode);
}
};
template< typename T>
void BinaryTree<T>::inOrder( BinaryNode *bNode ) const {
if( nullptr != bNode ) {
inOrder(bNode->leftNode);
std::cout << bNode->element << " " ;
inOrder(bNode->rightNode);
}
};
template< typename T>
void BinaryTree<T>::postOrder( BinaryNode *bNode ) const {
postOrder(bNode->leftNode);
postOrder(bNode->rightNode);
std::cout << bNode->element << " " ;
};