-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst.cpp
More file actions
261 lines (215 loc) · 5.42 KB
/
bst.cpp
File metadata and controls
261 lines (215 loc) · 5.42 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*This file contains an implementation of binary search tree
* Code by: Humayun Kabir, humayun.k1@gmail.com */
#include <iostream>
#include "queue.cpp"
using namespace std;
//a struct to represent tree node
template <class Key, class Value>
struct treeNode {
Key key;
Value val;
treeNode *left, *right;
int count;
//default constructor
treeNode() : count(0), left(NULL), right(NULL) {
}
//constructor
treeNode(Key key, Value val, int count) : key(key), val(val), count(count), left(NULL), right(NULL) {
}
//destructor
~treeNode() {
delete left;
delete right;
}
};
//a class to represent binry search tree
template<class Key, class Value>
class BST {
private:
treeNode<Key,Value> *root;
//returns the number of nodes in the tree rooted at x
int size(treeNode<Key,Value> *x) {
if(x == NULL)
return 0;
return
x->count;
}
//recursively inserts a node in the tree rooted at x
treeNode<Key,Value> *insert(treeNode<Key,Value> *x, Key key, Value val) {
if(x == NULL)
return new treeNode<Key,Value>(key, val, 1);
if( key < x->key)
x->left = insert(x->left, key, val);
else if( key > x->key)
x->right = insert(x->right, key, val);
else
x->val = val;
x->count = 1 + size(x->left) + size(x->right);
return x;
}
//inorder traversal of a tree rooted at x
void inorder(treeNode<Key,Value> *x, Queue<Key> & q) {
if(x == NULL)
return;
inorder(x->left,q);
q.Enqueue(x->key);
inorder(x->right,q);
}
//preorder traversal of a tree rooted at x
void preorder(treeNode<Key,Value> *x, Queue<Key> & q){
if( x == NULL)
return;
q.Enqueue(x->key);
preorder(x->left, q);
preorder(x->right, q);
}
//delete the minimum element from the tree rooted at x
treeNode<Key,Value> *deleteMin(treeNode<Key,Value> *x, bool fromDel) {
if( x == NULL)
return NULL;
if(x->left == NULL) {
treeNode<Key,Value> *retNode = x->right;
if( !fromDel ) {
x->right = NULL;
delete x;
}
return retNode;
}
x->left = deleteMin(x->left, fromDel);
x->count = 1 + size(x->left) + size(x->right);
return x;
}
//finds the key with minimum value in the tree rootted at x
treeNode<Key,Value> *min(treeNode<Key,Value> *x) {
if(x == NULL)
return NULL;
while(x->left != NULL) {
x = x->left;
}
return x;
}
//deletes a node with Key key from the tree rooted at x
treeNode<Key,Value> *Delete(treeNode<Key,Value> *x, Key key) {
if(x == NULL)
return NULL;
//delete the key from left subtree if it is less than the root
if( key < x->key)
x->left = Delete(x->left, key);
//delete the key from right subtree if it is greater than the root
else if(key > x->key)
x->right = Delete(x->right, key);
//if key is equal to the key of the root
else {
//if left subtree is empty return right subtree
if(x->left == NULL) {
treeNode<Key,Value> *retNode = x->right;
x->right = NULL;
delete x;
return retNode;
}
//if right subtree is empty return left subtree
if(x->right == NULL) {
treeNode<Key,Value> *retNode = x->left;
x->left = NULL;
delete x;
return retNode;
}
//if both the subtrees are non-empty
treeNode<Key,Value> *t = x;
//x is replaced by the minimum element of the right subtree
x = min(t->right);
x->right = deleteMin(t->right, true);
x->left = t->left;
t->left = NULL;
t->right = NULL;
delete t;
}
x->count = size(x->left) + size(x->right) + 1;
return x;
}
//postorder traversal of the tree
void postorder(treeNode<Key,Value> *x) {
if(x == NULL)
return;
postorder(x->left);
postorder(x->right);
cout<<"Key: "<<x->key<<" Val: "<<x->val<<endl;
}
//inorder traversal of the tree
void inorder(treeNode<Key, Value> *x) {
if( x == NULL )
return;
inorder(x->left);
cout<<"Key: "<<x->key<<" Val: "<<x->val<<endl;
inorder(x->right);
}
public:
//default constructor
BST() {
root = NULL;
}
//returns number of elements in the tree
int size() {
return size(root);
}
//insert a node in the BST with given key and value
void insert(Key key, Value val) {
root = insert( root, key, val);
}
//returns the keys in the tree
Queue<Key> keys() {
Queue<Key> q;
inorder(root, q);
return q;
}
//search for a node with given key in the tree
treeNode<Key,Value> *search(Key key){
treeNode<Key,Value> *x = root;
while( x != NULL ) {
if(key < x->key)
x = x->left;
else if(key > x->key)
x = x->right;
else
return x;
}
return NULL;
}
//delets the minimum element in the tree
void deleteMin() {
root = deleteMin(root, true);
}
//deletes a given key from the tree
void Delete(Key key) {
root = Delete(root, key);
}
//postorder traversal of the tree
void postorder() {
this->postorder(root);
}
//inorder traversal of the tree
void inorder() {
this->inorder(root);
}
//destructor
~BST() {
delete root;
}
};
//Test the data structure
int main(int argc, char *argv[]) {
BST<int,string> bst;
bst.insert(1, "hello1");
bst.insert(10, "hello10");
bst.insert(-4, "hello-4");
bst.insert(40, "hello40");
cout<<"Size of the tree: "<<bst.size()<<endl;
cout<<"Inorder traversal."<<endl;
bst.inorder();
bst.Delete(1);
cout<<"after deleting key: 1\n";
cout<<"Size of the tree: "<<bst.size()<<endl;
cout<<"Inorder traversal."<<endl;
bst.inorder();
return 0;
}