cs32w09dis8

# cs32w09dis8 - Discussion 3A Notes (Week 9, March 6) TA:...

This preview shows pages 1–3. Sign up to view the full content.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Discussion 3A Notes (Week 9, March 6) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Heaps A heap is a tree with special properties. In this class we will only consider a binary heap, whose underlying structure is a binary tree. It is a complete binary tree with the following property:- The root (of every subtree in the heap) contains a value greater than or equal to its children. This means, the root of a heap will have the greatest value in the heap, and that every subtree is a heap. Such heap is called a maxheap . There is also another type called minheap , which you can define by replacing greater than in the sentence above with less than. From now on, let us focus on maxheaps, as a maxheap and a minheap do not differ in anything else. As one can see, values in a heap are not necessarily sorted. A heap is more useful in situations where what matters is the maximum value, because the root has it -- you dont need to track down the tree for the largest value. (How would you say the same thing in Big-O sense?) This is a big advantage if there are a lot of values stored in the heap. At the same time, the same property of a heap makes it unsuitable for searching things. So here is the set of operations we need to define for heaps: insertion - insert a new node into the heap findMax - find the maximum value deleteMax - delete the node with the maximum value Before discussing the algorithms, let us talk about how a heap is implemented. Unlike what we did for binary trees, well use an array to implement our heap. Array Implementation of a heap The figure on left shows a valid heap. It is a complete binary tree, and every parent is greater than its children. Because it is a complete tree, we can number each node from top to bottom, left to right, sequentially. The root is numbered 0, and its children are numbered 1 and 2, and so forth. Because we can come up with such a numbering, we can simple create an array, say heap , where each node i is stored in heap[i] , as follows: findMax It probably is a waste of time to talk about how to implement this, no? Question: What is the running time for this operation? CS32: Introduction to Computer Science II Winter 2009 Week 9, Page 1/6 ! # \$ % &amp; ! # \$ % &amp; 9 7 6 1 5 2 insertNode( Node newNode ) Phase 1 How do we add a new node? First thing to worry about is that our tree must be complete. In the graph we saw in the previous page, after the insertion of the new node, there must be another node in the right branch of 6. So we will greedily (this is a technical term, believe it or not) add a new node at the end of this tree, to make it a right structure....
View Full Document

## This note was uploaded on 11/07/2009 for the course CS 32 taught by Professor Davidsmallberg during the Winter '08 term at UCLA.

### Page1 / 6

cs32w09dis8 - Discussion 3A Notes (Week 9, March 6) TA:...

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

View Full Document
Ask a homework question - tutors are online