-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.cpp
93 lines (83 loc) · 1.31 KB
/
node.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
90
91
92
93
#include "stdafx.h"
#include "node.h"
#include <iostream>
node::node()
{
left = NULL;
right = NULL;
info = 0;
}
node::node(int inf)
{
left = NULL;
right = NULL;
info = inf;
}
void node::get_info(node *& obj,int r)
{
if (obj == NULL) return;
if (obj != NULL)
{
get_info(obj->left,++r);
for (int i = 0; i<r; ++i) std::cout << "|";
std::cout<< obj->info<<"\n";
r--;
}
get_info(obj->right, ++r);
}
int node::rand_root()
{
return rand()%2;
}
void node::add_node(int inf, node *& obj)
{
if (NULL == obj)
{
obj = new node;
obj->info = inf;
obj->left = obj->right = NULL;
}
if (inf<obj->info)
{
if (obj->left != NULL) add_node(inf, obj->left);
else
{
obj->left = new node;
obj->left->left = obj->left->right = NULL;
obj->left->info = inf;
}
}
if (inf>obj->info)
{
if (obj->right != NULL) add_node(inf, obj->right);
else
{
obj->right = new node;
obj->right->left = obj->right->right = NULL;
obj->right->info = inf;
}
}
if (inf == obj->info)
{
if (obj->left != NULL) add_node(inf, obj->left);
else
{
obj->left = new node;
obj->left->left = obj->left->right = NULL;
obj->left->info = inf;
}
}
}
void node::del(node *& obj)
{
if (obj != NULL)
{
del(obj->left);
del(obj->right);
delete obj;
obj = NULL;
}
}
node::~node()
{
}