-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskHeap.java
More file actions
201 lines (177 loc) · 5.91 KB
/
TaskHeap.java
File metadata and controls
201 lines (177 loc) · 5.91 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* A heap, implemented as an array.
* The elements in the heap are instances of the class TaskElement,
* and the heap is ordered according to the Task instances wrapped by those objects.
*
* IMPORTANT: Except the percolation (private) functions, no single function may loop/recurse through all elements in the heap.
* You may only use loops which look at a small fraction of the heap.
*
*
*/
/*
Assignment number : 8
File Name : TaskHeap.java
Name: Ran Zaaroor
Student ID : 209374040
Email : Ran.zaaroor@gmail.com
*/
public class TaskHeap{
public static int capacity=200; // the maximum number of elements in the heap
/*
* The array in which the elements are kept according to the heap order.
* The following must always hold true:
* if i < size then heap[i].heapIndex == i
*/
TaskElement[] heap;
int size; // the number of elements in the heap, it is required that size <= heap.length
/**
* Creates an empty heap which can contain 200 elements.
*/
public TaskHeap(){
this.heap = new TaskElement[capacity+1];
this.size = 0;
}
/**
* Constructs a heap from a given arbitrary array of TaskElements of size at most 'capacity'.
* This should be done according to the "buildheap" function studied in class.
* NOTE: the heapIndex field of each TaskElement might be -1 (or incorrect).
* You may NOT use the insert function of heap.
* NOTE: for this function you may use loops.
*
*/
public TaskHeap(TaskElement[] arr) {
this.heap = new TaskElement[capacity+1];
this.size = arr.length;
//copy the given array into the heap, shifting the elements by 1
for(int i=0; i<arr.length;i++) {
heap[i+1]=arr[i];
heap[i+1].heapIndex=i+1;
}
//building the heap
for (int i = size/2+1; i >= 1; i--) {
this.percolateDown(i);
}
}
/**
* Returns the size of the heap.
*
* @return the size of the heap
*/
public int size(){
return size;
}
/**
* Inserts a given element into the heap.
*
* @param e - the element to be inserted.
*/
public void insert(TaskElement e){
heap[size + 1] = e;
e.heapIndex = size + 1;
size++;
percolateUp(size);
}
/**
* Returns and does not remove the element which wraps the task with maximal priority.
*
* @return the element which wraps the task with maximal priority.
*/
public TaskElement findMax(){
return heap[1];
}
/**
* Returns and removes the element which wraps the task with maximal priority.
*
* @return the element which wraps the task with maximal priority.
*/
public TaskElement extractMax() {
TaskElement max = findMax();
if (size == 1) {
heap[1] = null;
return max;
}
remove(1);
return max;
}
/**
* Removes the element located at the given index.
*
* Note: this function is not part of the standard heap API.
* Make sure you understand how to implement it, and why it is required.
* There are several ways this function could be implemented.
* No matter how you choose to implement it, you need to consider the different possible edge cases.
* @param index
*/
public void remove(int index){
swap(index, size);
heap[size] = null;
size--;
percolateDown(index);
}
private void percolateUp(int index) {
if(index == 1) return;
Task child = heap[index].t;
Task parent = heap[index/2].t;
if (child.compareTo(parent) < 0) return;
swap(index, index/2);
percolateUp(index/2);
}
private void swap(int childIndex, int parentIndex) {
TaskElement child = heap[childIndex];
TaskElement parent = heap[parentIndex];
heap[childIndex] = parent;
heap[parentIndex] = child;
child.heapIndex = parentIndex;
parent.heapIndex = childIndex;
}
private void percolateDown (int index) {
if(2*index > size) return;
Task parent = heap[index].t;
if(2*index == size) {
Task leftChild = heap[2*index].t;
if (parent.compareTo(leftChild) < 0)
swap(index, 2*index);
return;
}
Task leftChild = heap[2*index].t;
Task rightChild = heap[2*index + 1].t;
if(leftChild.compareTo(rightChild) > 0){
if(parent.compareTo(leftChild) < 0){
swap(index, 2*index);
percolateDown(2*index);
}
} else
if(parent.compareTo(rightChild) < 0){
swap(index, 2*index + 1);
percolateDown(2*index + 1);
}
}
public static void main (String[] args){
/*
* A basic test for the heap.
* You should be able to run this before implementing the queue.
*
* Expected outcome:
* task: Add a new feature, priority: 10
* task: Solve a problem in production, priority: 100
* task: Solve a problem in production, priority: 100
* task: Develop a new feature, priority: 10
* task: Code Review, priority: 3
* task: Move to the new Kafka server, priority: 2
*
*/
Task a = new Task(10, "Add a new feature");
Task b = new Task(3, "Code Review");
Task c = new Task(2, "Move to the new Kafka server");
TaskElement [] arr = {new TaskElement(a), new TaskElement(b), new TaskElement(c)};
TaskHeap heap = new TaskHeap(arr);
System.out.println(heap.findMax());
Task d = new Task(100, "Solve a problem in production");
heap.insert(new TaskElement(d));
System.out.println(heap.findMax());
System.out.println(heap.extractMax());
System.out.println(heap.extractMax());
System.out.println(heap.extractMax());
System.out.println(heap.extractMax());
}
}