-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.cpp
More file actions
66 lines (54 loc) · 1.27 KB
/
BinarySearchTree.cpp
File metadata and controls
66 lines (54 loc) · 1.27 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
#include <iostream>
#include <algorithm>
using namespace std;
int n, a[1010];
struct Node{
int data;
struct Node *left;
struct Node *right;
};
Node* CreateNode(int x){
// Create a new node with value x, it left and right is NULL for now
Node* p = new Node();
p->data=x;
p->left = p->right = NULL;
return p;
}
void addNode(Node* &root,int x){
// we want root to be permanently alternate, therefore "&root"
// x is int and root is Node*, so we must turn x into Node*
Node* temp = CreateNode(x);
//if this tree is empty
if (root==NULL){
root=temp;
}
// else we need to find where to insert this node
else{
if (root->data>=x)
addNode(root->left, x);
else
addNode(root->right, x);
}
}
int findheight(Node* root){
if (root==NULL)
return 0;
int lh=0,rh=0;
lh = findheight(root->left);
rh = findheight(root->right);
return max(lh,rh)+1;
}
int main(){
// Initiate a root with NULL value
Node* root = NULL;
// read data from input
cin>>n;
for (int i=1;i<=n;i++)
cin>>a[i];
// Create a tree
for (int i=1;i<=n;i++)
addNode(root,a[i]);
// Find the height of a tree
cout<<findheight(root);
return 0;
}