Heap psudocode

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/4

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 8:37 PM on 3/31/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

5 Terms

1
New cards

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 = largest

2
New cards

upheap (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;

3
New cards

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
}

4
New cards

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
}

5
New cards

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);
    }

}

Explore top flashcards

flashcards
Filmgeschiedenis 2 (2022-2023)
134
Updated 1029d ago
0.0(0)
flashcards
Essen und Trinken
59
Updated 108d ago
0.0(0)
flashcards
Semester 1 Final: Names
37
Updated 1204d ago
0.0(0)
flashcards
A Raisin in the Sun
30
Updated 674d ago
0.0(0)
flashcards
Economics
31
Updated 1084d ago
0.0(0)
flashcards
compscipaper2.0
100
Updated 36d ago
0.0(0)
flashcards
Filmgeschiedenis 2 (2022-2023)
134
Updated 1029d ago
0.0(0)
flashcards
Essen und Trinken
59
Updated 108d ago
0.0(0)
flashcards
Semester 1 Final: Names
37
Updated 1204d ago
0.0(0)
flashcards
A Raisin in the Sun
30
Updated 674d ago
0.0(0)
flashcards
Economics
31
Updated 1084d ago
0.0(0)
flashcards
compscipaper2.0
100
Updated 36d ago
0.0(0)