-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapMax.cpp
More file actions
126 lines (106 loc) · 2.12 KB
/
Copy pathHeapMax.cpp
File metadata and controls
126 lines (106 loc) · 2.12 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
#include "HeapMax.h"
int HeapMax::Left(int node)
{
return (2 * node + 1);
}
int HeapMax::Right(int node)
{
return (2 * node + 2);
}
int HeapMax::Parent(int node)
{
return (node - 1) / 2;
}
void HeapMax::FixHeap(int node)
{
int max;
int left = Left(node);
int right = Right(node);
//find maximum amond data, left and right
if ((left < heapSize) && (data[left]->priority > data[node]->priority))
max = left;
else
max = node;
if ((right < heapSize) && (data[right]->priority > data[max]->priority))
max = right;
if (max != node)
{
Swap(data[node], data[max]);
FixHeap(max);
}
}
Pair HeapMax::Max()
{
return *(data[0]);
}
Pair* HeapMax::DeleteMax()
{
if (heapSize < 1)
cout << "error" << endl;
Pair* max = data[0];
heapSize--;
data[0]= data[heapSize];
data[0]->index = 0;
FixHeap(0);
return (max);
}
void HeapMax::Insert(Pair* item)
{
if (heapSize == MAXSIZE)
cout << "error" << endl;
int i = heapSize;
heapSize++;
while ((i > 0) && (data[Parent(i)]->priority < item->priority))
{
data[i] = data[Parent(i)];
data[i]->index = i;
i = Parent(i);
}
data[i] = item;
item->index= i;
}
void HeapMax::Swap(Pair* pair1, Pair* pair2)
{
Pair temp;
temp = *pair1;
*pair1 = *pair2;
*pair2 = temp;
// swaping the index
int tempIndex;
tempIndex = pair1->index;
pair1->index = pair2->index;
pair2->index = pair1->index;
}
void HeapMax::DeleteIMax(int i)
{
Pair* right = data[heapSize - 1];
data[i] = right;
data[i]->index = i;
if (Right(i) < heapSize)
{
//not ok with his kids
if (data[i]->priority < data[Right(i)]->priority)
FixHeap(i);
}
else if (Left(i) < heapSize)
{
if (data[i]->priority < data[Left(i)]->priority)
FixHeap(i);
}
if (i != 0)
{
//not ok with his parent
while (data[i]->priority > data[Parent(i)]->priority)
Swap(data[i], data[Parent(i)]);
}
heapSize--;
}
int HeapMax::returnIndex(Pair x)
{
for (int i = 0; i < heapSize; i++)
{
if ((data[i]->data == x.data) && (data[i]->priority == x.priority))
return i;
}
return UNDEFINED;
}