-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBST.cpp
More file actions
145 lines (132 loc) · 2.96 KB
/
BST.cpp
File metadata and controls
145 lines (132 loc) · 2.96 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
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct tree{
struct tree* left;
struct tree* right;
struct tree* parent;
int info;
}node;
node* create(node* &root,int x){ //Used for both creation and insertion of BST
node* z=new node;
z->info=x;
z->left=z->right=z->parent=NULL;
node* current=new node;
current=root;
node* hero=new node;
hero=NULL;
while(current!=NULL){
hero=current;
if(z->info<current->info)
current=current->left;
else
current=current->right;
}
if(hero==NULL)
root=z;
else if(z->info < hero->info){
hero->left=z;
z->parent=hero;
}
else{
hero->right=z;
z->parent=hero;
}
return root;
}
/*void transplant(node* &root , node* &u , node* &v){ //Pushpendra's version of transplant()
if(u->parent==NULL){
root=v;
}
else if(u==u->parent->left && v!=NULL){
u->parent->left=v;
v->parent=u->parent;
}
else if(u==u->parent->left && v==NULL)
u->parent->left=v;
else if(u==u->parent->right && v!=NULL){
u->parent->right=v;
v->parent=u->parent;
}
else if(u==u->parent->right && v==NULL)
u->parent->right=v;
}*/
void transplant(node* &root , node* &u , node* &v){ //Cormen's version of transplant()
if(u->parent==NULL)
root=v;
else if(u==u->parent->left)
u->parent->left=v;
else if(u==u->parent->right)
u->parent->right=v;
if(v!=NULL)
v->parent=u->parent;
}
node* find_min(node* &root){
node* temp=root;
if(temp->left==NULL)
return temp;
else
return find_min(temp->left);
}
node* search(node* &root,int x){
node* temp=root;
while(temp!=NULL){
if(temp->info == x)
return temp;
else if(x < temp->info)
temp=temp->left;
else if(x > temp->info)
temp=temp->right;
}
cout<<"\nelement is not in Tree \n";
}
/*node* search(node* root,int x){ //Recursive search
node* temp=root;
if(x < temp->info)
return search(temp->left,x);
else if(x==temp->info)
return temp;
else if(x > temp->info)
return search(temp->right,x);
}*/
void del(node* &root,node* &p){
if(p->left==NULL && p->right!=NULL)
transplant(root,p,p->right);
else if(p->right==NULL && p->left!=NULL)
transplant(root,p,p->left);
else{
node* y=find_min(p->right);
if(y->parent!=p){
transplant(root,y,y->right);
y->right=p->right;
y->right->parent=y;
transplant(root,p,y);
y->left=p->left;
y->left->parent=y;
}
else{
transplant(root,p,y);
y->left=p->left;
y->left->parent=root;
}
}
}
void inorder(node* &p){
if(p!=NULL){
inorder(p->left);
cout<<p->info<<" ";
inorder(p->right);
}
}
int main(){
node* root=new node;
root=NULL;
for(int i=1;i<=10;i++)
root=create(root,2*i*i*i-7*i*i-i+7);
// root=create(root,i);
node* temp=search(root,1);
// node* temp=search(root,1);
del(root,temp);
inorder(root);
cout<<"\n"<<temp->info;
}