Notes 14 - Week 14: Heaps Some of the material covered is...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Week 14: Heaps Some of the material covered is contained in Chapter 25 (about heaps page 661 ff) and the rest is in Chapter 28 about heap implementations. Definitions: A tree is complete is all children exist except on the lowest level where all children are to the left as far as possible. A binary tree is a heap if it is complete A maxheap: if each parent node is greater or equal to the child nodes A minheap: if each parent node is less or equal to the child nodes Task 1: See examples of a maxheap and minheap on page 662. Review the ADT and especially notice the getMax and removeMax methods. Do question #16 and 17 on page 663 and check your answers in the appendix. From now on we will use max heaps in this course in the examples. Heap implementations can use the heapform array: children of a[i] are at a[2*i] and a[2*i+1] Note: the parent of any node is at location i / 2 the root can be detected using the condition: i / 2 == 0 or, by placing a sentinel in location 0 Task 2: After reviewing figure 28-1 on page 739 for an example of a heap stored as a heapform array draw the heap represented by the following heapform array and post and check your answers in the discussion group. 0 1 2 3 4 5 6 F D E B A Two ways of Adding an entry 1. Add Rules: store the value at the correct node at the bottom (next place in complete tree) swap up to the correct place (upheap) Example : Put the stream AFBCGD into a heap using add: A is already a heap. Next add F as the left child of a to make the tree complete
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/23/2012 for the course CS 111C taught by Professor Metzler during the Spring '11 term at City College of San Francisco.

Page1 / 5

Notes 14 - Week 14: Heaps Some of the material covered is...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online