-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
binary_search_tree.h
199 lines (176 loc) · 4.15 KB
/
binary_search_tree.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
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
195
196
197
198
199
/*******************************************************************************
* ALGORITHM IMPLEMENTAIONS
*
* /\ | _ _ ._ o _|_ |_ ._ _ _
* /--\ | (_| (_) | | |_ | | | | | _>
* _|
*
* BINARY SEARCH TREE
*
* Features:
* 1. Expected search time is O(log(n)), with worst case O(n).
* 2. Data should be !!!SHUFFLED!!! first before tree creation.
* 3. First initialize the value of the root (pointer to the
* structure treeNode) with NULL. eg:
* treeNode *root = NULL
*
* http://en.wikipedia.org/wiki/Binary_search_tree
*
******************************************************************************/
#ifndef ALGO_BINARY_SEARCH_TREE_H__
#define ALGO_BINARY_SEARCH_TREE_H__
#include <stdlib.h>
#include <stdint.h>
#include <exception>
#include <iostream>
namespace alg {
template<typename KeyT, typename ValueT>
class BST {
private:
/**
* binary search tree definition.
*/
struct treeNode {
KeyT key; // key
ValueT value; // data
treeNode *parent; // parent
treeNode *left; // left child
treeNode *right; // right child
};
class BSTException: public std::exception {
public:
virtual const char * what() const throw() {
return "key does not exist";
}
} excp_key;
private:
treeNode * m_root; // the root
private:
BST(const BST&);
BST& operator=(const BST&);
public:
BST():m_root(NULL){};
~BST() {
destruct_(m_root);
}
/**
* find key
*/
treeNode * find(const KeyT & key) {
treeNode * n= m_root;
while (n!=NULL && key != n->key) {
if (key < n->key) {
n = n->left;
} else {
n = n->right;
}
}
return n;
}
/**
* insert a new data into the binary search tree.
*/
void insert(const KeyT & key, const ValueT & value) {
treeNode *z= new treeNode;
z->key = key;
z->value = value;
z->left = z->right = z->parent = NULL;
treeNode * n = m_root;
treeNode * y = NULL;
while(n!=NULL) {
y = n;
if(key < n->key) {
n = n->left;
} else {
n = n->right;
}
}
z->parent = y;
if (y==NULL) {
m_root = z;
} else if (key < y->key) {
y->left = z;
} else {
y->right = z;
}
}
/**
* delete a key from the binary search tree.
*/
bool deleteKey(const KeyT & key) {
treeNode *z = find(key);
if (z == NULL) {
return false;
}
if (z->left == NULL) {
transplant(z, z->right);
} else if (z->right == NULL) {
transplant(z, z->left);
} else {
// find the minimum element of the right subtree
treeNode *y = minimum(z->right);
if (y->parent != z) {
// replace y with right-child
transplant(y, y->right);
// replace right-child of y with the right-child of z
y->right = z->right;
// make y the parent of the right-child
y->right->parent = y;
}
// replace z with y
transplant(z,y);
y->left = z->left;
y->left->parent = y;
}
delete z;
return true;
}
void print_tree(treeNode * n, int indent) {
if (n == NULL) {
return;
}
print_tree(n->right, indent+1);
int i;
for (i=0;i<indent;i++){
printf(" ");
}
std::cout << "[" << n->key << "," << n->value << "]" << std::endl;
print_tree(n->left, indent+1);
}
void print_helper() {
print_tree(m_root, 0);
}
private:
void destruct_(treeNode *n) {
if (n==NULL) return;
destruct_(n->left);
destruct_(n->right);
delete n;
}
/**
* replace node u with v.
*/
void transplant(treeNode *u, treeNode *v) {
if (u->parent == NULL) {
m_root = v;
} else if (u == u->parent->left) {
u->parent->left = v;
} else {
u->parent->right = v;
}
if (v!=NULL) {
v->parent = u->parent;
}
}
/**
* find the minimum element of the subtree
*/
treeNode * minimum(treeNode *x) {
while (x->left != NULL) {
x = x->left;
}
return x;
}
};
}
#endif //