-
Notifications
You must be signed in to change notification settings - Fork 1
/
bst.cpp.txt
199 lines (157 loc) · 3.16 KB
/
bst.cpp.txt
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
#include<bits/stdc++.h>
using namespace std;
//BINARY SEARCH TREE IMPLEMENTATION USING RECURSION
//write 3 functions
//getNode --> Here, root is becoming global. Hence, pointer to pointer is not used.
//insert
//search
class BstNode {
public:
int data;
BstNode* left;
BstNode* right;
};
BstNode* getNode(int new_data)
{
BstNode* new_node = new BstNode();
new_node->data = new_data;
new_node->left=new_node->right=NULL;
return new_node;
}
BstNode* Insert(BstNode* root,int new_data)
{
if (root==NULL)
{
root = getNode(new_data);
}
else if(new_data<=root->data)
{
root->left = Insert(root->left,new_data);
}
else
{
root->right = Insert(root->right,new_data);
}
//cout<<root->data<<"\t ";
return root;
}
bool Search (BstNode* root,int data)
{
if(root==NULL)
{
return false;
}
if(root->data==data)
{
return true;
}
else if (data<=root->data)
{
return Search(root->left,data);
}
else
return Search(root->right,data);
}
//finding the smallest element in the BST
int findSmall(BstNode* root)
{
BstNode* current = root;
if(current==NULL)
{
cout<<"No element in the BST";
return -1;
}
while(current->left!=NULL)
{
current=current->left;
}
return current->data;
}
//finding the largest element in the BST
int findLarge(BstNode* root)
{
BstNode* current = root;
if(current==NULL)
{
cout<<"No element in the BST";
return -1;
}
while(current->right!=NULL)
{
current=current->right;
}
return current->data;
}
/*
Finding Largest using recursion
findLarge(Node *root)
{
BstNode* current = root;
if(current==NULL)
{
cout<<"No element in the BST";
return -1;
}
else if (current->right==NULL)
{
return current->data;
}
return findLarge(current->right);
}
*/
//function to return the maximum of two numbers
//int max(int left,int right)
//{
// if(left>right)
// return left;
//
// return right;
// }
//int findHeight(BstNode* root)
//{
// int leftHeight, rightHeight,maxHeight;
// if(root==NULL)
// return -1;
//
// else{
//
// leftHeight = findHeight(root->left);
// rightHeight = findHeight(root->right);
// }
//
//
// if(leftHeight>rightHeight)
// maxHeight = leftHeight+1;
// else
// maxHeight = rightHeight+1;
//
//// cout<<leftHeight<<"\t"<<rightHeight;
//
// return maxHeight;
//
//}
int findHeight(BstNode* root)
{
if(root==NULL)
return -1;
return max(findHeight(root->left),findHeight(root->right))+1;
}
int main()
{
BstNode* root = NULL;
root = Insert(root,1);
root = Insert(root,2);
root = Insert(root,3);
root = Insert(root,4);
root = Insert(root,5);
cout<<"enter the number to be searched in the BST "<<endl;
int n;
//cin>>n;
//if(Search(root,n)== true) cout<<"No. available";
//else cout<<"No. not available";
cout<<"\n";
cout<<"Smallest Element in the BST is "<<findSmall(root)<<endl;
cout<<"Largest Element in the BST is "<<findLarge(root)<<endl;
cout<<"Height of the Binary Search Tree is "<<findHeight(root)<<endl;
return 0;
}