-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLAB7.cpp
More file actions
196 lines (169 loc) · 4.32 KB
/
LAB7.cpp
File metadata and controls
196 lines (169 loc) · 4.32 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
#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 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<<" minimum from 5:"<<
cout<<e.find_min(e.serch(5))->data;
cout<<" minimum from 7:"<<
cout<<e.find_min(e.serch(7))->data;
e.delet(32);
cout<<"Deleted 32, new display: ";
e.display();
return 0;
}