Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 62 Data Structures Dr. Joshua Stough October 23, 2008.

Similar presentations


Presentation on theme: "CSCI 62 Data Structures Dr. Joshua Stough October 23, 2008."— Presentation transcript:

1 CSCI 62 Data Structures Dr. Joshua Stough October 23, 2008

2 Today Huffman Trees Priority Queues Heaps –Skew heaps –Sorting with heaps

3 Huffman Trees More common characters represented using fewer bits.

4 Huffman Trees Text compression.

5 Huffman Trees

6

7 Take a look on the schedule for Program 6. Due Nov. 3, day before review for Midterm, which is Nov. 6. –Stacks/Queues, –Binary Structures, –Ordered Structures, –Priority Queues, –Heaps, and –some review of previous material.

8 Priority Queues Ordered structure, but restricted –Only minimum value may be removed. What’s the advantage? –Used for scheduling, and generally to rank choices that are generated out of order. Your dorm again.

9 Priority Queues Relatively simple: –Not a Queue –Could enforce any other linear behavior based on the priority.

10 Priority Queues More efficient code for Huffman using Priority Queue.

11 Priority Queues Using a Vector: –Easy: getFirst, remove, size, isEmpty, clear. public void add(E value){ int position = indexOf(value); data.add(position,value);} protected int indexOf(E target){ E midValue; int low = 0; // lowest possible location int high = data.size(); // highest possible location int mid = (low + high)/2; // low <= mid <= high // mid == high iff low == high while (low < high) { Assert.condition(mid < high,"Middle elementexists."); midValue = data.get(mid); if (midValue.compareTo(target) < 0) { low = mid+1; } else { high = mid;} mid = (low+high)/2;} return low;} O(n) – why? O(?)

12 Priority Queues using VectorHeap Heap: binary tree whose root references the minimum value and whose subtrees are themselves heaps. –Every path from root to leaf in ascending order.

13 Priority Queues using Heaps Complete Tree (and therefore Heap) can be rep’d in a Vector. –1. The root of the tree is stored in location 0. If non-null, this location references the minimum value of the heap. –2. The left child of a value stored in location i is found at location 2i + 1. –3. The right child of a value stored in location i may be found at the location following the left child, location 2(i + 1) = (2i + 1) + 1. –4. The parent of a value found in location i can be found at location (i-1)/2 (int division).

14 Vector-based Heap for Queues.

15

16 One more Insert a value that ultimately produces a correctly structured heap: Correct: if it was complete, hopefully it’s still complete. -insert at first available. -percolate up if it is higher priority than its parent. -continue til in-order or root. O(?)

17 Percolate

18 Removal Still want a heap after removing the root. –Complications with the Vector here. –Algorithm: Remove root Replace root with rightmost leaf (that way, tree still complete. Push down (like percolate) if new root is too large. –Compare to smallest child. If it’s bigger, then swap AND call pushdown on subtree.

19 pushDown

20 Heap Remove VectorHeap has improved performance over PriorityVector, cost more only in code (not space).

21 HeapSort Insert the elements of the input vector into a VectorHeap: –O(nlog n) N times: remove min and place in newly freed space. –O(nlog n)

22 Skew Heaps Heap is not complete Vector storage for non-complete heap is inefficient, use BinaryTree. Need merge for add/remove –If the left heap has no left child, make the right heap the left child of the left heap –Otherwise, exchange the left and right children of the left heap. Then merge (the newly made) left subheap of the left heap with the right heap

23 Skew Heaps, given merge On average, as efficient as VectorHeap, with more storage but no requirement of contiguous memory.


Download ppt "CSCI 62 Data Structures Dr. Joshua Stough October 23, 2008."

Similar presentations


Ads by Google