-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst_insert_delete.cpp
145 lines (120 loc) · 3.26 KB
/
bst_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
// Insert / Delete node in BST by level order
//
// Created by @altanai on 31/03/21.
//
#include<iostream>
using namespace std;
struct node{
int data;
struct node* left;
struct node* right;
node(int value){
data=value;
left= NULL;
right = NULL;
}
};
// Search(data)
// Begin
// If node == null or node->data == val
// return node
// If(data >root->data)
// Search(node->right)
// Else If(data < root->data)
// Search(node->left,data)
// Return node;
// end
node* searchbst(struct node* root, int val){
if(root==NULL || root->data == val)
return root;
if(val > root->data)
searchbst(root->right, val);
else if( val < root->data)
searchbst(root->left, val);
}
// Insert(data)
// Begin
// If node == null
// Return createNode(data)
// If(data >root->data)
// Node->right = insert(node->right,data)
// Else If(data < root->data)
// Node->left = insert(node->left,data)
// Return node;
// end
node* insertbst(struct node* root, int val){
if(root == NULL)
root = new node(val);
if(val > root->data)
root->right = insertbst(root->right, val);
else if(val < root->data)
root->left = insertbst(root->left, val);
return root;
}
node* deletebst(struct node* root, int val){
if(root->data == val && root->left == NULL && root->right == NULL)
root = NULL;
if(val > root->data)
root->right = deletebst(root->right, val);
else if(val < root->data)
root->left = deletebst(root->left, val);
return root;
}
// ----------------------- 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);
}
int main(){
struct node* root = new node(5);
root->left = new node(1);
root->right = new node(7);
printBT(root);
// root = insertbst(root,11);
// printBT(root);
// root = insertbst(root,2);
// printBT(root);
// root = insertbst(root,3);
// printBT(root);
struct node* snode = searchbst(root,1);
printBT(snode); // Will print seg fault if u look up element no presnet in BST ,todo : handle case for errors
cout<< snode->data;
// When the tobe deleted node is a Leaf Node
root = deletebst(root,7);
printBT(root);
// When the tobe deleted node has 1 child
// When the tobe deleted node has 2 children
return 0;
}
// g++ bst_insert_delete.cpp -o bst_insert_delete.out
// ./bst_insert_delete.out
// outout insert
// └──5
// ├──1
// └──7
// └──5
// ├──1
// └──7
// └──11
// └──5
// ├──1
// │ └──2
// └──7
// └──11
// └──5
// ├──1
// │ └──2
// │ └──3
// └──7
// └──11