SUNY Geneseo Department of Computer Science


Heap Insertion

{Date}

CSci 240, Spring 2007
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture

Misc

SOFIs

Final exam

Problem Set 14 due today

Questions?

Heap Insertion

Finish section 14.4, from "Insertion" (page 500) through end (page 503)

Flesh out the bottom-up insertion algorithm in terms of a tree in an array

    // in class MaxHeap<E extends Comparable<...> >
    private E[] elements              // 0-origin
    private int n

    insertBottomUp( v )
        elements[n] = v
        n = n + 1
        i = n - 1
        while ( (i-1)/2 >= 0 && elements[i] > elements[ (i-1)/2 ] )
            swap( i, (i-1)/2 )
            i = (i-1)/2

    void swap( int a, int b )
        E x = elements[a]
        elements[a] = elements[b]
        elements[b] = x

Swap and object representation

[Swap May Change which Objects Parameters Refer To without Changing Client's View]

Next

Finish heap insertion and deletion algorithms.

Mini-assignment above re sifting down.


Next Lecture