-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLAB8.cpp
238 lines (206 loc) · 6.1 KB
/
LAB8.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <iostream>
using namespace std ;
class Node{
public:
int data;
Node * left;
Node * right;
Node * parent;
Node(int value){
data = value;
left= NULL;
right= NULL;
parent= NULL;
}
};
class BiST{
public:
Node * root;
BiST(){
root= NULL;
}
void insrt(int value){
if(root == NULL)
root = new Node(value);
else insrtkro(root,value);
}
void insrtkro(Node * curr, int value){
//compare the curr data with value
if (value < curr->data){
// if value is less than curr, move left and recall
if(curr->left==NULL){
curr->left= new Node(value);
curr->left->parent= curr;
return;
}
else insrtkro(curr->left, value);
}
else{
//Else move right and call insert
if(curr->right==NULL){
curr->right = new Node(value);
curr->right->parent= curr;
return;
}
else insrtkro(curr->right, value);
}
}
void display(){
if(root== NULL)
cout<<"Tree has nothing to show."<<endl;
else display2(root);
}
void display2(Node * current){
//display left
if (current->left != NULL)
display2(current->left);
//display current
cout<<current->data<<",";
//display right
if(current->right != NULL)
display2(current->right);
else return;
}
Node * serch(int value){
Node * add = serch2(root,value);
if(add==NULL)
cout<<"Element not present"<<endl;
return add;
}
Node * serch2(Node * current, int value){
//if value found or reached the end
if (current->data == value)
return current;
else if (value < current->data){
if(current-> left == NULL)
return NULL;
return serch2 (current->left,value);
}
else if (value > current->data){
if(current-> right == NULL)
return NULL;
return serch2(current->right, value);
}
}
Node * find_min(Node * curr){
if(curr->left != NULL){
return find_min(curr ->left);
}
return curr;
}
void replace_with_parent(Node* A, Node* B){
//if A is not root
if(A!=root){
//if A is left child: add B
if(A->parent->left==A) A->parent->left=B;
//if A is right child: add B
else if(A->parent->right==A) A->parent->right=B;
//connect B back
if(B!=NULL) B->parent=A->parent;
//disconnect A
A->parent=NULL;
}
//if root
else {
root=B;
}
}
void delet(int value){
//search node with the value and store the address of node
Node* remove = serch(value);
//if not present
if(remove==NULL){
cout << value << "Such data not present" << endl;
}
//if present
else{
//replacement node
Node* replace ;
//if the delet node has two children
if(remove->left!=NULL and remove->right!=NULL){
//find replacement
replace = find_min(remove->right);
//swap values
int temp=replace->data;
replace->data=remove->data;
remove->data=temp;
//remove the swapped node
remove=replace;
}
//else{
//if only left child
if(remove->right==NULL){
replace = remove->left;
}
//if only right child
else if(remove->left==NULL){
replace = remove->right;
}
//if no child, simply delete
//replace
replace_with_parent(remove, replace);
//if root is removed , replace is new root
if(root == remove) root = replace;
//}
delete remove;
}
}
int countnode(){ //to call from main
return cont(root); //calls recursive fun
}
int cont(Node * curr){ //recursive function
if (curr==NULL) return 0; // stops recursion
return 1 + cont(curr->left) + cont(curr->right); //increases counting
}
int maxm(int a, int b){ //fun to find maximum of two integers
if (a>b) return a;
else return b;
}
int heightnode(){ //fun to call from main
return height(root); //calls recursive fun
}
int height(Node * curr){ //recursive fun
if (curr==NULL) return 0; //stops recursion
return 1 + maxm(height(curr->left),height(curr->right)); //inc counting
}
int rangeSearch(int k1, int k2){ //to call from main with two parameters
return rangeSearch2(root, k1, k2); //also passes root to recursive fun
}
int rangeSearch2(Node*curr, int k1, int k2){
if(curr!=NULL){ //if the node has child
if(curr->data>k1 and curr->data<k2){ //data in the node is within the range
cout<<curr->data<<", "; //data printed
return (1+rangeSearch2(curr->left,k1,k2)+rangeSearch2(curr->right,k1,k2)); //counting continues with its left and right subtree
}
else
return rangeSearch2(curr->left,k1,k2)+rangeSearch2(curr->right,k1,k2); //elements in the range of its left and right subtree are counted
}
else return 0; //if end has came
}
};
int main(){
BiST e;
e.insrt(5);
e.insrt(3);
e.insrt(4);
e.insrt(7);
e.insrt(1);
e.insrt(8);
e.insrt(6);
e.insrt(9);
e.insrt(32);
e.display();
cout<<endl;
cout<<" minimum from 5:"<<
cout<<e.find_min(e.serch(5))->data<<endl;
cout<<" minimum from 7:"<<
cout<<e.find_min(e.serch(7))->data<<endl;
e.delet(32);
cout<<"Deleted 32, New display: ";
e.display();
cout<<endl;
cout<<"No of nodes= "<<e.countnode()<<endl;
cout<<"Height of tree= "<<e.heightnode()<<endl;
cout << "No of nodes in range = " << e.rangeSearch(5,8) << endl;
return 0;
}