forked from lakshyasaxena/CP-Algorithms-in-C_plus-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Y__INSERTION_AND_DELETION_FROM_BINARY_SEARCH_TREE.cpp
182 lines (175 loc) · 5.37 KB
/
Y__INSERTION_AND_DELETION_FROM_BINARY_SEARCH_TREE.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
#include <bits/stdc++.h>
#define in input_nodes
#define pb push_back
using namespace std;
typedef long long ll;
//
// Given a Binary Search Tree (BST)
// We will perform basic operations on BST
// 1. Insert node in BST
// 2. Delete node from BST
//
// For reference, consider the BST given below
//
//
// root -> 60
// / \
// / \
// _/ \_
// / \
// _/ \_
// / \
// / \
// 30 100
// / \ / \
// / \ / \
// / \ / \
// 5 50 80 120
// /\ /\ / / \
// / \ / \ / / \
// 1 25 40 55 65 110 130
//
//
//
class Binary_Search_Tree_Node
{
public:
ll data;
Binary_Search_Tree_Node *left=NULL;
Binary_Search_Tree_Node *right=NULL;
};
typedef Binary_Search_Tree_Node bst;
//*********************************************************************************************
bst* insert_in_bst(bst *root,bst *temp)
{
if(root==NULL) // base case
{
root=temp;
return root;
}
if(root->data > temp->data) // left case
{
if(root->left==NULL)
{
root->left=temp;
return root;
}
root->left=insert_in_bst(root->left,temp);
}
else // right case
{
if(root->right==NULL)
{
root->right=temp;
return root;
}
root->right=insert_in_bst(root->right,temp);
}
return root;
}
//*********************************************************************************************
//*********************************************************************************************
ll find_in_order_successor(bst *root) // use when we have two children of node to be deleted
{
while(root->left!=NULL)
{
root=root->left;
}
return (root->data);
}
bst* delete_from_bst(bst *root,ll val)
{
if(root==NULL)
{
cout<<val<<" is not present in given BST\n";
return root;
}
if(root->data > val) // left case
{
root->left=delete_from_bst(root->left,val);
}
else if(root->data < val) // right case
{
root->right=delete_from_bst(root->right,val);
}
else // value found
{
if(root->left==NULL) // case of one child or no child
{
bst *temp=new(bst);
temp=root->right;
delete(root);
cout<<"Value deleted from BST\n";
return temp;
}
else if(root->right==NULL) // case of one child or no child
{
bst *temp=new(bst);
temp=root->left;
delete(root);
cout<<"Value deleted from BST\n";
return temp;
}
else // case of two children , we will find in_order successor
{
ll in_order_successor_value=find_in_order_successor(root->right);
root->data=in_order_successor_value;
//cout<<"In_order successor value = "<<in_order_successor_value<<"\n";
root->right=delete_from_bst(root->right,in_order_successor_value);
return root;
}
}
}
//*********************************************************************************************
void preorder_traversal(bst *root)
{
if(root==NULL) return;
cout<<root->data<<" ";
preorder_traversal(root->left);
preorder_traversal(root->right);
}
void inorder_traversal(bst *root)
{
if(root==NULL) return;
inorder_traversal(root->left);
cout<<root->data<<" ";
inorder_traversal(root->right);
}
int main()
{
ios_base::sync_with_stdio(false);
bst *root=NULL;
ll nodes=14;
vector <ll> input_nodes;
in.pb(60); in.pb(30); in.pb(100); in.pb(5); in.pb(50); in.pb(80); in.pb(120); in.pb(1);
in.pb(25); in.pb(40); in.pb(55); in.pb(65); in.pb(110); in.pb(130);
for(int i=0;i<in.size();i++)
{
bst *temp=new(bst);
temp->data=in[i];
root=insert_in_bst(root,temp);
}
cout<<"Pre-Order Tree traversal\n";
preorder_traversal(root);
cout<<"\n\nIn-Order Tree traversal\n";
inorder_traversal(root);
cout<<"\n";
//**************************************************************************************
ll node;
cout<<"\nDelete 2 from BST\n";
node=2;
root=delete_from_bst(root,node);
cout<<"\nDelete 80 from BST\n";
node=80;
root=delete_from_bst(root,node);
cout<<"\nIn-Order Tree traversal\n";
inorder_traversal(root);
cout<<"\n\nDelete 60 from BST\n";
node=60;
root=delete_from_bst(root,node);
cout<<"\nIn-Order Tree traversal\n";
inorder_traversal(root);
cout<<"\n\n";
//**************************************************************************************
return 0;
}