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

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
Advertisements

CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 27 / 2009 Instructor: Michael Eckmann.
The Heap ADT In this section of notes you will learn about a new abstract data type, the heap, as well how heaps can be used.
CS 206 Introduction to Computer Science II 02 / 20 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
Chapter 10 Heaps Anshuman Razdan Div of Computing Studies
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 29 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 01 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
CST 230 Razdan et alhttp://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Tree Traversals Pre-order traversal –process root.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
Heapsort Based off slides by: David Matuszek
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Binary Heaps Text Read Weiss, § Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete.
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
Heaps and Heapsort Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Heaps & Priority Queues
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Priority Queues and Heaps
October 30th – Priority QUeues
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Priority Queues.
Priority Queues.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Priority Queues & Heaps
Heaps By JJ Shepherd.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

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

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

Michael Eckmann - Skidmore College - CS Spring 2009 Midterm Exam grading Mean = (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 scale, use the following formula: Minimum of 4.33 and (RawScore – Mean) / StdDev + EC/85 Example: if your raw score is 70 with 2 points EC Min(4.33, ( )/ /85) = Min(4.33, 3.94) = 3.47

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).

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.

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.

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?

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?

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.

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.

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]

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?

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.

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?

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

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.

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.