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
50
51
52
53
54
55
56
57
#include "stdafx.h"
#include <iostream>
using namespace std;
class leaf
{
public:
int node;
leaf *left,*right;
leaf(int x)
{
node=x;
left=right=NULL;
cout << "leaf(int x) worker " << endl;
}
leaf* add_leaf(int x)
{
cout << "add_leaf(int x) worker " << endl;
if (this==NULL)
return new leaf(x);
if (x<node)
left=left->add_leaf(x);
else
right=right->add_leaf(x);
return this;
}
void delete_tree()
{
if (left) left->delete_tree();
if (right) right->delete_tree();
cout << "Deleting : "<< this->node << endl;
delete this;
}
void view()
{
if (left) left->view();
cout<<node<< " ";
if (right) right->view();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
leaf* root=NULL;
int m[10]={10,4,8,-1,4,2,8,2,5,-114};
for (int i = 0; i < 10; ++i)
root=root->add_leaf(m[i]);
root->view();
cout << endl;
root->delete_tree();
return 0;
}