-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary_Search_Tree.cpp
166 lines (137 loc) · 4.31 KB
/
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
#include <bits/stdc++.h>
using namespace std;
#define cin_2d(vec, n, m) for(int i = 0; i < n; i++) for(int j = 0; j < m && cin >> vec[i][j]; j++);
#define cout_2d(vec, n, m) for(int i = 0; i < n; i++, cout << "\n") for(int j = 0; j < m && cout << vec[i][j] << " "; j++);
#define fixed(n) fixed << setprecision(n)
#define ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
#define fill(vec, value) memset(vec, value, sizeof(vec));
#define mul_mod(a, b, m) (((a % m) * (b % m)) % m)
#define add_mod(a, b, m) (((a % m) + (b % m)) % m)
#define all(vec) vec.begin(), vec.end()
#define rall(vec) vec.rbegin(), vec.rend()
#define sz(x) int(x.size())
#define debug(x) cout << #x << ": " << (x) << "\n";
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define Mod 1'000'000'007
#define OO 2'000'000'000
#define EPS 1e-9
#define PI acos(-1)
template < typename T = int > using Pair = pair < T, T >;
vector < string > RET = {"NO", "YES"};
template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
for (auto &x : v) in >> x;
return in;
}
template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
for (const T &x : v) out << x << ' ';
return out;
}
struct BST {
int data;
BST *left, *right;
BST(int data = 0){
this -> data = data;
left = right = nullptr;
}
// Insert New node
BST* Insert(BST* root, int val){
if(!root) return new BST(val);
if(val > root -> data)
root -> right = Insert(root -> right, val);
else
root -> left = Insert(root -> left, val);
return root;
}
// Inorder Traverse (LRR)
void Inorder(BST* root){
if(!root) return;
Inorder(root -> left);
cout << root -> data << " ";
Inorder(root -> right);
}
// Preorde Traverse (RLR)
void Preorder(BST* root){
if(!root) return;
cout << root -> data << " ";
Preorder(root -> left);
Preorder(root -> right);
}
// Postorder Traverse (LRR)
void Postorder(BST* root){
if(!root) return;
Postorder(root -> left);
Postorder(root -> right);
cout << root -> data << " ";
}
// Traverse each level
void Level_Order(BST* root){
if(!root) return;
queue < BST* > bfs;
bfs.push(root);
while(!bfs.empty()){
BST* curr = bfs.front();
bfs.pop();
cout << curr -> data << " ";
if(curr -> left)
bfs.push(curr -> left);
if(curr -> right)
bfs.push(curr -> right);
}
}
// Search on a node
bool Search(BST* root, int val){
if(!root) return false;
if(root -> data == val) return true;
if(val > root -> data) return Search(root -> right, val);
else return Search(root -> left, val);
}
// Get minimum node in BST
BST* minValueNode(BST* node){
BST* current = node;
while (current && current -> left != nullptr) current = current -> left;
return current;
}
// Get maximum node in BST
BST* maxValueNode(BST* node){
BST* current = node;
while (current && current -> right != nullptr) current = current -> right;
return current;
}
// Delete Node
BST* Delete_Node(BST* root, int key){
if(!root) return root;
if(key < root -> data)
root -> left = Delete_Node(root -> left, key);
else if(key > root -> data)
root -> right = Delete_Node(root -> right, key);
else {
if(!root -> left && !root -> right) return nullptr;
else if(!root -> left){
BST* temp = root -> right;
free(root);
return temp;
}else if(!root -> right){
BST* temp = root -> left;
free(root);
return temp;
}
BST* temp = minValueNode(root -> right);
root -> data = temp -> data;
root -> right = Delete_Node(root -> right, temp -> data);
}
return root;
}
};
void Solve(){
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int t = 1;
//cin >> t;
while(t--)
Solve();
return 0;
}