-
Notifications
You must be signed in to change notification settings - Fork 0
/
bstPriorityQueue.cpp
186 lines (160 loc) · 5.1 KB
/
bstPriorityQueue.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
#include <iostream>
using namespace std;
//Declare template and class for BST
template<class T>
class BST{
//Declare the node for the BST
struct Node{
T data;
//Node Pointers
Node* lChildptr;
Node* rChildptr;
Node(T dataNew){ //Constructor to initialize data
data = dataNew;
lChildptr = NULL;
rChildptr = NULL;
}
}; //struct node ends here
private:
//Declare root node pointer
Node* root;
//Function to insert into the BST
void Insert(T newData, Node* &theRoot){
//For empty tree
if(theRoot == NULL)
{
theRoot = new Node(newData);
return;
}
//Store either in left or right sub-tree
if(newData < theRoot->data)
Insert(newData, theRoot->lChildptr);
else
Insert(newData, theRoot->rChildptr);;
}
//Function to Print the BST
void PrintTree(Node* p){
if(p != NULL){
if(p->rChildptr)
PrintTree(p->rChildptr);
cout<<" "<<p->data<<" ";
if(p->lChildptr)
PrintTree(p->lChildptr);
}
else
cout<<"\nEmpty BST";
}
struct Node* search(Node* root, T key)
{
// Base Cases: root is null or key is present at root
if (root == NULL || root->data == key)
return root;
// Key is greater than root's key
if (root->data < key)
return search(root->rChildptr, key);
// Key is smaller than root's key
return search(root->lChildptr, key);
}
public:
BST(){
root = NULL;
}
void AddItem(T newData){
Insert(newData, root);
}
void PrintTree(){
PrintTree(root);
}
//search a given key in a given BST
bool search(T key)
{
// Base Cases: root is null or key is present at root
if (root == NULL || root->data == key)
return true;
// Key is greater than root's key
if (root->data < key)
return search(root->rChildptr, key);
// Key is smaller than root's key
return search(root->lChildptr, key);
}
//Pop the highest element
int pop(){
int ele;
Node* prevNode = root;
Node* traverser = root;
while(traverser->rChildptr != NULL){
prevNode = traverser;
traverser = traverser->rChildptr;
ele = traverser->data;
}
if(traverser->lChildptr != NULL) {
prevNode->rChildptr = traverser->lChildptr;
}else {
prevNode->rChildptr = NULL;
}
return ele;
}
}; //BST body ends here
int main(){
//Create a BST object
BST<int> *myBT = new BST<int>();
//create a integer var to nput element values
int data;
char ch = 'y';
do{
cout<<"\n\n***MENU FOR BST OPERATIONS***\n\n";
cout<<"1. Insert an element\n";
cout<<"2. Search for an element\n";
cout<<"3. Pop an element\n";
cout<<"4. Traverse the BST\n";
cout<<"5. Exit\n";
cout<<"Enter your choice: ";
int choice;
cin>>choice;
switch(choice){
case 1:{
//input data from user
cout<<"\nEnter values in the BST";
cout<<"(-1 to end input):\n";
cin>>data;
while(data != -1){
//Insert values
myBT->AddItem(data);
cin>>data; //input more data till -1 is input
}
break;
}
case 2:{
cout<<"\nEnter value to be searched for : ";
int key;
cin>>key; //input the value to be searched for
bool var = myBT->search(key);
if(var){
cout<<"\nElement is Present...\n";
}
else{
cout<<"\nElement Not Found!\n";
}
break;
}
case 3:{
cout<<"\nPopped element : "<<myBT->pop();
break;
}
case 4:{
cout<<"\nBST: ";
myBT->PrintTree();
break;
}
case 5:{
cout<<"\nGood Bye have a great time...";
ch = 'n';
break;
}
default:
cout<<"Wrong option! Please select again\n";
}
}while(ch == 'y' || ch == 'Y');
cout<<"\n";
return 0;
}