Skip to content

Commit d03f26c

Browse files
authored
BST
1 parent d27985d commit d03f26c

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed

Diff for: bst.cpp

+205
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
#include <iostream>
2+
#include<queue>
3+
using namespace std;
4+
5+
class Node{
6+
public:
7+
int data;
8+
Node* right;
9+
Node* left;
10+
11+
Node(int d){
12+
this->data = d;
13+
this->left = NULL;
14+
this->right = NULL;
15+
}
16+
};
17+
18+
void levelOrderTraversal(Node* root) {
19+
queue<Node*> q;
20+
q.push(root);
21+
q.push(NULL);
22+
23+
while(!q.empty()) {
24+
Node* temp = q.front();
25+
q.pop();
26+
27+
if(temp == NULL) {
28+
//purana level complete traverse ho chuka hai
29+
cout << endl;
30+
if(!q.empty()) {
31+
//queue still has some child ndoes
32+
q.push(NULL);
33+
}
34+
}
35+
else{
36+
cout << temp -> data << " ";
37+
if(temp ->left) {
38+
q.push(temp ->left);
39+
}
40+
41+
if(temp ->right) {
42+
q.push(temp ->right);
43+
}
44+
}
45+
}
46+
}
47+
48+
void preOrder(Node* root){
49+
if(root == NULL){
50+
return;
51+
}
52+
cout<<root->data<<" ";
53+
preOrder(root->left);
54+
preOrder(root->right);
55+
}
56+
57+
void inOrder(Node* root){
58+
if(root == NULL){
59+
return;
60+
}
61+
62+
inOrder(root->left);
63+
cout<<root->data<<" ";
64+
inOrder(root->right);
65+
}
66+
67+
void postOrder(Node* root){
68+
if(root == NULL){
69+
return;
70+
}
71+
postOrder(root->left);
72+
postOrder(root->right);
73+
cout<<root->data << " ";
74+
}
75+
76+
Node* minVal(Node* root){
77+
Node* temp = root;
78+
79+
while(temp->left != NULL){
80+
temp = temp->left;
81+
}
82+
return temp;
83+
}
84+
85+
Node* maxVal(Node* root){
86+
Node* temp = root;
87+
88+
while(temp->right != NULL){
89+
temp = temp->right;
90+
}
91+
return temp;
92+
}
93+
94+
Node* deleteFromBST(Node* root, int d){
95+
if(root == NULL){
96+
return root;
97+
}
98+
99+
if(root->data == d){
100+
//0 child
101+
if(root->left == NULL && root->right == NULL){
102+
delete root;
103+
return NULL;
104+
}
105+
106+
//1 child
107+
//left
108+
if(root->left != NULL && root->right == NULL){
109+
Node* temp = root->left;
110+
delete root;
111+
return temp;
112+
}
113+
//right
114+
if(root->left == NULL && root->right != NULL){
115+
Node* temp = root->right;
116+
delete root;
117+
return temp;
118+
}
119+
120+
//2child
121+
if(root->left != NULL && root->right != NULL){
122+
int min = minVal(root->right) -> data;
123+
root->data = min;
124+
root->right = deleteFromBST(root->right, min);
125+
return root;
126+
}
127+
}
128+
else if(root->data>d){
129+
root->left = deleteFromBST(root->left,d);
130+
}
131+
else{
132+
root->right = deleteFromBST(root->right,d);
133+
}
134+
}
135+
136+
Node* insertBST(Node* root, int d){
137+
if(root == NULL){
138+
root = new Node(d);
139+
140+
return root;
141+
}
142+
143+
if(d>root->data){
144+
root->right = insertBST(root->right, d);
145+
}
146+
else{
147+
root->left = insertBST(root->left, d);
148+
}
149+
150+
return root;
151+
152+
}
153+
154+
void takeInput(Node* &root){
155+
int data;
156+
cin >> data;
157+
158+
while(data != -1){
159+
root = insertBST(root,data);
160+
cin >> data;
161+
}
162+
}
163+
164+
int main(){
165+
Node* root = NULL;
166+
167+
cout << "Enter data end with -1" << endl;
168+
takeInput(root);
169+
170+
cout << "Level Order Traversal:" << endl;
171+
levelOrderTraversal(root);
172+
173+
cout << "InOrder Traversal:" << endl;
174+
inOrder(root);
175+
176+
cout << endl << "PreOrder Traversal:" << endl;
177+
preOrder(root);
178+
179+
cout << endl << "PostOrder Traversal:" << endl;
180+
postOrder(root);
181+
182+
cout << endl << "Min Value:" << minVal(root) -> data << endl;
183+
184+
cout << endl << "Max Value:" << maxVal(root) -> data << endl;
185+
186+
root = deleteFromBST(root, 10);
187+
188+
cout << "Level Order Traversal:" << endl;
189+
levelOrderTraversal(root);
190+
191+
cout << "InOrder Traversal:" << endl;
192+
inOrder(root);
193+
194+
cout << endl << "PreOrder Traversal:" << endl;
195+
preOrder(root);
196+
197+
cout << endl << "PostOrder Traversal:" << endl;
198+
postOrder(root);
199+
200+
cout << endl << "Min Value:" << minVal(root) -> data<< endl;
201+
202+
cout << "Max Value:" << maxVal(root) -> data << endl;
203+
204+
return 0;
205+
}

0 commit comments

Comments
 (0)