Heaps

Ace your homework & exams now with Quizwiz!

Heap DeleteMin

Minimum element is always at the root Heap decreases by one in size Heap decreases by one in size Move last element into hole at root Percolate down while heap-order property not satisfied

What are the properties of a binary heap?

A binary heap is a binary tree with two properties Structure property (complete binary tree) Heap-order property order property

Complete Binary Tree

Binary tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right

How do you find left child, right child and parent?

Given element at position i in the array Left child(i) =at position 2i Right child(i) = at position 2i + 1 Parent(i) = at position floor(i/2)

What is the heap-order property?

Heap-order property (for a "MinHeap") For every node X, (key(parent(X)) ≤ key(X)) Except root node, which has no parent

Notes on Heap

It is easy to show that a complete binary tree of height h has between 2^h and 2^(h+1) − 1 nodes. -height of a complete binary tree is floor(logN) , which is clearly O(logN). -Insert and deleteMin must maintain heap -order property -A frequent implementation error in heaps occurs when there are an even number of elements in the heap, and the one node that has only one child is encountered. checked with the following: if( child != currentSize && array[ child +1]< array[ child ] )

Heapify

Rearrange a heap to maintain the heap property, that is, the key of the root node is more extreme (greater or less) than or equal to the keys of its children. If the root node's key is not more extreme, swap it with the most extreme child key, then recursively heapify that child's subtree. The child subtrees must be heaps to start.

Do you need to know the other functions for heaps?

Such as decreaseKey, increaseKey, remove?

Insert

To insert an element X into the heap, we create a hole in the next available location, since otherwise, the tree will not be complete. If X can be placed in the hole without violating heap order, then we do so and are done. Otherwise, we slide the element that is in the hole's parent node into the hole, thus bubbling the hole up toward the root. We continue this process until X can be placed in the hole.

Percolate Down Process

To percolate down, we check a value against its children. If it is the smallest of the three, then we're done. Otherwise, we swap it with the child that has the minimum value, and continue percolating down.

Percolate Up Process

To percolate up, we look at the value's parent. If the parent is smaller than the value, then we can quit -- we have a valid heap. Otherwise, we swap the value with its parent, and continue percolating up.

What does a heap data structure consist of?

a heap data structure consists of an array (of comparable objects) and an integer representing the current heap size


Related study sets

International Business Midterm Q's

View Set

Subject and Predicate, Subject and Predicate

View Set

5.1 Chemistry - Instrumentation Part 1 (1-40)

View Set

Marketing Strategy ch1-5 review exam

View Set