-
Notifications
You must be signed in to change notification settings - Fork 2
/
Binary_search_tree.cpp
209 lines (187 loc) · 7.53 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
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
/*
Write a program to create a binary search tree. Your program should able to create a binary search tree and perform the operation such as inserting an element into the tree,
deleting an element from the tree, finding the smallest element from the tree, finding the largest element from the tree, and computing the total number of nodes in the binary search tree.
The program should contain at least the following functions:
1. createBinarySearchTree ( ) // This function will create an empty binary search tree.
2. insertBinarySearchTree ( ) // This function will insert an element into the binary search tree.
3. deleteBinarySearchTree ( ) // This function will delete an element from the binary search tree if exists else return “item to be deleted does not exists in the tree”.
4. findSmallestElement() // This function will return the smallest element in the binary search tree.
5. findLargestElement() // This function will return the largest element in the binary search tree.
6. totalElement() // This function will return the total number of elements in the binary search tree.
You need to find out the parameters that must be passed to the above functions, and the return type of each function to carry out the intended function.
Test your program before uploading.
*/
#include<iostream>
using namespace std;
//structure to create a tree containing a data value and left and right child.
struct tree
{
int data;
struct tree * left;
struct tree * right;
};
// A function to create an empty binary search tree.
struct tree * createBinarySearchTree ()
{
struct tree * new_tree = new tree;
new_tree->left = NULL;
new_tree->right = NULL;
return new_tree;
}
// function to insert elements in binary search tree.
struct tree * insertBinarySearchTree(struct tree * root, int val)
{
//create an empty ele tree and assign value to it.
struct tree * ele = createBinarySearchTree();
ele->data = val;
//store the ele tree in root , if the root of the tree itself is NULL.
if(root==NULL)
root= ele;
struct tree * ptr = root; // A dummy variable ptr to traverse the tree.
struct tree * prev; // A structure pointer prev to point to previous node where data value is to be inserted.
// A loop to obtain the position where data is to be inserted
while(ptr != NULL && ptr->data != val)
{
if(val < ptr->data)
{
prev=ptr;
ptr=ptr->left;
}
else if(val > ptr->data)
{
prev=ptr;
ptr=ptr->right;
}
}
// Checking this condition so that same element is not added again and assigning new tree to the found location.
if(ptr == NULL)
{
if(val < prev->data)
prev->left = ele;
else
prev->right = ele;
}
return root;
}
// function to return inorder successor
struct tree * minimumtree(struct tree* node)
{
struct tree* current = node;
//loop to find the leftmost child
while (current && current->left != NULL)
current = current->left;
return current;
}
// function to delete elements from binary search tree.
struct tree * deleteBinarySearchTree(struct tree * root, int val)
{
if (root == NULL)
{
cout<<"Item to be deleted doesnot exist.\n";
return root;
}
// If the key to be deleted is smaller than the root's key, then it lies in left subtree.
if (val < root->data)
root->left = deleteBinarySearchTree(root->left, val);
// If the key to be deleted is greater than the root's key, then it lies in right subtree
else if (val > root->data)
root->right = deleteBinarySearchTree(root->right, val);
// if key is same as root's key, then This is the node to be deleted.
else
{
// node with only one child or no child
if (root->left == NULL)
{
struct tree *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct tree *temp = root->left;
free(root);
return temp;
}
// Get the inorder successor (smallest in the right subtree) for node with two children
struct tree* temp = minimumtree(root->right);
root->data = temp->data;
root->right = deleteBinarySearchTree(root->right, temp->data); // deleting
}
return root;
}
// Function to find the smallest element in binary search tree.
int findSmallestElement(struct tree * root)
{
//The Smallest Element is found in the bottom-left most child of the tree.
while(root->left != NULL)
{
root=root->left;
}
return root->data;
}
// Function to find the largest element in binary search tree.
int findLargestElement(struct tree * root)
{
//The Largest Element is found in the bottom-right most child of the tree.
while(root->right != NULL)
{
root=root->right;
}
return root->data;
}
// A function to calculate the total number of elements in Binary Search Tree.
int totalElement(struct tree * root)
{
// If no element in the root means size of tree is 0.
if(root == NULL)
return 0;
else
{
// totalElement(root->left) will recursively call this function and find the size of left sub-tree.
// totalElement(root->right) will recursively call this function and find the size of right sub-tree.
int size = totalElement(root->left) + totalElement(root->right) + 1;
return size;
}
}
int main()
{
struct tree * root = NULL;
int ele,smele,larele,totele;
int choice;
char ch;
// A menu driven Program to perform following operations as per the user choice.
do
{
cout<<"Perform Operations.\n";
cout<<"Enter 1 to insert element into binary search tree.\n";
cout<<"Enter 2 to delete element from binary search tree.\n";
cout<<"Enter 3 to find the smallest element in the binary search tree.\n";
cout<<"Enter 4 to find the largest element in the binary search tree.\n";
cout<<"Enter 5 to calculate total number of elements in the binary search tree.\n";
cin>>choice;
switch (choice)
{
case 1: cout<<"Enter element to be inserted.\n";
cin>>ele;
root=insertBinarySearchTree(root,ele);
break;
case 2: cout<<"Enter element to be deleted.\n";
cin>>ele;
root = deleteBinarySearchTree(root,ele);
break;
case 3: smele = findSmallestElement(root);
cout<<"Smallest Element in binary search tree is : "<< smele <<" .\n";
break;
case 4: larele = findLargestElement(root);
cout<<"Largest Element in binary search tree is : "<< larele <<" .\n";
break;
case 5: totele = totalElement(root);
cout<<"Total number of Elements in binary search tree are : "<< totele <<" .\n";
break;
default: cout<<"Any operations cannot be performed. Please Enter a valid number.\n";
}
cout<<"Enter 'y' if you want to perform operations again.\n";
cin>>ch;
}while(ch == 'y' || ch == 'Y');
return 0;
}