-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateBST.cpp
More file actions
155 lines (135 loc) · 3.06 KB
/
createBST.cpp
File metadata and controls
155 lines (135 loc) · 3.06 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
#include<iostream>
#include<queue>
using namespace std;
class Node{
public:
int data;
Node* left;
Node* right;
Node(int d){
this->data = d;
this->left = NULL;
this->right = NULL;
}
};
void levelOrder(Node *root)
{
queue<Node *> q;
q.push(root);
q.push(NULL);
while (!q.empty())
{
Node *temp = q.front();
q.pop();
if (temp == NULL)
{
cout << endl;
if (!q.empty())
{
q.push(NULL);
}
}
else
{
cout << temp->data << " ";
if (temp->left)
{
q.push(temp->left);
}
if (temp->right)
{
q.push(temp->right);
}
}
}
}
Node* createBST(Node* root, int d){
if(root == NULL){
Node* root = new Node(d);
return root;
}
if(d > root->data){
root->right = createBST(root->right,d);
}
else{
root->left = createBST(root->left,d);
}
return root;
}
int minVal(Node* root){
Node* temp = root;
while(temp->left){
temp = temp->left;
}
return temp->data;
}
int maxVal(Node* root){
Node* temp = root;
while(temp->right){
temp = temp->right;
}
return temp->data;
}
void takeInputAndCreateBST(Node* &root){
cout << "Enter the values of the nodes " << endl;
int data;
cin >> data ;
while(data != -1){
root = createBST(root,data);
cin >> data;
}
}
Node* deleteFromBST(Node* root, int val){
if(root == NULL){
return root;
}
if(val == root->data){
// 0 Node
if(root -> left == NULL && root->right == NULL){
delete root;
return NULL;
}
// 1 Node
else if(root -> left != NULL && root->right == NULL){
Node* temp = root->left;
delete root;
return temp;
}
else if(root -> left == NULL && root->right != NULL){
Node* temp = root->right;
delete root;
return temp;
}
//2 Nodes
else if(root -> left != NULL && root->right != NULL){
int mini = minVal(root->right);
root->data = mini;
return deleteFromBST(root->right,mini);
}
}
else if(val < root->data){
root->left= deleteFromBST(root->left,val);
return root;
}
else{
root->right = deleteFromBST(root->right,val);
return root;
}
}
int main(int argc, char const *argv[])
{
Node* root = NULL;
takeInputAndCreateBST(root);
cout << "Printing the tree " << endl;
levelOrder(root);
// 50 20 70 10 30 90 110 -1
cout << "Minimum value of the tree is " << minVal(root) << endl;
cout << "Maximum value of the tree is " << maxVal(root) << endl;
cout << "Enter value to delete " << endl;
int val;
cin >> val ;
root = deleteFromBST(root,val);
cout << "Printing the tree " << endl;
levelOrder(root);
return 0;
}