-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.cpp
163 lines (144 loc) · 2.95 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "Node.h"
Node::Node(char newChar, int frequency) : character(newChar, frequency)
{
left = NULL;
right = NULL;
subTree = NULL;
};
Node::Node(Node* node1, Node* node2, int frequency): character('*', frequency)
{
left = NULL;
right = NULL;
subTree = new Node('*', frequency);
subTree->setLeft(node1);
subTree->setRight(node2);
}
Character& Node::getCharObj()
{
return character;
}
Node* Node::getLeft()
{
return left;
}
Node* Node::getRight()
{
return right;
}
void Node::setLeft(Node* newLeft)
{
left = newLeft;
}
void Node::setRight(Node* newRight)
{
right = newRight;
}
Node* Node::getSubTree()
{
return subTree;
}
void Node::setSubTree(Node* root)
{
subTree = root;
}
VACANCY Node::vacant()
{
if (!left && !right)
return BOTH;
else if (left && !right)
return RIGHT;
else if (!left && right)
return LEFT;
else
return NONE;
}
Node* Node::compareChildPerOrder(HEAP_ORDER order, Node* child)
{
if (child)
{
if (order == MAX)
return (child->getCharObj().getFreq() >= getCharObj().getFreq()) ? (child) : (NULL);
else
return (child->getCharObj().getFreq() <= getCharObj().getFreq()) ? (child) : (NULL);
}
return NULL;
}
int Node::notInPlace(HEAP_ORDER order)
{
bool left = compareChildPerOrder(order, this->left);
bool right = compareChildPerOrder(order, this->right);
if (left && right) // both
return 1;
if (left && !right) // right
return 2;
else if (!left && right) // left
return 3;
else
return 0; // in place
}
// returns bigger child to replace or null if there isn't
Node* Node::repChild(HEAP_ORDER order)
{
if (!left && !right)
return NULL;
else if (left && right)
{
int leftFrequency = left->getCharObj().getFreq();
int rightFrequency = right->getCharObj().getFreq();
if (order == MAX)
{
if (compareChildPerOrder(MAX, left) ||
compareChildPerOrder(MAX, right) )
return (leftFrequency >= rightFrequency) ? (left) : (right);
}
else
{
if (compareChildPerOrder(MIN, left)
|| compareChildPerOrder(MIN, right))
return (leftFrequency <= rightFrequency) ? (left) : (right);
}
return NULL;
}
else
{
if (right)
return compareChildPerOrder(order, right);
else
return compareChildPerOrder(order, left);
}
}
int Node::height()
{
VACANCY vacancy = vacant();
if (vacancy == BOTH)
return 0;
else if (vacancy == NONE)
{
int l = left->height();
int r = right->height();
return ((l > r) ? (l) : (r)) + 1;
}
else if (vacancy == RIGHT)
return left->height() + 1;
else
return right->height() + 1;
}
Node* Node::highestChild()
{
VACANCY vacancy = vacant();
if (vacancy == BOTH)
return NULL;
else if (vacancy == NONE)
{
return ((left->height() > right->height()) ? (left) : (right));
}
else if (vacancy == RIGHT)
return right;
else
return left;
}
void Node::makeVacant()
{
left = NULL;
right = NULL;
}