CC2203-Les13Heap-for-viewing

CC2203-Les13Heap-for-viewing - 13-1/68Lesson 13: Heap and...

Info iconThis preview shows pages 1–23. 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

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight 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: 13-1/68Lesson 13: Heap and Heap SortTwo kinds of heap:• Min Heap.• Max Heap.13-2/68Min Tree DefinitionEach tree node has a value.Value in any node is the minimum value in the subtree for which that node is the root.Equivalently, no descendent has a smaller value.13-3/68Min Tree Example249348799Root has minimum element.13-4/68Max Tree Example949842731Root has maximum element.13-5/68Min Heap Definition• complete binary tree• min tree13-6/68Min Heap With 9 NodesComplete binary tree with 9 nodes.13-7/68Min Heap With 9 NodesComplete binary tree with 9 nodes that is also a min tree.24679386313-8/68Max Heap With 9 NodesComplete binary tree with 9 nodes that is also a max tree.98672651713-9/68Heap HeightSince a heap is a complete binary tree, the height of an n node heap isBy default, a heap is a max heap; a min heap must be explicitly specified. ( )nlogO1nlog22⇒+13-10/68987672651123456789A Heap Is Efficiently Represented As An Array98672651713-11/68Moving Up And Down A Heap9867265171234567813-12/68Putting An Element Into A Max HeapComplete binary tree with 10 nodes.986726517713-13/68Putting An Element Into A Max HeapNew element is 5.9867265177513-14/68Putting An Element Into A Max HeapNew element is 20.9867265177713-15/68Putting An Element Into A Max HeapNew element is 20.9867265177713-16/68Putting An Element Into A Max HeapNew element is 20.9867265177713-17/68Putting An Element Into A Max HeapNew element is 20.986726517772013-18/68Putting An Element Into A Max HeapComplete binary tree with 11 nodes.986726517772013-19/68Putting An Element Into A Max HeapNew element is 15.986726517772013-20/68Putting An Element Into A Max HeapNew element is 15.9867265177720813-21/68Putting An Element Into A Max HeapNew element is 15.867265177720891513-22/68MaxHeap putpublic void put(Comparable theElement) {// increase array size if necessaryif (size == heap.length)heap = (Comparable ) ChangeArrayLength.changeLength1D(heap, 2 * heap.length);// find place for theElement// currentNode starts at new leaf and moves up treeint currentNode = size++;while (currentNode > 0 && heap[(currentNode-1) / 2].compareTo(theElement) < 0) {// cannot put theElement in heap[currentNode]...
View Full Document

Page1 / 68

CC2203-Les13Heap-for-viewing - 13-1/68Lesson 13: Heap and...

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

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