Heaps and Priority Queues: A Comprehensive Guide, Study notes of Computer Science

An in-depth exploration of heaps and priority queues, essential data structures in computer science. Topics covered include binary trees, heap properties, heap operations, heap applications, and heap implementation. Learn about the key differences between heaps and binary search trees, and discover how heaps are used in sorting algorithms and priority queues.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-vth
koofers-user-vth 🇺🇸

9 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 132:
Object-Oriented Programming II
Heaps & Priority Queues
Department of Computer Science
University of Maryland, College Park
Overview
Binary trees
Full, Perfect, Complete
Heaps
Insert
getSmallest
Heap applications
Heapsort
Priority queues
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Heaps and Priority Queues: A Comprehensive Guide and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Heaps & Priority Queues

Department of Computer Science

University of Maryland, College Park

Overview

Binary trees

Full, Perfect, Complete

Heaps

Insert getSmallest

Heap applications

Heapsort Priority queues

Full Binary Tree

Binary tree where all nodes have 0 or 2 children

Not Allowed

Perfect Binary Tree

A full binary tree where

All leaves at level h for tree of height h

h = 2 h = 3 h = 4

h = 1

Heaps

Two key properties

Complete binary tree Value at node Smaller than or equal to values in subtrees

Example heap

X ≤ Y X ≤ Z

Y

X

Z

Heap & Non-heap Examples

Heaps Non-heaps

Heap Properties

Heaps are balanced trees

Height = log 2 (n) = O(log(n))

Can find smallest element easily

Always at top of heap!

Can organize heap to find maximum value

Value at node larger than values in subtrees Heap can track either min or max, but not both

Heap

Key operations

Insert ( X ) getSmallest ( )

Key applications

Heapsort Priority queue

Heap Insert Example

Insert ( 8 )

  1. Insert toend of tree swap if parent key larger2) Compare to parent, complete3) Insert

Heap Operation – getSmallest()

Algorithm

  1. Get smallest node at root
  2. Replace root with X at end of tree
  3. While ( X > child ) Swap X with smallest child // X drops down tree
  4. Return smallest node

Complexity

swaps proportional to height of tree

O( log(n) )

Heap GetSmallest Example

getSmallest ()

with end of tree1) Replace root children, if larger swap2) Compare node to with smallest child

  1. Repeat swapif needed

Heap GetSmallest Example

getSmallest ()

with end of tree1) Replace root children, if larger swap2) Compare node to with smallest child

  1. Repeat swapif needed

Heap Implementation

Calculating node locations

Array index i starts at 0 Parent(i) = ⎣ ( i – 1 ) / 2 ⎦ LeftChild(i) = 2 × i + RightChild(i) = 2 × i +

Heap Implementation

Example

Parent(1) = ⎣ ( 1 – 1 ) / 2 ⎦ = ⎣ 0 / 2 ⎦ = 0 Parent(2) = ⎣ ( 2 – 1 ) / 2 ⎦ = ⎣ 1 / 2 ⎦ = 0 Parent(3) = ⎣ ( 3 – 1 ) / 2 ⎦ = ⎣ 2 / 2 ⎦ = 1 Parent(4) = ⎣ ( 4 – 1 ) / 2 ⎦ = ⎣ 3 / 2 ⎦ = 1 Parent(5) = ⎣ ( 5 – 1 ) / 2 ⎦ = ⎣ 4 / 2 ⎦ = 2

Heap Implementation

Example

LeftChild(0) = 2 × 0 +1 = 1 LeftChild(1) = 2 × 1 +1 = 3 LeftChild(2) = 2 × 2 +1 = 5

Heap Implementation

Example

RightChild(0) = 2 × 0 +2 = 2 RightChild(1) = 2 × 1 +2 = 4

Heapsort – Insert Values

Heapsort – Remove Values

Input

Input

  • Heapsort – Insert in to Array
  • 11, 5, 13, 6, - Index = - Insert
    • Heapsort – Insert in to Array
  • 11, 5, 13, 6, Input - Index = - Insert - Swap
    • Heapsort – Remove from Array
      • 11, 5, 13, 6,
        • Index =
  • Remove root
  • Replace
  • Swap w/ child
    • Heapsort – Remove from Array
      • 11, 5, 13, 6, Input
        • Index =
  • Remove root
  • Replace
  • Swap w/ child

Heap Application – Priority Queue

Queue

Linear data structure First-in First-out (FIFO) Implement as array / linked list Enqueue Dequeue

Heap Application – Priority Queue

Priority queue

Elements are assigned priority value Higher priority elements are taken out first Equal priority elements are taken out in FIFO order Implement as heap Enqueue ⇒ insert( ) Dequeue ⇒ getSmallest( ) (^) Dequeue

Enqueue