-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinary_Search_Tree_4.cpp
More file actions
159 lines (140 loc) · 3.04 KB
/
Copy pathBinary_Search_Tree_4.cpp
File metadata and controls
159 lines (140 loc) · 3.04 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
/*
PRIYANSHU IS A PEACEFULL SOUL
*/
#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define ff first
#define ll long long
#define ss second
using namespace std;
class node
{
public:
int data;
node *left;
node *right;
node(int val)
{
data = val;
left = NULL;
right = NULL;
}
};
node*inorderSucc(node*root){
node*curr=root;
while (curr&&curr->left!=NULL)
{
curr=curr->left;
}
return curr;
}
node* minValue(node* root){
node* temp = root;
while(temp && temp->left!=NULL){
temp=temp->left;
}
return temp;
}
bool ifNodeExists(struct node* node, int key)
{
if (node == NULL)
return false;
if (node->data == key)
return true;
bool res1 = ifNodeExists(node->left, key);
if(res1) return true;
bool res2 = ifNodeExists(node->right, key);
return res2;
}
node* deleteBST(node* root,int x){
if(root==NULL){
return root;
}
if(ifNodeExists(root, x)){
if(x<root->data){
root->left=deleteBST(root->left,x);
}else if(x>root->data){
root->right=deleteBST(root->right,x);
}else{
if(root->left==NULL && root->right==NULL){
return NULL;
}else if(root->left==NULL){
node* temp=root->right;
free(root);
return temp;
}else if(root->right==NULL){
node* temp=root->left;
free(root);
return temp;
}
node* temp=minValue(root->right);
root->data=temp->data;
root->right=deleteBST(root->right,temp->data);
}
return root;
}else{
cout<<"Node "<<x<<" is not found in the tree";
exit(0);
}
}
node* insertBST(node*root,int val){
if(root==NULL){
return new node(val);
}
if(val<root->data){
root->left=insertBST(root->left,val);
}else{
root->right=insertBST(root->right,val);
}
return root;
}
void inorder(node*root){
if(root==NULL){
return;
}
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
void preorder(node*root){
if(root==NULL){
return;
}
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
int main()
{
node *root = NULL;
int n;cin>>n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin>>arr[i];
}
root=insertBST(root,arr[0]);
for (int i = 1; i < n; i++)
{
insertBST(root,arr[i]);
}
int m;cin>>m;
int arr2[m];
for (int i = 0; i < m; i++)
{
cin>>arr2[i];
}
root=deleteBST(root,arr2[0]);
for (int i = 1; i < m; i++)
{
deleteBST(root,arr2[i]);
}
inorder(root);
cout<<endl;
preorder(root);
return 0;
}