-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.h
102 lines (85 loc) · 1.52 KB
/
node.h
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
94
95
96
97
98
99
100
101
102
#include <cstddef>
#define NIL NULL
class Node
{
private:
public:
int val;
int height;
Node *left;
Node *right;
Node *p;
int get_val();
int get_left();
int get_right();
int get_parent();
void set_left(Node *left_child);
void set_right(Node *right_child);
Node(int value);
Node();
~Node();
};
Node::Node()
{
}
Node::Node(int value)
{
Node::val = value;
Node::height = 1;
Node::left = NIL;
Node::right = NIL;
Node::p = NIL;
}
Node::~Node()
{
}
int Node::get_val()
{
return this->val;
}
int Node::get_left()
{
return this->left->get_val();
}
int Node::get_right()
{
return this->right->get_val();
}
int Node::get_parent()
{
return this->p->get_val();
}
void Node::set_left(Node *left_child)
{
this->left = left_child;
this->left->p = this;
}
void Node::set_right(Node *right_child)
{
this->right = right_child;
this->right->p = this;
}
Node *newNode(int value)
{
Node *node = new Node();
node->val = value;
node->left = NIL;
node->right = NIL;
node->p = NIL;
node->height = 1;
return node;
}
/*Node *build_from_array(vector<int> A, int start, int finish)
{
if (start > finish)
{
return NIL;
}
int mid = (start + finish) / 2;
Node root = Node(A[mid]);
Node *onLeft = build_from_array(A, start, mid - 1); //left subtree
Node *onRight = build_from_array(A, mid + 1, finish); //right subtree
root.set_left(onLeft);
root.set_right(onRight);
return &root;
}*/