forked from ossamamehmood/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_tree_creation.cpp
73 lines (60 loc) · 1.18 KB
/
binary_tree_creation.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
#include <bits/stdc++.h>
using namespace std;
class Node{
public:
// this is the structure of the node
int data=0;
Node*left=NULL;
Node*right=NULL;
Node (int key){
data=key;
}
};
// This is the insert function
Node * insert(Node * root,int data){
if(root==0){
Node * p=new Node( data);
return p;
}
else{
if (root->data>data){
root->left=insert(root->left,data);
}
else{
root->right=insert(root->right,data);
}
return root;}
}
// This is the inorder Traversal function
void INORDER(Node* root){
if (root==0){
return ;
}
else{
INORDER(root->left);
cout<<root->data<<" ";
INORDER(root->right);
}}
// The below function calculates the Height if the tree.
int Height(Node * root){
if(root==0){
return 0;
}
else{
int leftl=Height(root->left);
int rightl =Height(root->right);
return (max(rightl+1,leftl+1));
}
}
}
int main ()
{
Node * root=0;
root=insert(root,6);
insert(root,67);
insert(root,90);
insert(root,103);
cout<< Height(root)<<" "<<endl;
INORDER(root);
return 0;
}