Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Heaps and Priority Queues - Data Structures - Lecture Slides, Slides of Data Structures and Algorithms

In the subject of the Data Structures, the key concept and the main points, which are very important in the context of the data structures are listed below:Heaps and Priority Queues, Full Binary Tree, Non-Leaf Node, Same Level, Leaves, Next-To-Last Level, Left to Right, Leaves, Different Types, Heap

Typology: Slides

2012/2013

Uploaded on 04/23/2013

saratey
saratey 🇮🇳

4.3

(10)

87 documents

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 9
Heaps and Priority Queues
Lecture 18
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download Heaps and Priority Queues - Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Chapter 9

Heaps and Priority Queues

Lecture 18

Full Binary Tree

  • Every non-leaf node has two children
  • Leaves are on the same level

Full Binary Tree

Examples of Different Types of Binary Trees 4

What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties:

  • Its shape must be a complete binary tree.
  • For each node in the heap, the value stored in that node is greater than or equal to the value in each of its children.

Is this a Heap?

tree

Not unique!

We Can Number the Nodes Left to Right by Level This Way

tree

And use the Numbers as Array Indexes to Store the Trees 70 0 60 1 40 3 30 4 12 2 8 5 tree [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 8 tree.nodes

// HEAP SPECIFICATION // Assumes ItemType is either a built-in simple data // type or a class with overloaded relational operators. template< class ItemType > struct HeapType { void ReheapDown ( int root , int bottom ) ; void ReheapUp ( int root, int bottom ) ; ItemType* elements; //ARRAY to be allocated dynamically int numElements ; };

ReheapDown

bottom

ReheapDown (cont)

if ( leftChild <= bottom ) // ReheapDown continued

if ( leftChild == bottom )

maxChild = leftChld;

else

if (elements [ leftChild ] <= elements [ rightChild ] )

maxChild = rightChild;

else

maxChild = leftChild;

if ( elements [ root ] < elements [ maxChild ] )

Swap ( elements [ root ] , elements [ maxChild ] );

ReheapDown ( maxChild, bottom ) ;

O(logN)

ReheapUp

bottom

Real-life Priority Queue