Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Binary Heaps: - Order Property: Key (Parent) Key (Child) (Minimum or Maximum Property)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Binary Heaps

• A binary heap is a binary tree of comparable elements such


that:
1. The element contained at each node is greater than or equal to
Heaps the elements in that node’s children
2. The tree is a complete binary tree, i.e., every level, except
possibly the lowest level, is full and in the lowest level the
nodes are as far left as possible 91

77 46

69 3 11

Heaps Examples that are NOT heaps


A heap is a binary tree T that stores a collection of keys (or key-element pairs) at its
internal nodes and that satisfies two additional properties:
• Order Property: key(parent) ≤ key(child) [ minimum or maximum property]
• Structural Property: all levels are full, except the last one, which is left-filled
(complete binary tree)

5 6

15 9 7 20

16 25 14 12 11 8

1
1
Height of a Heap
Array representation of Heaps
1 45
• Because the structure of a
• A heap T storing n 2 3
keys has height complete binary tree is so 42 23
regular, a Heap can be
h = log(n + 1), represented in an array
4 5 6 7
27 35 22 6
which is O(log n) without using pointers
8 9 10
• Index the nodes as shown: 19 5 21

1 2 3 4 5 6 7 8 9 10
45 42 23 27 35 22 6 19 5 21
The left node of the node at index i is at index 2i, the right node is at index
(2i+1), and the parent is at index  i/2 , so tree traversal is very easy

Inserting in HEAP

• First adjoin ITEM at the end of H so that H is still a complete


tree but not necessarily a Heap.
• Then let ITEM rise to its “ appropriate place” in H so that H is
finally a Heap.

7 8

2
2
Inserting into a Heap Inserting into a Heap (contd.)

• To insert an element into a heap: 45 45


– a node is created in the next available position
– the element is inserted into that node 35 23 35 23
– then, if necessary, the element is “bubbled up” until the heap order
property is restored 45 27 21 22 6 27 42 22 6

35 23 19 5 42 19 5 21
Insert 42 into this heap:
27 21 22 6
Create a node for 42 in the first Swap 42 with its parent node
available place
19 5

9 10

Heap Insertion
Inserting into a Heap (contd.)
• The key to insert is 6
45
Note that, since the largest
element is always at the root, a
42
35 23 heap makes an ideal structure
with which to implement a
27 35
42 22 6 priority queue

19 5 21

Swap 42 with its parent node again


Now we have heap order again

11

3
3
Heap Insertion Upheap
• Swap parent-child keys out of order

Upheap Continues End of Upheap

• Upheap terminates when new key is greater than the key of its
parent or the top of the heap is reached
• (total #swaps) = Order of height (h), which is O(log n)

4
4
Insheap(Tree,N,Item)
• Build a heap from list
44 30 50 22 60 55 77 55 Heap H with N elements is stored in array TREE and item of
information is given. PTR gives location of ITEM as it rises
in TREE and PAR denotes location of Parent of ITEM.
1. [Add new node to H and initialize PTR]
Set N=N+1 and PTR=N
2. [Find location to insert ITEM]
Repeat steps 3 to 6 while PTR<1
3. Set Par:=PTR/2 {location of parent node]
4. It Item<= Tree[Par] then Set Tree[PTR]=Item and return
5. Set Tree[PTR]:= Tree[PAR] [ Moves node down]
6. Set PTR=PAR [updates PTR] [end loop]
7. [Assign Item as root of H]
Set Tree[1]=Item
17 18
Return

Removal From a Heap Downheap

• The removal of the top


key leaves a hole
• We need to fix the
heap
• First, replace the hole
with the last key in the
heap
• Then, begin
Downheap
• Downheap compares the parent with the smallest child. If
the child is smaller, it switches the two.

5
5
Downheap Continues Downheap Continues

Deleting root of Heap


End of Downheap This algo assigns root TREE[1] of heap to variable ITEM and then reheaps.
Variable LAST saves the value of original last node of H. pointers PTR,
LEFT,RIGHT gives locations of LAST and its left & right children as LAST
sinks in tree.
1. Set ITEM=TREE[1] [Remove root of H]
2. Set LAST=TREE[N] and N=N-1. [Remove last node of H]
3. Set PTR=1, LEFT=2,RIGHT=3 [initialize pointers]
4. Repeat steps 5 to 7 while RIGHT<=N
5. If LAST>=TREE[LEFT] and LAST >=TREE[RIGHT] then Set
TREE[PTR]=LAST and return [ case when last element is second biggest]
6. If TREE[RIGHT]<=TREE[LEFT] then: set TREE[PTR]=TREE[LEFT] and
PTR=LEFT
Else set TREE[PTR]= TREE[RIGHT] and PTR =RIGHT.
7. Set LEFT=2 *PTR and RIGHT=Left +1 [Loop ends]
• Downheap terminates when the key is greater than the keys of both 8. If LEFT =N and if LAST <TREE[LEFT] then set PTR = LEFT ( special case
when LAST doesnot have right child but has left child(which has to be last node
its children or the bottom of the heap is reached.
in H).
24
9. Set TREE[PTR] = LAST
10.Return

6
6
Heap Sort(A,N)
1. Build a heap H using previous procedure
Repeat for J= 1 to N-1 Call INSHEAP(A,J,A[j+1])
2. [Sort A repeatedly deleting root of H using Procedure 7.10]
Repeat while N >1
1. Call DELHEAP(A,N,ITEM)
2. Set A[N+1 ] =ITEM
Exit.
Complexity:
Phase A: number of comparisons to find appropriate place of
new ITEM cannot exceed depth ie log 2n. Therefore total
num of comparisons to insert n elements is g(n) <=n log 2n
Phase B: Re-heaping uses 4 comparisons to move node one step
down the tree. Therefore total num h(n) of comparisons to
delete n elements of A from H h(n) <= 4 .n. log 2n
that means over-all complexity is O(n. log 2n). 25

7
7

You might also like