-
Notifications
You must be signed in to change notification settings - Fork 2
/
Binary search tree.cpp
228 lines (190 loc) · 5.53 KB
/
Binary search tree.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
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
#include <iostream>
#include <cstdio>
using namespace std;
namespace{
short CC_;
#define sf scanf
#define pf printf
#define PP getchar();
#define NL cout<<"\n";
#define pqueue priority_queue
#define testb(x_, i_) ((x_&1<<i_)!=0)
#define setb(x_, i_) (x_=(x_|(1<<i_)))
#define clrb(x_, i_) (x_=(x_&(!(1<<i_))))
#define DC(x_) cout<<">>> "<<#x_<<"\n";DA(x_.begin(), x_.end());
#define DD(x_) cout<<">>>>( "<<++CC_<<" ) "<<#x_<<": "<<x_<<endl;
#define SS printf(">_<LOOOOOK@MEEEEEEEEEEEEEEE<<( %d )>>\n",++CC_);
#define EXT(st_) cout<<"\n>>>Exicution Time: "<<(double)(clock()-st_)/CLOCKS_PER_SEC<<endl;
}
template <typename T>
class binarySearchTree{
private:
class Node{
public:
T data;
Node *lChild, *rChild;
Node(){
data= T();
lChild= rChild= NULL;
}
Node(const T& dt){
data= dt;
lChild= rChild= NULL;
}
};
Node* root;
int nodeCount;
public:
//default constructor
binarySearchTree(){
root= NULL;
nodeCount= 0;
}
bool empty(){
return nodeCount == 0;
}
int size(){
return nodeCount;
}
//wrapper function
const T* find(const T& target){
if(empty()){
std::cerr<< "Underflow error!"<<std::endl;
}
Node* temp= find(root, target);
if(temp == NULL) return NULL;
return &temp->data;
}
//find and return pointer to found element. NULL if not found
Node* find(Node* next, const T& target){
//target not found
if(next == NULL) return NULL;
//target found
else if(next->data == target) return next;
else if(next->data < target) return find(next->rChild, target);
else return find(next->lChild, target);
}
Node* insert(Node* next, const T& dt){
if(next == NULL){
return new Node(dt);
}
if(dt == next->data){
nodeCount--; //decreasing previously increased count as duplicates are not allowed
return next;
}
else if(dt < next->data)
next->lChild= insert(next->lChild, dt);
else
next->rChild= insert(next->rChild, dt);
return next;
}
//Find and return pointer to in-order successor in the tree
Node* inorderSuccessor(Node* subRoot){
subRoot= subRoot->rChild;
while(subRoot->lChild != NULL){
subRoot= subRoot->lChild;
}
return subRoot;
}
void insert(const T& dt){
nodeCount++;
if(root == NULL){
root = new Node(dt);
return;
}
insert(root, dt);
}
//wrapper function
void remove(const T& target){
if(empty()){
return;
}
//if remove was successful / returned non null pointer
if(find(root, target)){
nodeCount--;
remove(root, target);
if(nodeCount == 0) root= NULL;
}
}
Node* remove(Node* next, const T& target){
if(next == NULL) return NULL;
if(target == next->data){
//node has only one or no child
if(next->lChild == NULL){
Node* temp= next->rChild;
delete next;
return temp;
}
//node has only one or no child
else if(next->rChild == NULL){
Node* temp= next->lChild;
delete next;
return temp;
}
//node has two children
//replace this node with next larger element in the tree
Node* inorder_successor= inorderSuccessor(next);
next->data= inorder_successor->data;
next->rChild= remove(next->rChild, inorder_successor->data);
}
else if(target < next->data)
next->lChild= remove(next->lChild, target);
else
next->rChild= remove(next->rChild, target);
return next;
}
void clear(Node* doom){
if(doom == NULL) return;
if(doom->lChild != NULL)
clear(doom->lChild);
if(doom->rChild != NULL)
clear(doom->rChild);
delete doom;
}
//wrapper function
void clear(){
if(!empty()) clear(root);
}
//traverse and print each element in-order
void printInOrder(Node* next){
if(next == NULL) return;
else{
printInOrder(next->lChild);
std::cout<< next->data <<" ";
printInOrder(next->rChild);
}
}
//in order traversal wrapper function
void printInOrder(){
if(empty()) return;
printInOrder(root);
std::cout<<std::endl;
}
~binarySearchTree(){
clear();
}
};
int main(void)
{
using namespace std;
binarySearchTree<int> a;
char cmd;
int temp;
cout<< "press p *number* to insert a number in the bst"<<endl;
cout<< "press r *number* to remove a number in the bst"<<endl;
while(cin>>cmd){
if(cmd == 'p'){
cin>>temp;
a.insert(temp);
}
else if(cmd == 'r'){
cin>>temp;
a.remove(temp);
}
// DD(a.root)
a.printInOrder();
cout<< "Size: "<< a.size() <<endl;
// cout<< "ios: "<< ((a.inorderSuccessor(a.root) != NULL)? a.inorderSuccessor(a.root)->data : 99999) <<endl;
}
return 0;
}