Docsity

Priority Queues and Heap Data Structures, Study notes of Computer Science

Toggle sidebar
bg1
1
Priority Queues
Sections 6.1 to 6.5
Vector Representation of Complete
Binary Tree
v[k].parent == v[(k-1)/2]
v[k].lchild == v[2*k+1]
v[k].rchild == v[2*k+2]
R
l r
ll lr rrrl
root
Vector Representation of Complete
Binary Tree
v[k].parent == v[(k-1)/2]
v[k].lchild == v[2*k+1]
v[k].rchild == v[2*k+2]
R
6543210
Vector Representation of Complete
Binary Tree
v[k].parent == v[(k-1)/2]
v[k].lchild == v[2*k+1]
v[k].rchild == v[2*k+2]
lR
6543210
Vector Representation of Complete
Binary Tree
v[k].parent == v[(k-1)/2]
v[k].lchild == v[2*k+1]
v[k].rchild == v[2*k+2]
rlR
6543210
Vector Representation of Complete
Binary Tree
v[k].parent == v[(k-1)/2]
v[k].lchild == v[2*k+1]
v[k].rchild == v[2*k+2]
llrlR
6543210
pf3
pf4
pf5

Partial preview of the text

Download Priority Queues and Heap Data Structures and more Study notes Computer Science in PDF only on Docsity!

Priority Queues

Sections 6.1 to 6.

Vector Representation of Complete

Binary Tree

 v[k].parent == v[(k-1)/2]

 v[k].lchild == v[2*k+1]

 v[k].rchild == v[2*k+2]

R l r ll lr rl rr root

Vector Representation of Complete

Binary Tree

 v[k].parent == v[(k-1)/2]

 v[k].lchild == v[2*k+1]

 v[k].rchild == v[2*k+2]

R

Vector Representation of Complete

Binary Tree

 v[k].parent == v[(k-1)/2]

 v[k].lchild == v[2*k+1]

 v[k].rchild == v[2*k+2]

R l

Vector Representation of Complete

Binary Tree

 v[k].parent == v[(k-1)/2]

 v[k].lchild == v[2*k+1]

 v[k].rchild == v[2*k+2]

R l r

Vector Representation of Complete

Binary Tree

 v[k].parent == v[(k-1)/2]

 v[k].lchild == v[2*k+1]

 v[k].rchild == v[2*k+2]

R l r ll

Vector Representation of Complete

Binary Tree

 v[k].parent == v[(k-1)/2]

 v[k].lchild == v[2*k+1]

 v[k].rchild == v[2*k+2]

R l r ll lr

Vector Representation of Complete

Binary Tree

 v[k].parent == v[(k-1)/2]

 v[k].lchild == v[2*k+1]

 v[k].rchild == v[2*k+2]

R l r ll lr rl

Vector Representation of Complete

Binary Tree

 v[k].parent == v[(k-1)/2]

 v[k].lchild == v[2*k+1]

 v[k].rchild == v[2*k+2]

R l r ll lr rl rr

Priority Queue Note: STL provides deleteMax. But book provides deleteMin. We’ll follow the deleteMax strategy. deleteMax

Priority Queue Usage

int main() { priority_queue Q; Q.push(1); Q.push(4); Q.push(2); Q.push(8); Q.push(5); Q.push(7); assert(Q.size() == 6); assert(Q.top() == 8); Q.pop(); assert(Q.top() == 7); Q.pop(); assert(Q.top() == 5); Q.pop(); assert(Q.top() == 4); Q.pop(); assert(Q.top() == 2); Q.pop(); assert(Q.top() == 1); Q.pop(); assert(Q.empty()); } STL Priority Queues

 Element type with priority

tCompare & cmp

 Associative queue operations

Void push(t)void pop()T& top()void clear()bool empty()

The Push Heap Algorithm

 Add new data at next leaf

R l r ll lr rl rr

The Push Heap Algorithm

 Add new data at next leaf

R l r ll lr rl rr

The Push Heap Algorithm

 Repeat

Locate parent of v[k] = v[(k – 1)/2]
 if tree not partially ordered

swap

 else

stop 7 6 5 4 3 8 R l r ll lr rl rr

The Push Heap Algorithm

 Repeat

Locate parent of v[k] = v[(k – 1)/2]
 if tree not partially ordered

swap

 else

stop 7 6 5 4 3 8 R l r ll lr rl rr

The Push Heap Algorithm

 Repeat

Locate parent of v[k] = v[(k – 1)/2]
 if tree not partially ordered

swap

 else

stop 7 6 8 4 3 5 R l r ll lr rl rr

The Push Heap Algorithm

 Repeat

Locate parent of v[k] = v[(k – 1)/2]
 if tree not partially ordered

swap

 else

stop 7 6 8 4 3 5 R l r ll lr rl rr

The Push Heap Algorithm

 Repeat

Locate parent of v[k] = v[(k – 1)/2]
 if tree not partially ordered

swap

 else

stop 7 6 8 4 3 5 R l r ll lr rl rr

The Push Heap Algorithm

 Repeat

Locate parent of v[k] = v[(k – 1)/2]
 if tree not partially ordered

swap

 else

stop 8 6 7 4 3 5 R l r ll lr rl rr

The Pop-Heap Algorithm

 Copy last leaf to root

 Remove last leaf

 Repeat

find the larger child
 if not partially ordered tree

swap

 else

stop

 Until tree is partially ordered

The Pop Heap Algorithm

 Copy last leaf to root

R l r ll lr rl rr

The Pop Heap Algorithm

 Copy last leaf to root

R l r ll lr rl rr

The Pop Heap Algorithm

 Remove last leaf

R l r ll lr rl rr