11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3.5 Modeling Objects • 107> nitems := 0;> heap := table();>> bubbleup := proc( child::posint )> local parent;> parent := iquo( child, 2 );> if child > 1> and priority( heap[ child ] ) > priority( heap[> parent ] ) then> heap[ parent ], heap[ child ] := heap[ child ],> heap[ parent ];> procname( parent ) # recurse> end if> end proc;>> bubbledown := proc( parent::posint )> local child;> child := 2 * parent;> if child < nitems> and priority( heap[ 1 + child ] ) > priority(> heap[ child ] ) then> child := 1 + child> end if;> if child and priority( heap[ parent ] ) < priority( heap[> child ] ) then> heap[ parent ], heap[ child ] := heap[ child ],> heap[ parent ];> procname( child ) # recurse (new parent)> end if> end proc;>> # Initialize the priority queue.> init := proc()> heap := table();> nitems := 0> end proc;>> # Test whether the priority queue is empty.> empty := () -> evalb( nitems < 1 );>> # Return the number of items on the priority queue.> size := () -> nitems;>> # Query the highest priority item.> top := proc()> if empty() then> error "priority queue is empty"> else> heap[ 1 ]> end if> end proc;>> # Delete the highest priority item from the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!