-
Notifications
You must be signed in to change notification settings - Fork 26
/
bst-constuction.cpp
174 lines (148 loc) · 3.67 KB
/
bst-constuction.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
#pragma GCC optimize("Ofast")
#include <algorithm>
#include <bitset>
#include <deque>
#include <iostream>
#include <iterator>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;
void abhisheknaiidu()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
struct BstNode
{
int data;
BstNode* left;
BstNode* right;
};
BstNode* rootptr = NULL;
BstNode* GetNewNode(int data) {
BstNode* newNode = new BstNode(); // returns back the address the new node
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode; // address of newNode
}
void insert(BstNode* &rootptr, int data) {
if(rootptr == NULL) {
rootptr = GetNewNode(data);
return;
}
else if(data < rootptr->data) {
insert(rootptr->left,data);
}
else {
insert(rootptr->right, data);
}
// (data > rootptr->data)? insert(rootptr->right,data) : insert(rootptr->left, data);
}
void min(BstNode* &rootptr) {
while(rootptr->left != NULL) {
rootptr = rootptr->left;
}
// rootptr = rootptr->right;
}
bool search(BstNode* rootptr, int data) {
// cout << data << endl;
if(rootptr == NULL) return false;
if(rootptr->data == data) return true;
else if(data <= rootptr->data) return search(rootptr->left,data);
else return search(rootptr->right, data);
}
void Delete(BstNode* &rootptr, int data) {
if(rootptr == NULL ) return;
else if(data < rootptr->data) Delete(rootptr->left, data);
else if(data > rootptr->data) Delete(rootptr->right, data);
else { // Wohoo, we finally found the node, now we can delete it 😈
// Case 1: No Child
if(rootptr->left == NULL && rootptr->right == NULL) {
delete rootptr;
rootptr = NULL;
}
// Case 2: One Child
else if(rootptr->left == NULL) {
BstNode* temp = rootptr;
// delete rootptr;
rootptr = rootptr->left;
delete temp;
}
else if(rootptr->right == NULL) {
BstNode* temp = rootptr;
rootptr = rootptr->right;
delete temp;
}
// Case 3: Two Children
else {
BstNode* temp = min(rootptr->right);
rootptr->data = temp->data;
Delete(rootptr->right, temp->data);
}
}
}
int main(int argc, char* argv[]) {
abhisheknaiidu();
insert(rootptr, 5);
insert(rootptr, 3);
insert(rootptr, 1);
insert(rootptr,10);
insert(rootptr,4);
insert(rootptr,11);
int n;
cin >> n;
if(search(rootptr, n) == true) cout << "Found" << endl;
else cout << "Not Found";
Delete(rootptr, n)
// search(rootptr, 20);
// Iterative Version:
// Node *newNode(int key)
// {
// Node *node = new Node;
// node->data = key;
// node->left = node->right = nullptr;
// return node;
// }
// void insert(Node **headref, int data)
// {
// if (*headref == NULL)
// {
// *headref = newNode(data);
// return;
// }
// Node *travptr = *headref;
// Node *parentNode = NULL;
// while (travptr != NULL)
// {
// parentNode = travptr;
// travptr = (data > travptr->data) ? travptr->right : travptr->left;
// }
// if (data > parentNode->data)
// parentNode->right = newNode(data);
// else
// parentNode->left = newNode(data);
// }
// bool find(Node **headref, int data)
// {
// Node *travptr = *headref;
// if (data == (*headref)->data)
// return true;
// while (travptr != NULL && travptr->data != data)
// travptr = (data > travptr->data) ? travptr->right : travptr->left;
// if (travptr == NULL)
// {
// return false;
// }
// return true;
// }
return 0;
}