-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtree
80 lines (76 loc) · 930 Bytes
/
btree
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
#include<iostream>
using namespace std;
class Bnode {
friend class Btree;
private:
Bnode *left;
Bnode *right;
int val;
public:
Bnode(int v)
{
val = v;
}
Bnode(int v, Bnode *l, Bnode *r)
{
val = v;
left = l;
right = r;
}
int weight()
{
return val;
}
~Bnode() {}
};
int max(int a, int b)
{
return a > b ? a : b;
}
class Btree{
private:
Bnode *root;
public:
Btree()
{
root = NULL;
}
void CreateTree(Bnode *&r)
{
int c;
cin >> c;
if (c != -1)
{
r = new Bnode(c);
CreateTree(r->left);
CreateTree(r->right);
}
}
void PreOrder(Bnode* &r)
{
if (r)
{
cout << r->weight() << " ";
PreOrder(r->left);
PreOrder(r->right);
}
}
int height(Bnode *&r)
{
if (r == NULL)return 0;
else
{
return 1 + max(height(r->left), height(r->right));
}
}
};
int main()
{
Btree *ss;
Btree s3;
ss = &s3;
Bnode *s;
ss->CreateTree(s);
cout << ss->height(s);
return 0;
}