You are on page 1of 13

Heap Trees

What is a Heap?
A heap is a binary tree whose left and right sub trees have values less than their parents. the tree is complete or nearly complete. The key value of each node is greater than or equal to the key value in each of its descendents. The structure is sometimes called min/max heap. (key values less / key values greater resp.) It is implemented using arrays as the relation between parents and children is fixed and is calculated as:

1. For a node located at i index its children are found at: Left child : 2i+1 Right child : 2i+2
2. The parent of a node located at i index is located at (i-1)/2. 3. Given the index for a left child j, its right sibling ,if any is found at j+1.Conversly,given the index for a right child k,left sibling which exist is found at k-1. 4. Given the size n,of a complete heap, the location of the first leaf is (n/2). 5.Given the location of the first leaf element ,the location of the last non-leaf element is one less.

Example:
a heap
25

53

44

15

21

13

18

12

Heap Operations
Reheap Up Reheap Down Insert a node Delete a node
There are two ways to build a heap either we start with empty array and insert elements one at a time or given an array of elements that are not heap ,we can rearrange the elements in array to form a heap.

ReheapUp It uses recursion to move the new node up of the tree. It begins by determining the address of parent using the relationship. If the key of new data is greater than the key of parent ,it exchanges the nodes and recursively calls itself. ReheapUp (heap<array>,new node<index>) pre:-heap is an array with invalid data new node is index location to new data in heap post :- new node is inserted in heap. 1.if (new node not zero) 1.parent=(new node-1)/2 2.if (heap[new node].key>heap[parent].key) 1.swap(new node,parent) 2.ReheapUp(heap, parent) 2.return

ReheapDown ReheapUp (heap<array>,root<index>,last<index>) Heap:-array of data, root:-root of heap/sub heap, last :index of last element in heap.
1.If ( root * 2 + 1 < = last ) 1.leftkey =heap[root * 2 +1].data. key 2.if(root * 2 + 2 < = last ) 1.right key=heap[root *2 + 2].data. key 3. else 1.rightkey=low key 4.if (left key > right key) 1.largechildkey = left key 2.largechildindex = =root *2 + 1 5.else 1.largechildkey = right key 2.largrchildindex = =root *2 + 2

6. if (heap[root].data. key < heap [largechildindex].data. key) 1.swap (root, largechildindex) 2.ReheapDown(heap, Largechilsindex,last) 2.Return

Build Heap
Build heap(heap <array>,size<int>) pre:- heap is an array with elements in non-heap form size:-no. of elements in array post:- array is now a heap 1.Walker =1 2.loop(walker<=size) 1.ReheapUp(heap , walker) 2.walker=walker+1 3.return

Insert Heap
Insertheap(heap <array >,last<index>,data<data type>) Pre:-heap is a valid heap structure last is index to last node into heap Post :- data have been inserted into heap 1.If(heap full) return(false) 2.last=last+1 3.heap[last]=data 4.reheapup(heap, last) 5.return true

Delete Heap
delete heap(heap <array >,last<index>,data out<data type>) Pre:-heap is a valid heap structure last is index to last node into heap data out refers to parameter for output Post :- data have been deleted from heap root data placed in data out 1.If (heap empty) 1.return (false) 2.dataout=heap[0] 3.heap[0]=heap[last] 4.last=last-1 5.reheapdown(heap,0,last) 6.return (true)

Heap sort
The selection sort algorithm scans the unsorted elements and selects the smallest element. Finding the smallest element among the n elements requires n-1 comparisions.This part of the selection sort makes it very slow. The heap sort is improved version of selection sort in which the largest element (root) is selected and exchanged with the last element in the unsorted list. As heap is a tree structure we dont need to scan the entire list to locate the largest key .Rather, we reheap,which moves the largest element to the root by following tree branches. This ability to follow branches makes the heap sort much faster than straight selection sort.

Heap Sort
Heapsort<heap<array>,last<index>) Pre:- heap is valid heap structure last is index to last element in array Post:- array has been sorted

//Create heap
1.walker=1 2.loop(walker<=last) 1.reheapup (heap, walker) 2.walker=walker+1

//Heap created. Now sort it


3.sorted =last 4.loop(sorted > 0) 1.exchange (heap,0,sorted) 2.sorted=sorted-1 3.reheapdown(heap,0,sorted) 5.return

You might also like