Download presentation
Presentation is loading. Please wait.
1
CS 206 Introduction to Computer Science II 10 / 29 / 2008 Instructor: Michael Eckmann
2
Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Today’s Topics Questions? Priority Queues –With floating point priorities Deque Heaps
3
Michael Eckmann - Skidmore College - CS 376 - Fall 2008 Midterm Exam grading Mean = 70.39 StdDev = 12.46 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.17 + (RawScore – Mean) / StdDev + 4*EC/85 Example: if your raw score is 78 with 2 points EC Min(4.33, 3.17 + (78-70.39)/12.46 + 4*2/85) = Min(4.33, 3.87) = 3.87
4
Michael Eckmann - Skidmore College - CS 376 - Fall 2008 Midterm Exam grading Notice that a raw score of exactly the mean will get the mean grade of 3.17 (ignoring EC) A raw score below the mean (of 70.39) will get a grade below 3.17 (ignoring EC) The reason I use the Min(4.33,...) is because I define 4.33 to be the maximum score per assignment/exam.
5
Michael Eckmann - Skidmore College - CS 376 - Fall 2008 Grading At the end of the semester I compute your letter grade from your final average on a 0-4.0 scale like so: >4.17 = A+ >3.83 = A >3.5 = A- >3.17 = B+ >2.83 = B >2.5 = B- >2.17 = C+ >1.83 = C >1.5 = C- >1.17 = D+ >0.83 = D >0.5 = D-
6
Michael Eckmann - Skidmore College - CS 376 - Fall 2008 Midterm Exam Let's look at the last problem on the exam I would like to meet with anyone who would like to go over material from that exam.
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
Priority Queues What alternate implementation(s) could handle a large range of floating point priorities? –Store a linked list of nodes where each node contains a priority value and the item stored in the priority queue –This will require more work to insert into the priority queue, but will remove the need to keep track of largest priority, etc. What are your thoughts on implementing a priority queue in this way?
9
Priority Queues What alternate implementation(s) could handle a large range of floating point priorities? –Store a linked list of nodes where each node contains a priority value and the item stored in the priority queue –This will require more work to insert into the priority queue, but will remove the need to keep track of largest priority, etc. Note: This is not the preferred way to implement a priority queue.. a heap is better. We'll analyze the running times of the operations for this implementation vs. the heap.
10
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?
11
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.
12
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.
13
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]
14
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?
15
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.
16
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?
17
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
18
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.
19
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.