-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRandomuzed_bst.cpp
More file actions
261 lines (248 loc) · 6.63 KB
/
Randomuzed_bst.cpp
File metadata and controls
261 lines (248 loc) · 6.63 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
/*
* C++ Program to Implement Randomized Binary Search Tree
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstdlib>
#define MAX_VALUE 65536
using namespace std;
/*
* Class RBSTNode
*/
class RBSTNode
{
public:
RBSTNode *left, *right;
int priority, element;
/** Constructor **/
RBSTNode()
{
this->element = 0;
this->left = this;
this->right = this;
this->priority = MAX_VALUE;
}
/** Constructor **/
RBSTNode(int ele)
{
RBSTNode(ele, NULL, NULL);
}
/** Constructor **/
RBSTNode(int ele, RBSTNode *left, RBSTNode *right)
{
this->element = ele;
this->left = left;
this->right = right;
this->priority = rand() % 100 + 1;
}
};
/*
* Class RandomizedBinarySearchTree
*/
class RandomizedBinarySearchTree
{
private:
RBSTNode *root;
RBSTNode *nil;
public:
/** Constructor **/
RandomizedBinarySearchTree()
{
root = nil;
}
/** Function to check if tree is empty **/
bool isEmpty()
{
return root == nil;
}
/** Make the tree logically empty **/
void makeEmpty()
{
root = nil;
}
/** Functions to insert data **/
void insert(int X)
{
root = insert(X, root);
}
RBSTNode *insert(int X, RBSTNode *T)\
{
if (T == nil)
return new RBSTNode(X, nil, nil);
else if (X < T->element)
{
T->left = insert(X, T->left);
if (T->left->priority < T->priority)
{
RBSTNode *L = T->left;
T->left = L->right;
L->right = T;
return L;
}
}
else if (X > T->element)
{
T->right = insert(X, T->right);
if (T->right->priority < T->priority)
{
RBSTNode *R = T->right;
T->right = R->left;
R->left = T;
return R;
}
}
return T;
}
/** Functions to count number of nodes **/
int countNodes()
{
return countNodes(root);
}
int countNodes(RBSTNode *r)
{
if (r == nil)
return 0;
else
{
int l = 1;
l += countNodes(r->left);
l += countNodes(r->right);
return l;
}
}
/** Functions to search for an element **/
bool search(int val)
{
return search(root, val);
}
bool search(RBSTNode *r, int val)
{
bool found = false;
while ((r != nil) && !found)
{
int rval = r->element;
if (val < rval)
r = r->left;
else if (val > rval)
r = r->right;
else
{
found = true;
break;
}
found = search(r, val);
}
return found;
}
/** Function for inorder traversal **/
void inorder()
{
inorder(root);
}
void inorder(RBSTNode *r)
{
if (r != nil)
{
inorder(r->left);
cout<<r->element <<" ";
inorder(r->right);
}
}
/** Function for preorder traversal **/
void preorder()
{
preorder(root);
}
void preorder(RBSTNode *r)
{
if (r != nil)
{
cout<<r->element <<" ";
preorder(r->left);
preorder(r->right);
}
}
/** Function for postorder traversal **/
void postorder()
{
postorder(root);
}
void postorder(RBSTNode *r)
{
if (r != nil)
{
postorder(r->left);
postorder(r->right);
cout<<r->element <<" ";
}
}
};
/*
* Main Contains Menu
*/
int main()
{
RandomizedBinarySearchTree rbst;
cout<<"Randomized Binary SearchTree Test\n";
char ch;
int choice, item;
/** Perform tree operations **/
do
{
cout<<"\nRandomized Binary SearchTree Operations\n";
cout<<"1. Insert "<<endl;
cout<<"2. Search "<<endl;
cout<<"3. Count Nodes "<<endl;
cout<<"4. Check Empty"<<endl;
cout<<"5. Clear"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch (choice)
{
case 1:
cout<<"Enter integer element to insert: ";
cin>>item;
rbst.insert(item);
break;
case 2:
cout<<"Enter integer element to search: ";
cin>>item;
if (rbst.search(item))
cout<<"Element "<<item<<" found in the Tree"<<endl;
else
cout<<"Element "<<item<<" not found in the Tree"<<endl;
break;
case 3:
cout<<"Nodes = "<<rbst.countNodes()<<endl;
break;
case 4:
if (rbst.isEmpty())
cout<<"Tree is Empty"<<endl;
else
cout<<"Tree is not Empty"<<endl;
break;
case 5:
cout<<"\nRandomizedBinarySearchTree Cleared"<<endl;
rbst.makeEmpty();
break;
default:
cout<<"Wrong Entry \n ";
break;
}
/** Display tree **/
cout<<"\nPost order : ";
rbst.postorder();
cout<<"\nPre order : ";
rbst.preorder();
cout<<"\nIn order : ";
rbst.inorder();
cout<<"\nDo you want to continue (Type y or n) \n";
cin>>ch;
}
while (ch == 'Y'|| ch == 'y');
return 0;
}