-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassHeapTree.cpp
More file actions
165 lines (150 loc) · 4.71 KB
/
Copy pathClassHeapTree.cpp
File metadata and controls
165 lines (150 loc) · 4.71 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
#include <iostream>
#include "ClassHeapTree.h"
using namespace std;
HeapTree::HeapTree()
{
this->root = NULL;
this->tail = NULL;
this->size = 0;
}
HeapTree::~HeapTree()
{
// delete this->root;
}
HeapTreeNode* HeapTree::getRoot(){
return this->root;
}
int HeapTree::getSize(){
return this->size;
}
//CLASS FUNCTIONS
void HeapTree::insertItem(Record* recordPtr){
//If item already exists in our heap then just update its data, else insert it in heap
if(this->root->contains(recordPtr)){
this->updateNode(recordPtr);
} else {
if(this->root == NULL){
this->root = new HeapTreeNode(recordPtr);
this->tail = this->root;
} else if (tail->getLeftNode() == NULL){
HeapTreeNode* newNode = new HeapTreeNode(recordPtr);
this->tail->setLeftNode(newNode);
this->tail->getLeftNode()->setParentNode(this->tail);
this->tail->getLeftNode()->maxHeapify();
//delete newNode;
} else {
HeapTreeNode* newNode = new HeapTreeNode(recordPtr);
this->tail->setRightNode(newNode);
this->tail->getRightNode()->setParentNode(this->tail);
this->tail->getRightNode()->maxHeapify();
HeapTreeNode* prevTail = this->tail;
this->setTail(this->tail);
this->tail->setPrevTail(prevTail);
//delete newNode;
//delete prevTail;
}
this->size++;
}
}
void HeapTree::setTail(HeapTreeNode* node){
/*
If we reach this stage that means a level is completely filled
and we need to proceed to the next level by going to the extreme left.
*/
if (node->getParentNode() == NULL){
this->tail = node;
while(this->tail->getLeftNode() != NULL){
this->tail = this->tail->getLeftNode();
}
}
/*
If current node is the left node, go to the right node and
proceed left from there to reach the left most node.
*/
else if (node->getParentNode()->getLeftNode() == node){
this->tail = node->getParentNode()->getRightNode();
while(this->tail->getLeftNode() != NULL){
this->tail = this->tail->getLeftNode();
}
}
else if(node->getParentNode()->getRightNode() == node){
this->setTail(node->getParentNode());
}
}
void HeapTree::showHeap(){
int space = 0;
this->root->printHeap(space);
}
void HeapTree::updateNode(Record* recordPtr){
HeapTreeNode* toBeUpdated = this->root->searchNode(recordPtr);
toBeUpdated->increaseKey();
toBeUpdated->maxHeapify();
}
void HeapTree::deleteRoot(){
if(this->size == 1){ //When only the root exists
this->root = NULL;
this->size = 0;
return;
}
if(this->size == 2){ //One root one children
if(this->root->getRightNode() != NULL){
this->root->swap(this->root->getRightNode());
this->root->setRightNode(NULL);
}else{
this->root->swap(this->root->getLeftNode());
this->root->setLeftNode(NULL);
}
}else if(this->tail == this->root){ //One root two children
this->tail = NULL;
HeapTreeNode* max = this->root->getLeftNode();
if(max < this->root->getRightNode()){
this->root->swap(this->root->getRightNode());
this->root->setRightNode(NULL);
}else{
this->root->swap(this->root->getLeftNode());
this->root->setLeftNode(NULL);
}
}else{
if(this->tail->getRightNode() != NULL){
this->root->swap(this->tail->getRightNode());
this->tail->setRightNode(NULL);
this->root->revMaxHeapify();
}else if(this->tail->getLeftNode() != NULL){
this->root->swap(this->tail->getLeftNode());
this->tail->setLeftNode(NULL);
this->root->revMaxHeapify();
}else{
this->tail = this->tail->getPrevTail();
this->deleteRoot();
this->size++;
}
}
this->size--;
}
//Query Functions
void HeapTree::getTopKDiseases(int K){
int i;
HeapTreeNode* node;
for(i = 0; i < K; i++){
if(this->root == NULL){
cout << "No more results!" << endl;
return;
}
node = this->getRoot();
cout << "Virus: " << node->getRecordPtr()->getDisease() << endl;
this->deleteRoot();
}
}
void HeapTree::getTopKCountries(int K){
int i;
HeapTreeNode* node;
for(i = 0; i < K; i++){
if(this->root == NULL){
cout << "No more results!" << endl;
return;
}
node = this->getRoot();
cout << "Country: " << node->getRecordPtr()->getCountry() << endl;
this->deleteRoot();
}
}