Walls - Lecture Slides - Algorithms and Data Structures | CS 200, Study notes of Computer Science

Material Type: Notes; Professor: Howe; Class: Algorithms and Data Structures; Subject: Computer Science; University: Colorado State University; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 03/18/2009

koofers-user-rwi
koofers-user-rwi 🇺🇸

5

(1)

10 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS200: Priority Queues
and Heaps
Walls Ch. 12:
Table
Priority Queues
heaps
heap sort
ADT Table
Manage data by value
Operations
1. Create empty
2. Is empty?
3. Size
4. Insert new item
5. Delete item with search key
6. Retrieve item by search key
7. Traverse items in sorted order
Pseudocode for Table ADT
createTable() // creates an empty table
tableIsEmpty():boolean {query}
// Determines whether a table is empty
tableLength():integer {query}
// Determines the number of items in a table
tableTraverse():TableItemType
// Traverses a table in sorted search key order.
Pseudocode for Table ADT
(cont.)
tableInsert(in newItem:TableItemType) throws
TableException
//Inserts newItem into a table whose items have distinct
// search keys that differ from newItem’ s search key.
// throws exception if unsuccessful.
tableDelete(in searchKey:KeyType): boolean
// Deletes from a table the item whose search key equals searchKey.
// Returns true if successful, false if no item found.
tableRetrieve(in searchKey:KeyType):TableItemType
// Returns item whose search key equals searchKey. Returns null
// if not there.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Walls - Lecture Slides - Algorithms and Data Structures | CS 200 and more Study notes Computer Science in PDF only on Docsity!

CS200: Priority Queues

and Heaps

Walls Ch. 12:

 Table

 Priority Queues

 heaps

 heap sort

ADT Table

 Manage data by value

 Operations

  1. Create empty
  2. Is empty?
  3. Size
  4. Insert new item
  5. Delete item with search key
  6. Retrieve item by search key
  7. Traverse items in sorted order

Pseudocode for Table ADT

 createTable() // creates an empty table

 tableIsEmpty():boolean {query}

// Determines whether a table is empty

 tableLength():integer {query}

// Determines the number of items in a table

 tableTraverse():TableItemType

// Traverses a table in sorted search key order.

Pseudocode for Table ADT

(cont.)

 tableInsert(in newItem:TableItemType) throws

TableException

//Inserts newItem into a table whose items have distinct

// search keys that differ from newItem’s search key.

// throws exception if unsuccessful.

 tableDelete(in searchKey:KeyType): boolean

// Deletes from a table the item whose search key equals searchKey.

// Returns true if successful, false if no item found.

 tableRetrieve(in searchKey:KeyType):TableItemType

// Returns item whose search key equals searchKey. Returns null

// if not there.

Table Items

public abstract class KeyedItem> {

private KT searchKey;

public KeyedItem(KT key) {

searchKey = key; }

public KT getKey() {

return searchKey; }

Table or Dictionary Example

Date Title Artist(s)

2 3/24/06 Unwritten Natasha Bedingfeld

4 3/24/06 Temperature Sean Paul

3 3/24/06 Move Along The All-American Rejects

5 3/24/06 Beep The Pussycat Dolls

1 3/24/06 Bad Day Daniel Powter

iTunes Top Songs

Table Items Example

Public class TopTunes extends KeyedItem {

private String title; // search key

private int rank; private String artist;

private String date;

public TopTunes(String theTitle, int theRank, …) {

super(theTitle); rank = theRank; …}

Table Interface

public interface TableInterface,

KT extends Comparable > {

// Precondition for all operations:

// No two items of the table have the same search key.

// The table's items are sorted by search key.

public boolean tableIsEmpty();

// Determines whether a table is empty.

// Postcondition: Returns true if the table is empty; false otherwise

public int tableLength();

// Determines the length of a table.

// Postcondition: Returns the number of items in the table.

Priority Queue ADT Operations

1. Create an empty priority queue

createPQueue()

2. Determine whether empty

pqIsEmpty():boolean {query}

3. Insert new item

pqInsert(in newItem:PQItemType) throws PQueueException

4. Retrieve and delete the item with the highest

priority

pqDelete():PQItemType

Priority Queue – Implementation

 ArrayList ordered by priority

 Sorted ArrayList

 location of maximum element – position 0

 add – find the insertion location

 Unsorted ArrayList

 add – at end of vector

 find the maximum element (linear search)

 Linked list

 Binary search tree

Heaps

 Characteristics

 complete binary tree

 root holds the maximum value

 each subtree is also a heap

 values in descending order on every path from

root to leaf

root of tree holds maximum value (global)

each node is greater than its two children (local)

(this is called: “the heap property”)

Heap Examples - Validity

Valid

COMPLETE

Valid

INCOMPLETE

Not Valid

Heap ADT

 createHeap() // create empty heap

 heapIsEmpty():boolean {query}

// determines if empty

 heapInsert(in newItem:HeapItemType) throws

HeapException

// inserts newItem based on its search key. Throws

// exception if heap is full

 heapDelete():HeapItemType

// retrieves and then deletes heap’s root item, which has

// largest search key

Implementation:

ArrayList-based Heap

 Addressing

 root at position 0

 left child of position i at position 2i+

 right child of position i at position 2(i+1)

 parent of position i at position (i-1)/2

ArrayList-based Binary Tree –

Priority Queue Example

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

30 5 25 20 15

Why is there so much wasted space?

ArrayList-based Heap –

Example

0 1 2 3 4 5 6

30 20 25 10 15 5

What is the property of a complete heap in an ArrayList?

Heap Operations - heapInsert

 put a new value into first open position (maintaining

completeness)

 percolate values up

Re-enforcing the heap property

 swap with parent until in the right place

0 1 2 3 4 5 6

7 Add 36 to the heap

Heap Insert Pseudocode

// insert newItem into bottom of tree

items.add(newItem,items.size())

// trickle new item up to appropriate spot

place = items.size()-

parent = (place-1)/

while ((parent >= 0) and (items.get(place) > items.get(parent))) {

swap data at position place and position parent

place = parent

parent = (place-1)/

Heap operations – heapDelete

 always remove value at root (Why?)

 substitute with rightmost leaf of bottom level

(Why?)

 percolate down

 swap with maximum child as necessary

0 1 2 3 4 5 6

Delete from heap

Heap operations – heapDelete

 always remove value at root (Why?)

 substitute with rightmost leaf of bottom level

(Why?)

 percolate down

 swap with maximum child as necessary

0 1 2 3 4 5 6

Delete from heap

Save 36

Heap operations – heapDelete

 always remove value at root (Why?)

 substitute with rightmost leaf of bottom level

(Why?)

 percolate down

 swap with maximum child as necessary

0 1 2 3 4 5 6

Delete from heap

Return 36

heapDelete Pseudocode

// return the item in root

rootItem = items.get(0)

//copy item from last node into root

Items.set(0,rootItem) = items.get(size-1)

  • -size

heapRebuild(items, 0, size)

return rootItem

heapRebuild Pseudocode

heapRebuild(items:ArrayList, root:integer, size:integer)

if (root is not a leaf) {

child = 2 * root + 1 // left child

if (root has right child) {

rightChild = child + 1

if (items.get(rightChild).getKey() > items.get(child).getKey()) {

child = rightChild } } // larger child

if (items.get(root).getKey() < items.get(child).getKey()) {

swap data at position root with position child

heapRebuild(items, child, size)

ArrayList-based Heaps -

Order of Complexity

delete

insert

Best Case Worst Case

O(c)

O(log n)

because tree is complete

O(c)

O(n)

because of ensure-capacity