include stdafx include iostream using namespace std class leaf public

 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
#include "stdafx.h"
#include <iostream>
using namespace std;
class leaf
{
public:
int node;
int height_left, height_right;
leaf *left,*right;
leaf(int x)
{
node=x;
left=right=NULL;
height_left=height_right=0;
cout << "leaf(int x) worker " << endl;
}
leaf* add_leaf(int x)
{
cout << "add_leaf(leaf *t, int x) worker " << endl;
if (this==NULL)
return new leaf(x);
if (x<node)
if (left) left->add_leaf(x); else left=left->add_leaf(x);
else
if (right) right->add_leaf(x); else right=right->add_leaf(x);
return this;
}
void view()
{
if (left) left->view();
cout<<node<<'('<<height_left<<'/'<<height_right<<") ";//значение узла (высота левого поддерева/высота правого поддерева)
if (right) right->view();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
leaf* root=NULL;
for (int i = 0; i < 10; ++i)
root=root->add_leaf(10-i);
root->view();
return 0;
}