-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassHeapTreeNode.cpp
More file actions
172 lines (131 loc) · 3.9 KB
/
ClassHeapTreeNode.cpp
File metadata and controls
172 lines (131 loc) · 3.9 KB
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
164
165
166
167
168
169
170
171
172
#include <iostream>
#include "ClassRecord.h"
#include "ClassHeapTreeNode.h"
using namespace std;
HeapTreeNode::HeapTreeNode(Record* recordPtr) {
this->recordPtr = recordPtr;
this->rightNode = NULL;
this->leftNode = NULL;
this->parentNode = NULL;
this->prevTail = NULL;
this->occurences = 1;
}
HeapTreeNode::~HeapTreeNode(){
// delete this->recordPtr;
// delete this->leftNode;
// delete this->rightNode;
}
Record* HeapTreeNode::getRecordPtr() {
return this->recordPtr;
}
HeapTreeNode* HeapTreeNode::getParentNode() {
return this->parentNode;
}
HeapTreeNode* HeapTreeNode::getRightNode() {
return this->rightNode;
}
HeapTreeNode* HeapTreeNode::getLeftNode() {
return this->leftNode;
}
HeapTreeNode* HeapTreeNode::getPrevTail() {
return this->prevTail;
}
int HeapTreeNode::getOccurences()
{
return this->occurences;
}
void HeapTreeNode::setRecordPtr(Record* recordPtr){
this->recordPtr = recordPtr;
}
void HeapTreeNode::setParentNode(HeapTreeNode* Node){
this->parentNode = Node;
}
void HeapTreeNode::setRightNode(HeapTreeNode* Node){
this->rightNode = Node;
}
void HeapTreeNode::setLeftNode(HeapTreeNode* Node){
this->leftNode = Node;
}
void HeapTreeNode::setPrevTail(HeapTreeNode* Node){
this->prevTail = Node;
}
void HeapTreeNode::setOccurences(int newOccurences){
this->occurences = newOccurences;
}
//CLASS FUNCTIONS
void HeapTreeNode::maxHeapify(){
if(this->getParentNode() != NULL){
if(this->getParentNode()->getOccurences() < this->getOccurences()){
this->swap(this->getParentNode());
this->getParentNode()->maxHeapify();
}
}
}
void HeapTreeNode::swap(HeapTreeNode* newNode){
int temp = this->getOccurences();
this->setOccurences(newNode->getOccurences());
newNode->setOccurences(temp);
Record* tempPtr = this->getRecordPtr();
this->setRecordPtr(newNode->getRecordPtr());
newNode->setRecordPtr(tempPtr);
}
bool HeapTreeNode::contains(Record* recordPtr) {
if (this == NULL){
return false;
}
if (this->recordPtr->getCountry() == recordPtr->getCountry() && this->recordPtr->getDisease() == recordPtr->getDisease()){
return true;
}
/* then recur on left sutree */
bool res1 = this->leftNode->contains(recordPtr);
if(res1) return true; // node found, no need to look further
/* node is not found in left, so recur on right subtree */
bool res2 = this->rightNode->contains(recordPtr);
return res2;
}
void HeapTreeNode::increaseKey(){
this->occurences++;
}
HeapTreeNode* HeapTreeNode::searchNode(Record* recordPtr){
HeapTreeNode* res = NULL;
// Base Cases: this is null or key is present at root
if (this == NULL) {
return NULL;
}
if (this->getRecordPtr()->getCountry() == recordPtr->getCountry() && this->getRecordPtr()->getDisease() == recordPtr->getDisease())
return this;
// recur on left subtree
res = this->leftNode->searchNode(recordPtr);
if(res != NULL){
return res;
}
// recur on right subtree
res = this->rightNode->searchNode(recordPtr);
return res;
}
void HeapTreeNode::revMaxHeapify(){
if(this == NULL || this->getLeftNode() == NULL){
return;
}
HeapTreeNode* max = this->getLeftNode();
if(this->getRightNode() != NULL && max->getOccurences() < this->getRightNode()->getOccurences()){
max = this->getRightNode();
}
if(max->getOccurences() > this->getOccurences()){
this->swap(max);
max->revMaxHeapify();
}
}
void HeapTreeNode::printHeap(int space){
if(this == NULL){
return;
}
space += 10;
this->getRightNode()->printHeap(space);
cout << endl;
for(int i = 10; i < space; i++){
cout << " ";
}
cout << this->getRecordPtr()->getDisease() << "/" << this->getOccurences() << endl;
this->getLeftNode()->printHeap(space);
}