-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchingTree.cpp
More file actions
223 lines (185 loc) · 4.33 KB
/
BinarySearchingTree.cpp
File metadata and controls
223 lines (185 loc) · 4.33 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include<conio.h>
#include<stdio.h>
#include<iostream>
using namespace std;
class BST{
private:
struct node{
node *left;
int item;
node *right;
};
node *root;
public:
~BST(){
root=NULL;
}
node* getrootAddress(){
return root;
}
void Insertion(int data){
node *t=new node;
t->item=data;
t->left=NULL;
t->right=NULL;
node *n=root;
if(root==NULL){
root=t;
}
else{
while(1){
if(data==n->item)
delete t;
else if(data<n->item){
if(n->left==NULL){
n->left=t;
break;
}
else{
n=n->left;
}
}
else{
if(n->right==NULL){
n->right=t;
break;
}
else{
n=n->right;
}
}
}
}
}
void preorder(node* ptr);
void inorder(node* ptr);
void postorder(node* ptr);
void deletion(int data,node* ptr);
/*
node *minvaluenode(node *n){
node *curr=n;
while(curr->left!=NULL)
curr=curr->left;
return curr;
}
node* deletion(int data,node *r){
node *temp;
if (r==NULL)
{
return r;
}
else if(data<r->item){
r->left=deletion(data,r->left);
}
else if(data>r->item){
r->right=deletion(data,r->right);
}
else{
if(r->left==NULL){
temp=r->right;
delete r;
return temp;
}
else if(r->right==NULL){
temp=r->left;
delete r;
return temp;
}
else{
temp=minvaluenode(r->right);
r->item=temp->item;
r->right=deletion(temp->item,r->right);
}
}
return r;
} */
};
void BST::preorder(node *ptr){
if(ptr!=NULL){
cout<<ptr->item<<" ";
preorder(ptr->left);
preorder(ptr->right);
}
}
void BST::inorder(node *ptr){
if(ptr!=NULL){
inorder(ptr->left);
cout<<ptr->item<<" ";
inorder(ptr->right);
}
}
void BST::postorder(node *ptr){
if(ptr!=NULL){
postorder(ptr->left);
postorder(ptr->right);
cout<<ptr->item<<" ";
}
}
void BST::deletion(int data,node *ptr){
ptr=root;
node *parptr=NULL; //(*parptr means parent pointer which act as tail pointer to traversing pointer i.e. *ptr)
if(ptr==NULL){
cout<<"\n NULL TREE !";
}
else{
// node *suc; // succesor pointer
// node *parsuc=NULL; // tail pointer to succesor pointer
int temp;
while(1){
if(data==ptr->item){
break;
}
else if(data<ptr->item){
parptr=ptr;
ptr=ptr->left;
}
else{
parptr=ptr;
ptr=ptr->right;
}
}
if(ptr->left==NULL && ptr->right ==NULL){
if(data<parptr->item){
parptr->left==NULL;
delete ptr;
}
else{
parptr->right==NULL;
delete ptr;
}
}
else if(ptr->left){
}
while (1)
{
if(suc->left==NULL){
break;
}
parsuc=suc;
suc=suc->left;
}
temp=ptr->item;
ptr->item=suc->item;
suc->item=temp;
if(suc->right!=NULL){
while(1){
if(suc->right==NULL && suc->left==NULL)
break;
parsuc=suc;
suc=suc->right;
}
}
}
}
int main(){
BST obj;
obj.Insertion(50);
obj.Insertion(30);
obj.Insertion(20);
obj.Insertion(40);
obj.Insertion(80);
obj.Insertion(70);
obj.Insertion(60);
obj.inorder(obj.getrootAddress());
return 0;
}