-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathbinarySearchTree.h
171 lines (167 loc) · 3.69 KB
/
binarySearchTree.h
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
//Author : yqtao
//date : 2016.09.09
//Email : [email protected]
/*
Implement binary search tree
*/
#ifndef BINARY_SEARCH_TREE
#define BINARY_SEARCH_TREE
#include<iostream>
#include<stack>
#include<deque>
using namespace std;
template<typename T> struct BSTNode{
T val;
BSTNode<T> *left, *right;
BSTNode<T>(int x) : val(x),left(nullptr),right(nullptr){}
};
template<typename T> class BST {
private:
BSTNode<T>* root;
public:
BST() { root = nullptr; }
bool empty() { return root = nullptr; }
void insert(const T& x);
BSTNode<T>* search(const T& x);
void inorder(); //迭代中序遍历
void morris_inorder(); //mirror中序遍历
void breadFirst(); //广度优先遍历
void findAndDelete(const T& x);
void deleteNode(BSTNode<T>*& node);
};
template<typename T>
void BST<T>::insert(const T& x){
BSTNode<T> *p = root, *prev = 0;
while (p) {
prev = p;
if (x < p->val) p = p->left;
else p = p->right;
}
if (root == nullptr) root = new BSTNode<T>(x);
else if (x < prev->val) prev->left = new BSTNode<T>(x);
else prev->right = new BSTNode<T>(x);
}
//
template<typename T>
BSTNode<T>* BST<T>::search(const T& x) {
BSTNode<T>* p = root;
while (p) {
if (p->val == x)
return p;
else if (x < p->val)
p = p->left;
else
p = p->right;
}
return nullptr;
}
//
template<typename T>
void BST<T>::inorder() {
stack<BSTNode<T>*> st;
BSTNode<T> *p = root;
while (!st.empty() || p) {
if (p) {
st.push(p);
p = p->left;
}
else {
p = st.top();
st.pop();
cout << p->val << " ";
p = p->right;
}
}
cout << endl;
}
//
template<typename T>
void BST<T>::morris_inorder() {
BSTNode<T> *cur = root, *pre = nullptr;
while (cur) {
if (cur->left==nullptr) { //1.当前左节点为空,则输出
cout << cur->val << " "; //将当前结点为他的右孩子
cur = cur->right;
}
else {
pre = cur->left; //2.当前结点不为空,则寻找他在中序遍历的前驱结点
while (pre->right&&pre->right!=cur) {
pre = pre->right;
}
if (pre->right == nullptr) { //2.1 如果当前的结点的前驱结点的右孩子为空
pre->right = cur; //则其指向当前结点
cur = cur->left;
}
else { //2.2 如果当前结点的右孩子不空,则还原原来的结构
pre->right = nullptr;
cout << cur->val << " ";
cur = cur->right;
}
}
}
cout << endl;
}
//
template<typename T>
void BST<T>::breadFirst() {
deque<BSTNode<T>*> deq;
BSTNode<T> *p = root;
deq.push_back(p);
while (!deq.empty()) {
p = deq.front();
deq.pop_front();
cout<< p->val << " ";
if (p->left) deq.push_back(p->left);
if (p->right) deq.push_back(p->right);
}
cout << endl;
}
//
template<typename T>
void BST<T>::findAndDelete(const T& x) {
BSTNode<T>* node = root, *prev = 0;
while (node) {
if (node->val == x)
break;
prev = node;
if (node->val < x)
node = node->right;
else
node = node->left;
}
if (node&&node->val == x) {
if (node == root)
deleteNode(root);
else if (prev->left == node)
deleteNode(prev->left);
else
deleteNode(prev->right);
}
else if (root)
cout << x << " is not in the tree" << endl;
else cout << "the tree is empty" << endl;
}
//
template<typename T>
void BST<T>::deleteNode(BSTNode<T>*& node) {
BSTNode<T> *prev, *tmp = node;
if (node->left == nullptr) //node has no left child
node = node->right;
else if (node->right == nullptr) //node has no right child
node = node->left;
else { //have both child
tmp = node->left;
prev = node;
while (tmp->right) {
prev = tmp;
tmp = tmp->right;
}
node->val = tmp->val;
if (prev == node)
prev->left = tmp->left;
else
prev->right = tmp->left;
}
delete tmp;
}
#endif // !BINARY_SEARCH_TREE