-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinHeap.cpp
More file actions
97 lines (78 loc) · 1.76 KB
/
Copy pathMinHeap.cpp
File metadata and controls
97 lines (78 loc) · 1.76 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
/*
* MinHeap.cpp
*
* Created on: Apr 20, 2023
* Author: süleyman yağız başaran
* id: 22103782
* sec: 3
* hw: 3
*/
#include <string.h>
#include <iostream>
using namespace std;
#include "MinHeap.h"
#include "Food.h"
// Default constructor
MinHeap::MinHeap() {
size = 0;
}
bool MinHeap::heapIsEmpty() {
return (size == 0);
}
void MinHeap::heapDelete(Food &rootItem) {
//if (heapIsEmpty())
// return;
//else {
rootItem = items[0];
items[0] = items[--size];
heapRebuild(0);
//}
}
void MinHeap::heapRebuild(int root) {
int leftChild = 2 * root + 1; // index of root's left child, if any
if ( leftChild < size ) {
// root is not a leaf so that it has a left child
int rightChild = leftChild + 1; // index of a right child, if any
// If root has right child, find larger child
if ( (rightChild < size) && (items[rightChild].spawnTime < items[leftChild].spawnTime) )
{//<=
leftChild = rightChild;
}
// If root’s item is smaller than larger child, swap values
if ( items[root].spawnTime > items[leftChild].spawnTime ) { // >=
Food temp = items[root];
items[root] = items[leftChild];
items[leftChild] = temp;
/*
}*/
// transform the new subtree into a heap
heapRebuild(leftChild);
}
}
}
//helpers
int MinHeap::getSize(){
return size;
}
Food MinHeap::peek(){
return items[0];
}
void MinHeap::heapInsert(Food &newItem){
//if (size >= MIN_HEAP)
// return;
items[size] = newItem;
int place = size;
int parent = (place - 1)/2;
while ( (place > 0) && (items[place].spawnTime < items[parent].spawnTime) ) { // <
Food temp = items[parent];
items[parent] = items[place];
items[place] = temp;
place = parent;
parent = (place - 1)/2;
// }
/*
else if(
else break;*/
}
++size;
}