Docsity

Data Structures and Algorithms: Building a Binary Heap in O(n) Time, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Data Structures and Algorithms: Building a Binary Heap in O(n) Time and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms

( CS210A )

Lecture 29:

  • Building a Binary heap on ๐’ elements in O (๐’) time.
  • Applications of Binary heap : sorting
  • Binary trees: beyond searching and sorting

Recap from the last lecture

Building a Binary heap

Problem: Given ๐’ elements {๐‘ฅ 0 , โ€ฆ, ๐‘ฅ๐‘›โˆ’1}, build a binary heap H storing them.

Trivial solution:

( Building the Binary heap incrementally )

CreateHeap ( H );

For( ๐’Š = 0 to ๐’ โˆ’ ๐Ÿ )

Insert( ๐‘ฅ๐‘–, H);

What is the time complexity of this algorithm?

Building a Binary heap incrementally

The time complexity for inserting a leaf node =?

leaf nodes = ๐’/๐Ÿ ,

๏ƒจ Theorem: Time complexity of building a binary heap incrementally is O (๐’ log ๐’). 5

O (log ๐’ )

What useful inference can you draw from this Theorem?

Top-down approach

Building a Binary heap incrementally

Top-down approach

Think of alternate approach for building a binary heap

heap property:?

We just need to ensure this property at each node. 8

โ€œEvery node stores value smaller than its children โ€

In any complete binary tree , how many nodes satisfy heap property?

all leaf nodes ๏Š

Bottom-up approach

Does it suggest a new approach to build binary heap?

A new approach to build binary heap

1. Just copy the given ๐’ elements {๐‘ฅ 0 , โ€ฆ, ๐‘ฅ๐‘›โˆ’1} into an array H.

2. The heap property holds for all the leaf nodes in the corresponding

complete binary tree.

3. Leaving all the leaf nodes,

process the elements in the decreasing order of their numbering

and set the heap property for each of them.

A new approach to build binary heap

H^98 14 33^37 11 52 32 85^17 25 88 41 21^29 76 47 75 9 57

The first node to be processed.

A new approach to build binary heap

H^98 14 33^37 11 52 32 85^9 23 88 41 21^29 76 47 75 17 57

The third node to be processed.

A new approach to build binary heap

H^98 14 33^37 11 52 32 47^9 23 88 41 21^29 76 85 75 17 57

A new approach to build binary heap

v

H^98 54 21^9 11 33 29 47 17 23 88 41 52^32 76 85 75 37 57^25

A new approach to build binary heap

v

H^98 54 21^9 11 33 29 47 17 23 88 41 52^32 76 85 75 37 57^25

Heapify( ๐’Š ,H)

Heapify( ๐’Š ,H)

{ ๐’ ๏ƒŸ size ( H ) -1 ;

While (? and? )

For node ๐’Š, compare its value with those of its children

If it is smaller than any of its children ๏ƒจ Swap it with smallest child and move down โ€ฆ

Else stop!

Heapify( ๐’Š ,H)

Heapify( ๐’Š ,H)

{ ๐’ ๏ƒŸ size ( H ) -1 ;

While (? and? )

If(? ) ๐’Ž๐’Š๐’ ๏ƒŸ ๐Ÿ๐’Š + ๐Ÿ ;

If(? ) ๐’Ž๐’Š๐’ ๏ƒŸ ๐Ÿ๐’Š + ๐Ÿ ;

If( ๐’Ž๐’Š๐’ โ‰  ๐’Š )

{ H (๐’Š) โ†” H (๐’Ž๐’Š๐’);

else

Flag ๏ƒŸ false;

๐’Š โ‰ค (๐’โˆ’1)/2 Flag = true

Flag ๏ƒŸ true;

๐Ÿ๐’Š + ๐Ÿ โ‰ค ๐’ and H[ ๐’Ž๐’Š๐’ ]>H[ ๐Ÿ๐’Š + ๐Ÿ ]

H[ ๐’Š ]>H[ ๐Ÿ๐’Š + ๐Ÿ ]