-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_depth.cpp
89 lines (84 loc) · 1.17 KB
/
max_depth.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<iostream>
using namespace std;
class node{
int data;
node*left;
node*right;
friend class BST;
};
class BST{
public:
node*root ;
public:
void insert();
node*insert1(node*root,int data);
int maxdepth(node* root);
void maxdepth1();
BST()
{
root = NULL;
}
};
void BST::maxdepth1()
{
cout<<maxdepth(root->left);
}
void BST::insert()
{
int c;
cout<<"enter the data"<<endl;
cin>>c;
root = insert1(root,c);
}
node* BST::insert1(node*root,int data)
{
node* tmp = new node;
tmp->data = data;
if(root==NULL)
{
return tmp;
}
else
if(root->data > tmp->data)
{
root->left = insert1(root->left,data);
}
else
if(root->data<tmp->data){
root->right = insert1(root->right,data);
}
return root;
}
int BST::maxdepth(node*root)
{
if(root==NULL)
{
return 0;
}
else
{
int l = maxdepth(root->left);
int r = maxdepth(root->right);
if(l>r)
return l+1;
else
return r+1;
}
}
int main()
{
BST a;
int ch;
while(1)
{
cout<<"1.insert"<<endl;
cout<<"2.depth"<<endl;
cin>>ch;
switch (ch) {
case /* value */1:a.insert();
break;
case 2 : a.maxdepth1();
break;
}
}
}