-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path38 Binary_Search_Tree_height.cpp
More file actions
122 lines (109 loc) · 2.3 KB
/
38 Binary_Search_Tree_height.cpp
File metadata and controls
122 lines (109 loc) · 2.3 KB
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
/*
Calculate the height of binary search tree .The height of a binary search tree is the number of edges between the tree's root and its furthest leaf. You are given a pointer, named as root , pointing to the root of a binary search tree. Create a function named CalculateHeight function which will return the height of the binary search tree. Note n (number of nodes ) should be between 3 to 10 ,If entering other value display message NOT IN RANGE
Input Format
The first line contains an integer,n , denoting the number of nodes in the tree.
Each of the n subsequent lines contains an integer, data, denoting the value of an element that must be added to the BST.
Constraints
Number of nodes n must lies between 3 to 10.
Output Format
Print the integer returned by your CalculateHeight function denoting the height of the BST.
Sample Input 0
7
3
5
2
1
4
6
7
Sample Output 0
3
*/
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
int data;
node *left=NULL;
node *right=NULL;
};
node *root=NULL;
node *loc=NULL, *locp=NULL;
void find(int x)
{
node *cur=root;
node *curp=NULL;
while(cur!=NULL)
{
if(cur->data==x)
{
loc=cur;
locp=curp;
break;
}
else if(cur->data > x)
{
curp = cur;
cur = cur->left;
}
else
{
curp = cur;
cur = cur->right;
}
}
if(cur==NULL)
{
loc = cur;
locp = curp;
}
}
void insert(int x)
{
find(x);
if(loc==NULL)
{
node *p = new node;
p->data = x;
if(locp==NULL)
{
root = p ;
}
else if(locp->data > x)
{
locp->left= p ;
}
else
{
locp->right=p;
}
}
}
int height(node *head)
{
if(head==NULL)
return 0;
return max(height(head->left),height(head->right))+1;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
cin>>n;
if(n>=3 && n<=10)
{for(int i=0;i<n;i++)
{
int x;
cin>>x;
insert(x);
}
cout<<height(root)-1;}
else
{
cout<<"NOT IN RANGE";
}
return 0;
}