-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryHeap.java
More file actions
211 lines (168 loc) · 5.6 KB
/
BinaryHeap.java
File metadata and controls
211 lines (168 loc) · 5.6 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
202
203
204
205
206
207
208
209
package binaryheap;
package lab10;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.Queue;
/**
* This class implements a binary heap data structure by extending the
* ArrayList class.
* It also implements the java.util.Queue interface so that it can be
* used with the framework problem solver.
* @author tcolburn
* @param <E> the type of element stored on this binary heap
*/
public class BinaryHeap<E> extends ArrayList<E> implements Queue<E> {
/**
* Creates an empty binary heap with a given capacity and comparator.
* @param capacity The initial size of the underlying ArrayList object.
* @param comp A comparator object for comparing keys of binary heap elements.
*/
public BinaryHeap(int capacity, Comparator<E> comp) {
super(capacity+1);
init();
this.comp = comp;
}
/**
* Getter for the comparator for this binary heap.
* @return the comparator for this binary heap
*/
public Comparator<E> getComp() {
return comp;
}
/**
* Initializes the underlying ArrayList object for use as a binary heap.
* A null object is added to location 0, which is not used by the heap.
*/
private void init() {
add(0, null);
}
/**
* Clears this binary heap by clearing and initializing the underlying
* ArrayList object.
*/
@Override
public void clear() {
super.clear();
init();
}
/**
* Returns the current size of this binary heap. Since the first location
* (index 0) of the underlying ArrayList object is not used, the size of the
* binary heap is one less than the size of the ArrayList object.
* @return The binary heap's current size.
*/
@Override
public int size() {
return super.size()-1;
}
/**
* Returns true if this binary heap is empty, that is, its size is zero.
* @return Whether this binary heap is empty.
*/
@Override
public boolean isEmpty() {
return size() == 0;
}
/**
* Adds a new element to this binary heap. At the end of the add,
* the heap has one more element and the heap property is maintained.
* @param element The element to add
* @return true. In accordance with the Collection interface, returns
* true since duplicate elements are allowed.
*/
@Override
public boolean add(E element) {
// add element to the ArrayList, by default, add to the end of list;
super.add(element);
if (this.size() == 1)
return true; //Only contains 1 root node, no need to compare
else {
//create a hole at the end of the list; what is the index for the end of list??
int hole = _______________;
//Get parent index; what is the parent idex?
int parent = _______________;
//Get the list comparator to compare two objects. We will cover this later.
Comparator<E> comp = this.getComp();
//Iteratively bubble up the hole
for (; (comp.compare(element,this.get(parent)) < 0); hole = parent )
{
//if the element is less than its parent, you can
//copy the parent value to position indexed by the hole;
set(hole, _________________);
//Update parent index in order to move one level up
parent = ______________;
//Check if it reaches the root node, if yes then break out the loop
if (parent == 0)
break;
}
//
set(hole, element);
}
return true;
}
/**
* Removes an element from the root of this binary heap. After removal,
* the heap has one less element and the heap property is restored.
* This method does not override any method in the ArrayList class
* (note absence of an index parameter).
* However, it implements a method in the Queue interface.
* @return The removed element
*/
@Override
public E remove() {
/* You must complete this part. */
// if empty heap, throw exception
if(this.size()==0)
throw new NoSuchElementException();
// if only contains the root node, remove it by calling ArrayList's default remove();
if(this.size()==1)
return super.remove(this.size());
// Get minimum element in the list, at index of 1, to be removed;
E minElement = _________________;
// Remove the last element in the list, which is used to fill the hole at root;
E lastElement = super.remove(_________________);
// Copy last element to the root (i.e. the hole)
this.set(1, lastElement);
// Fix heap order property
bubbleDown(1);
return minElement;
}
private void bubbleDown(int hole){
int child;
E element = this.get(1);
Comparator<E> comp = this.getComp();
//Implement the iterative bubble down to swap element with the large child
// To compare the values of children, use the following syntax:
// comp.compare(this.get(child+1), this.get(child))<0
// which means that the right child is smaller
// After the for loop above, you find the right position, i.e. the hole index
// where you should save the element.
this.set(hole, element);
}
/**
* A Comparator object used to compare binary heap elements during its
* add and remove operations.
*/
private final Comparator<E> comp;
/*
The following are required to complete the implementation of the Queue<E>
interface. They are not used in the test.
*/
@Override
public boolean offer(E e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public E poll() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public E element() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public E peek() {
throw new UnsupportedOperationException("Not supported yet.");
}
}