-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary_search_tree.cpp
More file actions
210 lines (185 loc) · 4.23 KB
/
binary_search_tree.cpp
File metadata and controls
210 lines (185 loc) · 4.23 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
#include <iostream>
using namespace std;
#include <queue>
class Node {
public:
Node* left;
Node* right;
int x;
Node(){
left = NULL;
right = NULL;
}
Node(int k){
left = NULL;
right = NULL;
x = k;
}
};
class binary_search_tree{
public:
Node* Root;
binary_search_tree()
{
Root = NULL;
}
void insert(int k) {
if (Root == NULL) {
Root = new Node(k);
return;
}
Node* sub = Root;
Node* temp = new Node(k);
int c = 0;
while ((sub->right != NULL) || (sub->left != NULL)) {
if (k <= sub->x){
if(sub->left == NULL) {sub->left = temp;c=1; }
else {sub = sub->left;}
}
else{
if(sub->right == NULL) {sub->right = temp;c=1;}
else {sub = sub->right;}
}
}
if (c!=1){
if (k<=sub->x) {sub->left = temp;}
else {sub->right = temp;}
}
}
void check(int k){
Node* sub = Root;
int c = 0;
if(sub->x == k){cout << "found" << endl;return; }
while ((sub->right) || (sub->left)){
if(k<=sub->x){sub = sub->left;}
else {sub = sub->right;}
if(k == sub->x) {cout << "found" << endl;return;}
}
cout << "not found" << endl;
}
void max() {
if(Root == NULL){cout << "emtpy tree " << endl;return;}
Node* sub = Root;
int c = sub->x;
while(sub->right != NULL){
sub = sub->right;
if(c < sub->x) {c = sub->x;}
}
cout <<"max number of the tree is :" << c << endl;
}
void min() {
if(Root == NULL){cout << "emtpy tree " << endl;return;}
Node* sub = Root;
int c = sub->x;
while(sub->left != NULL){
sub = sub->left;
if(c > sub->x) {c = sub->x;}
}
cout <<"min number of the tree is : " << c << endl;
}
int height(Node* sub) {
if(sub == NULL) {return -1;}
else if ( height(sub->left) > height(sub->right) ){
return height(sub->left) + 1;
}
else {
return height(sub->right) + 1;
}
}
void linear_traversal (){
queue<Node*> q;
q.push(Root);
cout << "starting linear_traversal of tree: " << endl;
while(!q.empty()){
Node* sub = q.front();
if(sub->left != NULL) {q.push(sub->left);}
if(sub->right != NULL) {q.push(sub->right);}
cout << sub->x << endl;
q.pop();
}
cout << "ended" << endl;
}
void pre_order(Node* sub){
if(sub == NULL) {return;}
cout << sub->x << endl;
pre_order(sub->left);
pre_order(sub->right);
}
void in_order(Node* sub){
if(sub == NULL) {return;}
in_order(sub->left);
cout << sub->x << endl;
in_order(sub->right);
}
void post_order(Node* sub){
if(sub == NULL) {return;}
post_order(sub->left);
post_order(sub->right);
cout << sub->x << endl;
}
Node* min_root(Node* sub) {
if(sub == NULL){return NULL;}
sub = sub->right;
int c = sub->x;
Node* temp = new Node();
while(sub->left != NULL){
sub = sub->left;
if(c > sub->x) {c = sub->x; temp = sub;}
}
return temp;
}
Node* delete_it(Node* sub, int k){
if(k < sub->x) {sub->left = delete_it(sub->left,k);}
else if(k > sub->x) {sub->right = delete_it(sub->right,k);}
else{
if(sub->left == NULL){
Node* temp = sub->right;
delete sub;
return temp;}
else if(sub->right == NULL){
Node* temp = sub->left;
delete sub;
return temp;}
else{
Node* temp = min_root(sub);
int m = temp->x;
sub->x = m;
sub->right = delete_it(temp, m);
}
}
return sub;
}
};
int main(){
binary_search_tree b;
int n;
cout << "enter the count of numbers " << endl;
cin >> n;
for(int i=0;i<n;i++){
int k;
cout << "enter it " << endl;
cin >> k;
b.insert(k);
}
cout << "enter the number to be searched " << endl;
int c;
cin >> c;
b.check(c);
b.max();
b.min();
int h = b.height(b.Root); cout << "height of the tree is: " << h << endl;
b.linear_traversal();
cout << "pre order traversal is: " << endl;
b.pre_order(b.Root);
cout << "in order traversal is: " << endl;
b.in_order(b.Root);
cout << "post order traversal is: " << endl;
b.post_order(b.Root);
int d;
cout << "enter the number you want to delete..?" << endl;
cin >> d;
//b.Root = b.delete_it(b.Root,d);
b.delete_it(b.Root,d);
b.linear_traversal();
return 0;
}