-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventHeap.java
167 lines (136 loc) · 3.88 KB
/
EventHeap.java
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
/*Whatcom Community College - Winter 2019
CS240 Data Structures and Algorithm Analysis
Professor Ryan Parsons
AUTHORS: Adib Thaqif, Andrew Jacobi, Donald Strong, and Micah Miller
Heap implementation for choosing an event in the help feature.
Implemented using an array
Problems: exceptions, Floyd's method not used
change mutators:
deleteMin, insert, percolate Up and Down to consider the heap property
*/
import java.io.*;
import java.util.*;
import java.lang.Exception.*;
public class EventHeap{
private Event[] Heap;
private int size = 0;
public int maxSize;
//private int eventValue = 0;
public EventHeap(int maxSize){
this.size = size;
this.maxSize = maxSize;
Heap = new Event[maxSize + 1];
//this.eventValue = event.reward + Math.abs(event.punishment);
}
public int length(){
return size;
}
public Event get(int pos){
return Heap[pos];
}
public Event parent(int pos){
return Heap[pos/2];
}
public Event leftChild(int pos){
return Heap[2*pos];
}
public Event rightChild(int pos){
return Heap[2*pos + 1];
}
private boolean isLeaf(int pos){
return (pos >= (size/2) && pos <= size);
}
public boolean isEmpty(){
return (size == 0);
}
private int eventValue(Event event){
int x = 0;
if(event != null){
x = event.reward + Math.abs(event.punishment);
}
return x;
}
public boolean isFull(){
return (size == Heap.length-1);
}
public String toString(){
if(isEmpty()){
throw new IllegalArgumentException();
}
return Arrays.toString(Heap);
}
private Event findMin(){
if(isEmpty()){
throw new ArrayStoreException("Heap is empty");
}
return Heap[1];
}
private void emptyHeap(){
for(int i = 0; i < size; i++){
Heap[i] = null;
}
}
private void resize(){
if(size == maxSize - 1){
throw new IllegalArgumentException("Size of heap is full!");
}
}
public void insert(Event event){
if (isFull()){
resize();
}
size++;
//System.out.println("size = " + size);
//Heap[size+1] = value;
int i = percolateUp(size,event);
Heap[i] = event;
}
//check aspect values first
//add from hashtable storage.
public Event deleteMin(){
if(isEmpty()){
throw new IllegalArgumentException();
}
Event minEvent = Heap[1];
int hole = percolateDown(1,Heap[size]);
Heap[hole] = Heap[size];
size--;
return minEvent;
}
private int percolateUp(int hole, Event event){
//System.out.println(Heap[hole/2] + "");
while(hole > 1 && eventValue(event) < eventValue(Heap[hole/2])){
Heap[hole] = Heap[hole/2];
hole = hole/2;
}
return hole;
//System.out.println(hole);
}
private int percolateDown(int hole, Event event){
int target;
while(!isLeaf(hole)){
int left = 2*hole;
int right = left + 1;
if(eventValue(Heap[left]) < eventValue(Heap[right]) || (right > size)){
target = left;
}else{
target = right;
}
if(eventValue(Heap[target]) < eventValue(event)){
Heap[hole] = Heap[target];
hole = target;
}else{
break;
}
}
return hole;
}
//using Floyd's method to buildHeap
public void buildHeap(){
for (int i = size/2; i > 0; i--){
Event event = Heap[i];
int hole = percolateDown(i,event);
Heap[hole] = event ;
}
}
}