forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearchTree.cpp
186 lines (169 loc) · 4.52 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
186
#include <iostream>
using namespace std;
/* A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned properties
The value of the key of the left sub-tree is less than the value of its parent (root) node's key.
The value of the key of the right sub-tree is greater than or equal to the value of its parent (root) node's key.*/
// This program implements insetion,Deletion and Searching in Binary Search Tree(BST)
struct Node{ //Structure is declared which holds Node data and address of left and right child
struct Node *lchild;
int data;
struct Node *rchild;
}*root=NULL; // root object is declared globally for struct Node
void insert(int element) //Insertion happens here
{
struct Node *p=root; // using 2 pointers for keeping track of Nodes
struct Node *q,*tmp;
tmp= new Node;
tmp->data=element;
tmp->lchild=tmp->rchild=NULL;
if(root==NULL)
{
root=tmp;
}
else
{
while(p!=NULL)
{
q=p;
if(p->data==element)
{
return;
}
else if(element < p->data) //if Inserted data is less than current Node data, move to left child
{
p=p->lchild;
}
else{
p=p->rchild; //if Inserted data is greater than or equal to current Node data, move to right child
}
}
if(element > q->data)
{
q->rchild=tmp;
}
else
q->lchild=tmp;
}
}
void Search(Node *p,int key) //Seraching operation takes key as input and search for key in BST
{
if(p==NULL)
{
cout << " BST is empty" << endl;
}
else{
while(p!=NULL)
{
if(p->data==key)
{
cout << "Element is found" << endl;
return;
}
else if(key < p->data)
{
p=p->lchild;
}
else{
p=p->rchild;
}
}
}
cout << "Element not found "<<endl;
}
void InOrder(Node *p) //Inorder travesal is used because inorder of BST gives Sorted output
{
if(p)
{
InOrder(p->lchild);
cout << p->data << " ";
InOrder(p->rchild);
}
}
int Height(Node *p) //To find height of corresponding Node
{
int x,y;
if(p==NULL)return 0;
x=Height(p->lchild);
y=Height(p->rchild);
return x>y?x+1:y+1;
}
/*For deleting any Node on BST, Deleted Node has to be replaced with Appropiate Node without affecting the property of BST
If we are deleting any node, corresponding Node should be replaced by either InOrder predecessor or Inorder Successor based on height condition*/
struct Node * InOrderpredecssor(Node *p) //If height of left child is greater than right child,it will enter this loop
{
while(p && p->rchild!=NULL)
{
p=p->rchild;
}
return p;
}
struct Node * InOrderSuccessor(Node *p) //If height of left child is less than right child,it will enter this loop
{
while(p && p->lchild!=NULL)
{
p=p->lchild;
}
return p;
}
struct Node * Delete(Node *p,int key)
{
struct Node *q;
if(p==NULL)
{
return NULL;
}
if(p->lchild==NULL && p->rchild==NULL)
{
delete p;
return NULL;
}
if(key < p->data)
{
p->lchild=Delete(p->lchild,key);
}
else if(key > p->data)
{
p->rchild=Delete(p->rchild,key);
}
else
{
if(Height(p->lchild) > Height(p->rchild))
{
q=InOrderpredecssor(p->lchild);
p->data=q->data;
p->lchild=Delete(p->lchild,q->data);
}
else
{
q=InOrderSuccessor(p->rchild);
p->data=q->data;
p->rchild=Delete(p->rchild,q->data);
}
}
return p;
}
int main()
{
int Num,data,searchdata;
cout << "Enter Number of data to be instered in BST: "<<endl;
cin >> Num;
cout <<"Enter the node values: " << endl; //Enter the Node values to be inserted into BST
for(int i=0;i<Num;i++)
{
cin >> data;
insert(data);
}
cout <<"Elements in BST are: "<< endl;
InOrder(root);
cout << endl;
cout << "Enter any node value to search in BST:" << endl;
cin >> searchdata;
Search(root,searchdata);
cout <<"Enter the value to be deleted in BST: " << endl;
cin >> data;
Delete(root,data);
cout << "After deleting Node: " << endl;
InOrder(root);
cout << endl;
return 0;
}