-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearchTree.cpp
185 lines (159 loc) · 4.72 KB
/
BinarySearchTree.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
175
176
177
178
179
180
181
182
183
184
185
//Implement Binary Search Tree and Perform Operations : Insertion, Deletion, Searching (Issue #107)
//https://github.com/amritansh22/Data-Structures-and-Algorithms-in-cpp/issues/107
//Contributed by @tauseefmohammed2 : https://github.com/tauseefmohammed2
#include<iostream>
using namespace std;
class BinarySearchTree {
struct BSTnode {
int data;
BSTnode* left;
BSTnode* right;
};
BSTnode* root;
//Function to Clear a Node
BSTnode* clear(BSTnode* t) {
if(t == NULL)
return NULL;
{
clear(t->left);
clear(t->right);
delete t;
}
return NULL;
}
//Function to Insert a Node
BSTnode* insert(int x, BSTnode* t)
{
if(t == NULL)
{
t = new BSTnode;
t->data = x;
t->left = t->right = NULL;
}
else if(x < t->data)
t->left = insert(x, t->left);
else if(x > t->data)
t->right = insert(x, t->right);
return t;
}
//Function to Obtain the Node with the Minimum Value
BSTnode* min(BSTnode* t)
{
if(t == NULL)
return NULL;
else if(t->left == NULL)
return t;
else
return min(t->left);
}
//Function to Obtain the Node with the Maximum Value
BSTnode* max(BSTnode* t) {
if(t == NULL)
return NULL;
else if(t->right == NULL)
return t;
else
return max(t->right);
}
//Function to Delete/Remove a Node from the BST
BSTnode* remove(int x, BSTnode* t) {
BSTnode* temp;
if(t == NULL)
return NULL;
else if(x < t->data)
t->left = remove(x, t->left);
else if(x > t->data)
t->right = remove(x, t->right);
else if(t->left && t->right)
{
temp = min(t->right);
t->data = temp->data;
t->right = remove(t->data, t->right);
}
else
{
temp = t;
if(t->left == NULL)
t = t->right;
else if(t->right == NULL)
t = t->left;
delete temp;
}
return t;
}
//Function to Display Inorder Traversal of the BST
void inorder_traversal(BSTnode* t) {
if(t == NULL)
return;
inorder_traversal(t->left);
cout << t->data << " ";
inorder_traversal(t->right);
}
//Function to Search and Check whether a Given Node is Present in the BST or Not
BSTnode* search(BSTnode* t, int x) {
if(t == NULL)
return NULL;
else if(x < t->data)
return search(t->left, x);
else if(x > t->data)
return search(t->right, x);
else
return t;
}
public:
//Creating Corresponding Functions for the Object to Acccess Outside the Class
BinarySearchTree() {
root = NULL;
}
~BinarySearchTree() {
root = clear(root);
}
void insert(int x) {
root = insert(x, root);
}
void remove(int x) {
root = remove(x, root);
}
void display() {
inorder_traversal(root);
cout << endl;
}
void search(int x) {
root = search(root, x);
if(root == NULL)
cout << endl << x <<" is Not Present in the BST";
else
cout<< endl << x <<" is Present in the BST";
}
};
int main() {
//Creating Object of BinarySearchTree Class
BinarySearchTree t;
//Inserts the Following Nodes into the BST
t.insert(20);
t.insert(25);
t.insert(15);
t.insert(10);
t.insert(30);
//Displays Inorder Traversal of BST
t.display();
//Remove 20 and 25 From the BST and Display Corresponding Inorder Traversal
t.remove(20);
t.display();
t.remove(25);
t.display();
t.search(15);
return 0;
}
//Output :
//10 15 20 25 30
//10 15 25 30
//10 15 30
//15 is Present in the BST
//20 is Not Present in the BST
//Time Complexity :
//Insertion : O(h) [h = Height of BST]
//Deletion : O(h) [h = Height of BST]
//Searching : O(h) [h = Height of BST]
//Space Complexity :
//O(n) [n = Number of Nodes in BST]