Download presentation

Presentation is loading. Please wait.

Published byArthur Boden Modified over 2 years ago

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

2
Michael Eckmann - Skidmore College - CS 206 - Spring 2009 Today’s Topics Questions? Comments? Heaps

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

4
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]

5
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?

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

7
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?

8
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

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

10
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?

11
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

Similar presentations

OK

CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.

CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on surface water tension Human brain anatomy and physiology ppt on cells Ppt on phase sequence checker for three phase supply Ppt on squid proxy server Ppt on man made disaster management Ppt on reserve bank of india A ppt on personality development Ppt on indian culture and tradition Ppt on recycling of waste material Convert pdf to ppt online adobe