Presentation on theme: "CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann."— Presentation transcript:
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann
Michael Eckmann - Skidmore College - CS Spring 2009 Today’s Topics Questions? Comments? 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) Algorithm: –We find the last node that is NOT a leaf. Do a downward reheapification from that node. –Then the next to last node that is NOT a leaf and do a downward reheapification from that node. –Etc. –Until get to the root and do a downward reheapification from the root. –Example in the handout. Question: If the tree was stored in an array once we found the last node that is NOT a leaf, how do we find the “next to last” node that is NOT a leaf, and so on?
Heaps Let's write a class to represent a Heap. Let's say the data we want to store are ints. The Heap will be stored as –an array of ints –an int to represent the next available slot We'll write functionality to –Remove the root –Add to the heap –The above two will need functionality to do Downward reheapification Upward reheapification