-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbt_insert_delete.cpp
208 lines (164 loc) · 4.35 KB
/
bt_insert_delete.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
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
// Insert/ Delete node in BT by level order
// Note BST has order but BT has no order among elements
//
// Created by @altanai on 31/03/21.
//
#include <iostream>
#include<queue>
using namespace std;
struct node{
int data;
struct node* left ;
struct node* right;
node(int value){
data=value;
left=NULL;
right=NULL;
}
};
// ----------------------- utility function start
void printBT(const std::string& prefix, const struct node* root, bool isLeft)
{
if( root != nullptr )
{
std::cout << prefix;
std::cout << (isLeft ? "├──" : "└──" );
std::cout << root->data << std::endl;
printBT( prefix + (isLeft ? "│ " : " "), root->left, true);
printBT( prefix + (isLeft ? "│ " : " "), root->right, false);
}
}
void printBT(const struct node* root)
{
printBT("", root, false);
}
/* Inorder traversal of a binary tree */
void inorder(node* temp){
if (temp == NULL)
return;
inorder(temp->left);
cout << temp->data << ' ';
inorder(temp->right);
}
// -----------------------
// Insert in level order
// iterative level order traversal in BT of the given tree using queue to find a node with a null child
node* insert_levelorder(node* root, int newdata){
if(root == NULL){
root = new node(newdata);// If the tree is empty, assign new node address to root
return root;
}
queue<node*> q; // empty queue
q.push(root);
while(!q.empty()){
node* curr = q.front();
q.pop();
if(curr->left) q.push(curr->left);
else{
curr->left = new node(newdata);
return root;
}
if(curr->right) q.push(curr->right);
else{
curr->right = new node(newdata);
return root;
}
}
}
// TBD : XX NOT working ok for innner nodes , woks ok for leave node
// tree shrinks from bottom
// deleted node is replaced by bottom most and rightmost node
// not a bst, hence order is of no consequence
// delete the deepest rightmost node
void deleteDeepest(node* root, node* delnode){
queue<node*> q; // empty queue
q.push(root);
while(!q.empty()){
node* curr = q.front();
q.pop();
if(curr == delnode){
curr = NULL;
delete(delnode);
return;
}
if(curr->left){
if(curr-> left == delnode){
delete(delnode);
return;
}else{
q.push(curr->left);
}
}
if(curr->right){
if(curr->right == delnode){
curr->right=NULL;
delete(delnode);
return;
}else{
q.push(curr->right);
}
}
}
}
// find nod to be deleted
node* delete_levelorder(node* root, int deldata){
if(root == NULL){
return root;
}
node* delnode;
queue<node*> q; // empty queue
q.push(root);
while(!q.empty()){
node* curr = q.front();
q.pop();
if(curr->data == deldata) delnode=curr;
if(curr->left) q.push(curr->left);
if(curr->right) q.push(curr->right);
}
if(delnode !=NULL ){ // if the node to be deleted is found , then replace with deepest and rightmost node
int temp = delnode->data;
deleteDeepest(root, delnode);
delnode->data = temp;
}
return root;
}
int main(){
struct node* root = new node(1);
root->left = new node(2);
// root->left->left = new node(3);
root->left->right = new node(4);
root->right = new node(5);
root->right->left = new node(6);
cout << "\n Before \n";
printBT(root);
int a1=0;
insert_levelorder(root,a1);
cout << "\n After inserting " << a1 <<"\n";
printBT(root);
int a2=2;
delete_levelorder(root,a2);
cout << "\n After deleting " << a2 <<"\n";
printBT(root);
return 0;
}
// g++ bt_insert_delete.cpp -o bt_insert_delete.out
// ./bt_insert_delete.out
// Outout:
// Before
// └──1
// After
// └──1
// ├──11
// Before
// └──1
// ├──2
// │ └──4
// └──5
// ├──6
// After
// └──1
// ├──2
// │ ├──11
// │ └──4
// └──5
// ├──6