-
Notifications
You must be signed in to change notification settings - Fork 5
/
Binary_Search_Tree.cpp
170 lines (142 loc) · 3.22 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
167
168
169
170
/*
@author : Amirul Islam
@topic : Binary Search Tree
@description : Insert O( log N ), Seach O( log N ), Delete O( log N ), Print O( N )
*/
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
Node(int data) {
this->data = data;
left = right = NULL;
}
};
Node* Insert(Node* root, int data) {
if (root == NULL) {
root = new Node(data);
}
else if (data <= root->data) {
root->left = Insert(root->left, data);
}
else {
root->right = Insert(root->right, data);
}
return root;
}
bool Search(Node* root, int data) {
// Empty BST
if (root == NULL) return false;
// YES! FOUND ...
if (data == root->data) return true;
if (data < root->data) {
return Search(root->left, data);
}
else {
return Search(root->right, data);
}
}
Node* FindMin(Node* root) {
while (root->left != NULL) {
root = root->left;
}
return root;
}
Node* Delete(Node* root, int data) {
// Empty
if (root == NULL) return root;
else if (data < root->data) {
root->left = Delete(root->left, data);
}
else if (data > root->data) {
root->right = Delete(root->right, data);
}
// FOUND
else {
if (root->right == NULL) {
Node* temp = root->left;
free(root);
return temp;
}
else if (root->left == NULL) {
Node* temp = root->right;
free(root);
return temp;
}
else {
Node* temp = FindMin(root->right);
root->data = temp->data;
root->right = Delete(root->right, temp->data);
}
}
return root;
}
void Preorder(Node* root) {
if (root == NULL) return;
cout << root->data << " ";
Preorder(root->left);
Preorder(root->right);
}
// PRINT IN SORTED ORDER
void Inorder(Node* root) {
if (root == NULL) return;
Inorder(root->left);
cout << root->data << " ";
Inorder(root->right);
}
void Postorder(Node* root) {
if (root == NULL) return;
Postorder(root->left);
Postorder(root->right);
cout << root->data << " ";
}
void PrintBST(Node* root) {
cout << "\nPre Order: ";
Preorder(root);
cout << "\nIn Order: ";
Inorder(root);
cout << "\nPost Order: ";
Postorder(root);
cout << "\n";
}
void printSearchResult(Node* root, int n) {
cout << n << (Search(root, n) ? " FOUND!\n" : " NOT FOUND!\n");
}
int main() {
Node* root = NULL;
/*
35
/ \
10 55
/ \
5 15
*/
// insert data
root = Insert(root, 15);
root = Insert(root, 10);
root = Insert(root, 35);
root = Insert(root, 55);
root = Insert(root, 5);
printSearchResult(root, 10);
printSearchResult(root, 25);
// print data in different order
PrintBST(root);
// delete data
int n = 35;
root = Delete(root, n);
cout << "\n" << n << " DELETED!\n";
PrintBST(root);
return 0;
}
/*
10 FOUND!
25 NOT FOUND!
Pre Order: 15 10 5 35 55
In Order: 5 10 15 35 55
Post Order: 5 10 55 35 15
35 DELETED!
Pre Order: 15 10 5 55
In Order: 5 10 15 55
Post Order: 5 10 55 15
*/