1/4
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
downheap (max heap)
while index < size:
left = 2 * index + 1
right = 2 * index + 2
largest = index // assume current node is largest
if left < size and table[left] > table[largest]:
largest = left
if right < size and table[right] > table[largest]:
largest = right
if largest == index:
break // heap property satisfied
swap(table[index], table[largest])
index = largestupheap (max heap)
while index > 1:
parent = index / 2;
if parent value < index value:
save parent value;
set parent value to index value;
set index value to saved parent value;
set index to parent index;
else:
break;remove(){}
public T remove() {
save root
set new root to last element in backing array
set last element in backing array to null
decrement size
call downheap on root
return root
}add
public void add(T data) {
// handle null data
if resize necessary, resize
add data to the back of the backing array
increment size
upheap on newly added data
}maxHeap constructor with buildHeap()
public MaxHeap(ArrayList<T> data) {
if (data is null) {
// throw
}
initialize backingArray;
set size to data.size();
for every element in data {
if (element is null) {
// throw
}
add element to backing array
}
for (int i = (size / 2) - 1; i >= 0; i--) {
downHeap(i);
}
}