-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary_search_tree.cpp
More file actions
211 lines (185 loc) · 5.53 KB
/
binary_search_tree.cpp
File metadata and controls
211 lines (185 loc) · 5.53 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
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
209
210
211
/***************************************************
* Problem Name : binary_search_tree.cpp
* Problem Link : Basic Code
* Verdict : Done
* Date : 2020-06-02
* Problem Type : Data Stucture - Binary Search Tree
* Author Name : Saikat Sharma
* University : CSE, MBSTU
***************************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <cstdlib>
#include <deque>
#include <stack>
#include <bitset>
#include <cassert>
#include <map>
#include <set>
#include <cassert>
#include <iomanip>
#include <random>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define __FileRead freopen ("input.txt", "r", stdin)
#define __FileWrite freopen ("output.txt", "w", stdout)
#define SET(a,v) memset(a,v,sizeof(a))
#define SZ(v) (int)v.size()
#define pii pair<ll,ll>
#define pll pair <ll, ll>
#define debug cout <<"######\n"
#define debug1(x) cout <<"### " << x << " ###\n"
#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n"
#define nl cout << "\n";
#define sp cout << " ";
#define sl(n) scanf("%lld", &n)
#define sf(n) scanf("%lf", &n)
#define si(n) scanf("%d", &n)
#define ss(n) scanf("%s", n)
#define pf(n) scanf("%d", n)
#define pfl(n) scanf("%lld", n)
#define all(v) v.begin(), v.end()
#define srt(v) sort(v.begin(), v.end())
#define r_srt(v) sort(v.rbegin(), v.rend())
#define rev(v) reverse(v.rbegin(), v.rend())
#define Sqr(x) ((x)*(x))
#define Mod(x, m) ((((x) % (m)) + (m)) % (m))
#define Max3(a, b, c) max(a, max(b, c))
#define Min3(a, b, c) min(a, min(b, c))
#define pb push_back
#define mk make_pair
#define F first
#define S second
#define MAX 100005
#define INF 1000000009
#define MOD 1000000007
template<class T>
using min_heap = priority_queue<T, std::vector<T>, std::greater<T>>;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;
/************************************ Code Start Here ******************************************************/
struct BST_Node {
int data;
BST_Node *left, *right;
// get a ned node
BST_Node *get_node (int value) {
BST_Node *pNode = (BST_Node *) malloc (sizeof (BST_Node) );
pNode->data = value;
pNode->left = NULL;
pNode->right = NULL;
return pNode;
}
// insert value in BST
BST_Node *insert_bst (BST_Node *root, int value) {
if (root == NULL) {
return get_node (value);
}
if (value > root->data) {
root->right = insert_bst (root->right, value);
} else {
root->left = insert_bst (root->left, value);
}
return root;
}
BST_Node *minValueNode (BST_Node *root) {
BST_Node *tmpNode = root;
while (tmpNode != NULL && tmpNode->left != NULL) {
tmpNode = tmpNode->left;
}
return tmpNode;
}
BST_Node *delete_bst (BST_Node *root, int key) {
if (root == NULL) return root;
if (key > root->data) {
root->right = delete_bst (root->right, key);
} else if (key < root->data) {
root->left = delete_bst (root->left, key);
} else {
if (root->left == NULL) {
BST_Node *tmpNode = root->right;
free (root);
return tmpNode;
} else if (root->right == NULL) {
BST_Node *tmpNode = root->left;
free (root);
return tmpNode;
} else {
BST_Node *tmpNode = minValueNode (root->right);
root->data = tmpNode->data;
root->right = delete_bst (root->right, tmpNode->data);
}
}
}
// inorder search
void inorder_search (BST_Node *root) {
if (root == NULL) return;
inorder_search (root->left);
cout << root->data << "\n";
inorder_search (root->right);
}
void preorder_search (BST_Node *root) {
if (root == NULL) return;
cout << root->data << "\n";
inorder_search (root->left);
inorder_search (root->right);
}
// find number of element in a BST;
int numberOfElements (BST_Node *root) {
if (root == NULL) {
return 0;
} else {
int l = numberOfElements (root->left);
int r = numberOfElements (root->right);
int cnt = l + r + 1;
return cnt;
}
}
// height of a BST;
int maxDepth (BST_Node *root) {
if (root == NULL)
return 0;
else {
int l = maxDepth (root->left);
int r = maxDepth (root->right);
int cnt;
if (l > r) cnt = l;
else cnt = r;
return cnt + 1;
}
}
};
int main () {
//~ __FastIO;
// declare the BST_Node object
BST_Node BST;
// create the root
BST_Node *root = NULL;
// insert values
root = BST.insert_bst (root, 50);
BST.insert_bst (root, 30);
BST.insert_bst (root, 20);
BST.insert_bst (root, 40);
BST.insert_bst (root, 70);
BST.insert_bst (root, 60);
BST.insert_bst (root, 80);
// print the BST in inorder
BST.inorder_search (root);
return 0;
}