Skip to content

Commit 0a12b09

Browse files
committed
Uploaded file
1 parent 1354399 commit 0a12b09

File tree

1 file changed

+259
-0
lines changed

1 file changed

+259
-0
lines changed

BinarySeachTree/BST.cpp

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
#include <iostream>
2+
#include <queue>
3+
4+
using namespace std;
5+
6+
7+
class EmptyBstException:exception{};
8+
class NodeNotFoundException:exception{};
9+
10+
class BST
11+
{
12+
private:
13+
struct Node
14+
{
15+
int data;
16+
struct Node *left, *right;
17+
};
18+
19+
public:
20+
struct Node *root;
21+
BST();
22+
BST(int data);
23+
24+
void insert(int data);
25+
void del(int data);
26+
27+
void levelorder(struct Node *t);
28+
void preorder(struct Node *t);
29+
void inorder(struct Node *t);
30+
void postorder(struct Node *t);
31+
32+
};
33+
34+
BST::BST()
35+
{
36+
this->root = NULL;
37+
}
38+
39+
BST::BST(int data)
40+
{
41+
this->root = new Node;
42+
this->root->data = data;
43+
this->root->left = NULL;
44+
this->root->right = NULL;
45+
}
46+
47+
void BST::insert(int data)
48+
{
49+
struct Node *temp, *p, *q;
50+
temp = new Node;
51+
p = NULL;
52+
q = this->root;
53+
54+
temp->data = data;
55+
temp->left = NULL;
56+
temp->right = NULL;
57+
58+
if(q== NULL){
59+
this->root = temp;
60+
}
61+
else{
62+
while(q!= NULL){
63+
p = q;
64+
if(data < q->data){
65+
q = q->left;
66+
}
67+
else{
68+
q = q->right;
69+
}
70+
}
71+
72+
if(data < p->data){
73+
p->left = temp;
74+
}
75+
else{
76+
p->right = temp;
77+
}
78+
79+
}
80+
}
81+
82+
void BST::del(int data)
83+
{
84+
struct Node *temp, *p, *q;
85+
86+
p = NULL;
87+
q = this->root;
88+
89+
try{
90+
if(q == NULL)
91+
throw EmptyBstException();
92+
93+
if(data == this->root->data){
94+
q = p = this->root;
95+
}
96+
97+
while(q != NULL && q->data != data){
98+
p = q;
99+
if(data < q->data){
100+
q = q->left;
101+
}
102+
else{
103+
q = q->right;
104+
}
105+
}
106+
107+
108+
109+
if(q == NULL)
110+
throw NodeNotFoundException();
111+
112+
if(q->left == NULL){
113+
temp = q->right;
114+
delete q;
115+
p->left = temp;
116+
}
117+
else if(q->right == NULL){
118+
temp = q->left;
119+
delete q;
120+
p->right = temp;
121+
}
122+
else
123+
{
124+
struct Node *t = NULL;
125+
temp = q->right;
126+
127+
while(temp->left != NULL){
128+
t = temp;
129+
temp = temp->left;
130+
131+
}
132+
q->data = temp->data;
133+
134+
if(t != NULL){
135+
t->left = temp->right;
136+
delete temp;
137+
}
138+
else{
139+
q->right = temp->right;
140+
}
141+
142+
}
143+
144+
cout << "\nDeleted " << data << endl;
145+
146+
}
147+
148+
catch(EmptyBstException e){
149+
printf("\n\t\t////// Unable to delete a node(%d) in empty bst //////\n", data);
150+
}
151+
152+
catch(NodeNotFoundException e){
153+
printf("\n\t\t///// Node(%d) Not found /////\n", data);
154+
}
155+
156+
}
157+
158+
void BST::levelorder(struct Node *t)
159+
{
160+
cout << "\nLevel order Traversal: ";
161+
162+
queue<struct Node*> q;
163+
q.push(this->root);
164+
165+
while(!q.empty()){
166+
167+
struct Node *temp = q.front();
168+
169+
if(temp!= NULL) cout << temp->data << " ";
170+
if(!q.empty()) q.pop();
171+
if(temp->left != NULL) q.push(temp->left);
172+
if(temp->right != NULL) q.push(temp->right);
173+
174+
}
175+
176+
cout << endl;
177+
178+
}
179+
180+
void BST::preorder(struct Node* t)
181+
{
182+
if(t == NULL){
183+
return;
184+
}
185+
186+
cout << t->data << " ";
187+
preorder(t->left);
188+
preorder(t->right);
189+
190+
}
191+
192+
void BST::inorder(struct Node* t)
193+
{
194+
if(t == NULL){
195+
return;
196+
}
197+
198+
inorder(t->left);
199+
cout << t->data << " ";
200+
inorder(t->right);
201+
}
202+
203+
void BST::postorder(struct Node* t)
204+
{
205+
if(t == NULL){
206+
return;
207+
}
208+
209+
postorder(t->left);
210+
postorder(t->right);
211+
cout << t->data << " ";
212+
}
213+
214+
// struct Node* BST::maxOfBst(struct Node *t)
215+
// {
216+
// struct Node *temp = t;
217+
// while(temp->right != NULL){
218+
// temp = temp->right;
219+
// }
220+
// return temp;
221+
// }
222+
223+
224+
int main()
225+
{
226+
BST myBst;
227+
228+
myBst.insert(30);
229+
myBst.insert(25);
230+
myBst.insert(60);
231+
myBst.insert(20);
232+
myBst.insert(10);
233+
myBst.insert(70);
234+
myBst.insert(15);
235+
myBst.insert(22);
236+
myBst.insert(50);
237+
238+
myBst.del(10);
239+
myBst.del(100);
240+
myBst.del(30);
241+
242+
243+
cout << "\nPreorder Traversal : ";
244+
myBst.preorder(myBst.root);
245+
cout << endl;
246+
247+
cout << "\nInorder Traversal : ";
248+
myBst.inorder(myBst.root);
249+
cout << endl;
250+
251+
cout << "\nPostorder Traversal : " ;
252+
myBst.postorder(myBst.root);
253+
cout << endl;
254+
255+
myBst.levelorder(myBst.root);
256+
257+
// cout << myBst.root->data;
258+
return 0;
259+
}

0 commit comments

Comments
 (0)