Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 206 Introduction to Computer Science II 03 / 20 / 2009 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 206 Introduction to Computer Science II 03 / 20 / 2009 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 206 Introduction to Computer Science II 03 / 20 / 2009 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 206 - Spring 2009 Today’s Topics Questions? Comments? Midterm grading Priority Queue (finish implementation)‏ Deque

3 Michael Eckmann - Skidmore College - CS 376 - Spring 2009 Midterm Exam grading Mean = 65.79 (after removing highest and lowest grade)‏ StdDev = 9.5(after removing highest and lowest grade)‏ Your Raw Score is out of 85 To convert to a curved 0-4.0 scale, use the following formula: Minimum of 4.33 and 3.0 + (RawScore – Mean) / StdDev + EC/85 Example: if your raw score is 70 with 2 points EC Min(4.33, 3.0 + (70-65.79)/9.5 + 2/85) = Min(4.33, 3.94) = 3.47

4 Priority Queues Priority queues have the following characteristics –Each item placed into a priority queue has a priority value associated with it –When a remove is requested from a priority queue, we remove the highest priority item –We should also have a way to determine if the priority queue is empty –We can also have a peek to see what item has the highest priority without removing it It is best to limit priority values to be integers (reason to be seen shortly).

5 Priority Queues It is also a wise thing to maintain the following values for a priority queue –current size (how many items are in the queue)‏ –the priority of the highest priority item in the queue to make remove more efficient however it causes (some) additional work –after the remove happens we need to possibly change this value –after an add we just need to check if the one we added is higher than the highest, if so, change it.

6 Priority Queues Implementation of a priority queue could be an array of queues. The index of the array is the priority value (see how we want our priority values to be integers?)‏ The range of priority values (all integers) determines how many queues we're storing --- that is, how many elements of the array there will be. Let's implement a priority queue in this way now.

7 Priority Queues If we don't want to limit the priority values to integers or the range of priorities is large --- an array of queues is impractical. What alternate implementation(s) could handle a large range of floating point priorities?

8 Deques Deque (or Dequeue) stands for double-ended queue. It is pronounced “deck”. Allows adding to and removing from both the front and rear. –Since this allows adding and removing from both ends what kind of linked list would be best to implement this data structure?

9 Heaps Before we get into heaps, we need to define this. Total Order Semantics: –==, !=, >=,, < all defined –x == y iff x and y are identical –for all x and y exactly one of these three is true x y, or x == y –for any x and y x x >= y is same as (x>y) || (x==y)‏ x <= y is same as (x<y) || (x==y)‏ x != y is same as !(y == x)‏ –x < y and y < z implies x < z Binary Search Trees required this total order semantics among its elements.

10 Heaps A heap is a binary tree in which the elements can be compared with a total order semantics AND the arrangement of the values in the heap follow the following rules. –1) the element in each node is >= the elements of that node's children (maxHeap)‏ –2) the tree is complete - every level except the deepest must contain as many nodes as possible and the deepest level all the nodes are as far left as possible if node is <= children, then it is called a minHeap Let's look at some examples of heaps and non-heaps on the board.

11 Heaps Recall the array implementation of a binary tree. The array implementation works well for complete binary trees. Node [i] has it's children (if they exist) at left child: [2i+1] right child: [2i+2] If Node [i] is not the root, then Node [i]'s parent is at [(i-1)/2]

12 Heaps To insert a new node into a heap –1) after the node is inserted the structure still must be a heap that is it is a complete binary tree with the element in each node >= the elements of that node's children –Any ideas on how to do this?

13 Heaps To insert a new node into a heap –Place the node in the next available slot (deepest level and as far to the left as possible)‏ –Then, check that node against it's parent and swap if appropriate. Continue to do this until we don't need to swap. We swap when a node's parent is < the node. –When this process is done, we still have a heap. The process of rising a node into it's proper place is (upward) heapification. To heapify is to start with a complete binary tree that is not a heap and make it a heap.

14 Heaps To delete the root from the heap –1) after the root is removed from the heap, the structure still must be a heap that is it must be a complete binary tree with the element in each node >= the elements of that node's children –Any ideas on how to do this?

15 Heaps To delete the root from the heap –if the tree consists of only one node, the result is an empty tree –if the tree consists of more than one node, then move the last element (the one furthest to the right in the deepest level) in the tree to the root now we have a complete binary tree with one less node but it may not be a heap, so we need to heapify, but this time we need to do a (downward) heapification. call the element that moved to the root, the out-of- place node while (out-of-place node is < one of its children)‏ –swap the out-of-place node with its larger child

16 Heaps Heaps can be used to implement Priority Queues –Main operations of a priority queue remove (highest priority item)‏ add For a Heap implementation of a priority queue, we would remove from the root (and then make sure the heap remains a heap.)‏ For add, we would place at last slot and heapify.

17 Heaps To heapify an arbitrary binary tree (or an array)‏ –We can heapify parts of the tree and eventually end up with the whole binary tree heapified. –Example in the handout.


Download ppt "CS 206 Introduction to Computer Science II 03 / 20 / 2009 Instructor: Michael Eckmann."

Similar presentations


Ads by Google