-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBST_height.cpp
71 lines (65 loc) · 1.42 KB
/
BST_height.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
#include <iostream>
//errors removed
using namespace std;
struct Node{
int data;
Node* left;
Node* right;
};
int height(Node *root){
if(root==NULL){
return -1;
}
int lh=height(root->left);
int rh=height(root->right);
return max(lh,rh)+ 1;
}
void Print(Node* root){
if(root==NULL){
return ;
}
Print(root->left);
cout<<root->data<<" ";
Print(root->right);
}
Node* GetNewNode(int data){
Node* newNode=new Node();
newNode->data=data;
newNode->left=newNode->right=NULL;
return newNode;
}
Node* Insert(Node* root, int data){
if(root==NULL){
root=GetNewNode(data);
return root;
}
else if(data<=root->data)
root->left=Insert(root->left,data);
else
root->right=Insert(root->right,data);
return root;
}
bool Search(Node* root, int data){
if(root==NULL) return false;
else if(root->data==data) return true;
else if(data<=root->data) return Search(root->left,data);
else return Search(root->right,data);
}
int main()
{
int num;
int num1;
Node* root=NULL;
root=Insert(root,15);
root=Insert(root,20);
root=Insert(root,10);
root=Insert(root,40);
root=Insert(root,5);
root=Insert(root,30);
root=Insert(root,76);
cout<<"The BST inorder traversal is:\n";
Print(root);
cout<<"\nThe height of BST: "<<height(root)<<"\n";
return 0;
}
#sayan achis