-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst.cpp
More file actions
58 lines (58 loc) · 864 Bytes
/
bst.cpp
File metadata and controls
58 lines (58 loc) · 864 Bytes
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
#include<iostream>
using namespace std;
class bst
{
public:
int data;
bst *left,*right;
};
int count;
bst * insertInBinaryTree(bst* root,int x)
{
if(!root)
{
bst *temp=new bst();
temp->data=x;
temp->left=temp->right=NULL;
root=temp;
count++;
return root;
}
if(root->data>x)
{
root->left=insertInBinaryTree(root->left,x);
}
else if(root->data<x)
{
root->right=insertInBinaryTree(root->right,x);
}
return root;
}
int height(bst* root)
{
if(root==NULL)
return 0;
else
return (max(height(root->right),height(root->left))+1);
}
void inorder(bst* root)
{
if(root->left)
inorder(root->left);
cout<<root->data<<" ";
if(root->right)
inorder(root->right);
}
int main()
{
bst* root=NULL;
int n,x;
cin>>n;
for(int i=0;i<n;i++){
cin>>x;
root=insertInBinaryTree(root,x);
}
cout<<height(root)<<endl;
inorder(root);
return 0;
}