cs32s10dis8 - Discussion 1B Notes(Week 9 May 28 TA Brian...

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: Discussion 1B Notes (Week 9, May 28) TA: Brian Choi ([email protected]) 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: Every node holds a value greater than or equal to its children. This means that 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 you don’t need to track down the tree for the largest value; the root has it. (How long does it take to find the maximum value in a heap, in Big-O?) 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 general search operations. 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, we’ll 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 system, 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 of this operation? CS32: Introduction to Computer Science II Spring 2010 Copyright 2010 Brian Choi Week 9, Page 1/6 ! # $ % & ' ! # $ % & ' 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 add a new node at the end of this tree, to make it a right structure....
View Full Document

This note was uploaded on 02/09/2012 for the course CS 32 taught by Professor Davidsmallberg during the Spring '08 term at UCLA.

Page1 / 6

cs32s10dis8 - Discussion 1B Notes(Week 9 May 28 TA Brian...

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

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