You are on page 1of 65

Chapter 9 Priority Queues

Single- and Double-Ended


Priority Queues
Leftist Trees
Binomial Heaps
Fibonacci Heaps
Pairing Heaps
Min-Max Heaps
Interval Heaps

C-C Tsai

P.1

Single- and Double-Ended


Priority Queues

A priority queue is a collection of elements such


that each element has an associated priority.
For single-ended priority queues (SEPQ), the
operations supported by a min priority queue
are:

C-C Tsai

SP1: Return an element with minimum priority.


minElement(); minKey();
SP2: Insert an element with an arbitrary priority.
insert();
SP3: Delete an element with minimum priority.
deleteMin();

Other ADTs: size(); isEmpty();


P.2

Illustration of Priority Queue


Operation

output

Priority Queue

insert(5,A)

{(5,A)}

insert(9,C)

{(5,A),(9,C)}

insert(3,B)

{(3,B),(5,A),(9,C)}
{(3,B),(5,A),(7,D),(9,C)}

insert(7,D)

minElement()

{(3,B),(5,A),(7,D),(9,C)}

minKey()

{(3,B),(5,A),(7,D),(9,C)}

removeMin()

{(5,A),(7,D),(9,C)}

size()

{(5,A),(7,D),(9,C)}

minElement()

{(5,A),(7,D),(9,C)}

deleteMin()

{(7,D),(9,C)}

deleteMin()

{(9,C)}

deleteMin()

{}

deleteMin()

error

{}

true

{}

isEmpty()
C-C Tsai

P.3

Heaps Applied for Priority Queue

Max heap tree: a tree in which the key value in


each node is no smaller than the key values in its
children. A max heap is a complete binary tree
that is also a max tree.
Min heap tree: a tree in which the key value in
each node is no larger than the key values in its
children. A min heap is a complete binary tree
that is also a min tree.
max heap tree
[2] 12
[4]

C-C Tsai

10

[1]

[1] 14

[5]

[3]
8

[6]
6

min heap tree


[2]

7
[4]

[3]

7
[5]

10

[6]
6
P.4

Double-Ended Priority Queues


For double-ended priority queues (DEPQ), the
operations supported by a min priority queue are:

SP1: Return an element with minimum priority.


SP2: Return an element with maximum priority.
SP3: Insert an element with an arbitrary priority.

SP4: Delete an element with minimum priority.

SP5: Delete an element with maximum priority.

Primary operations

Insert
Delete Max
Delete Min

Note that a single-ended priority queue supports just


one of the above remove operations.

C-C Tsai

P.5

Leftist Trees

C-C Tsai

Leftist trees provide an efficient


implementation of meldable priority queues.
Linked binary tree.
Can do everything a heap can do and in the
same asymptotic complexity.
Can meld two leftist tree priority queues in
O(log n) time.

P.6

Extended Binary Trees


Two extended binary trees

Internal node

External node

C-C Tsai

P.7

Height-Based Leftist Tree (HBLT)

For any node x in an extended binary tree. Let leftChild(x) and


rightChild(x) denote the left and right children of the internal x,
respectively. Define shortest(x) be the length of a shortest path from x
to an external node in the subtree rooted at x.
If x is an external node, shortest(x) = 0;
otherwise
shortest(x) = 1+min {shortest(leftChild(x)), shortest(rightChild(x))}
2
2
1

C-C Tsai

2
1

1
1

P.8

The Definition of Leftist Trees

A leftist tree is a binary tree such that if it is not


empty, then for every internal node x:
shortest(leftChild(x) >= shortest(rightChild(x))
Is not a leftist tree

Is a leftist tree

2
1

C-C Tsai

P.9

Lemma of Leftist Trees

Let x be the root of a leftist tree that has n internal nodes.


a) n >= 2shortest(x) 1
b) The rightmost root to external node path is the shortest
root to external node path. Its length is
shortest(x) <= log2(n+1)
x

shortest(x)

C-C Tsai

P.10

Min-Leftist Trees
Definition: A min-leftist tree (max-leftist tree) is
a leftist tree in which the key value in each
node is no larger (smaller) than the key values
in its children (if any).
Two min-leftist trees:

2
7
11
13

50

12

80
20

10
18

15

C-C Tsai

P.11

Combination of leftist trees

The combination steps (min-leftist trees)


1. Choose minimum root of the two trees, A and B.
2. Leave the left subtree of smaller root (suppose A)
unchanged and combine the right subtree of A with
B. Back to step 1, until no remaining vertices.
3. Compare shortest(x) and swap to make it satisfy
the definition of leftist trees.

C-C Tsai

P.12

Example1: Meld Two Leftist Trees


2

1 7

1 50

1 11

1 9

1 80

2 12

1 13

1 20
2

5
1 8

1 10
1
1
18 15

combine

7
11

50

13

80

12
20

C-C Tsai

10
18

15

P.13

Example1: Meld Two Leftist Trees


2

2
7
11

11

13

12
20

50
18

10

80

13

12
20

10
18

15

50
80

15

C-C Tsai

P.14

Example1: Meld Two Leftist Trees


2

2
7

11

11

13

12
20

13

10

12

50

15

18

20

80

10
18

15

50
80

C-C Tsai

P.15

Example1: Meld Two Leftist Trees


2 2

2 2

17
1 11
1 13

5 2

17

19

8 2

2 12

10 1

1 20 1 18

15 1

1 11
50 1

1 13

2 12

C-C Tsai

15 1

1 9

80 1

10 1
15 1

50 1
80 1

1 7

8 2
50 1

8 2

exchange2

5 2

10 1

19

1 20 1 18

80 1

exchange1

5 2

2 12

1 20 1 18

1 11
1 13
P.16

Combination of leftist trees


Step 1. Choose smaller
root and set as a.
Step 2. If a has no
right_chlid, set b as as
right_child. Else,
recursively combine as
right_child and b.
Step 3. Make the
combined tree satisfied
the leftist tree property.

O(log n)

C-C Tsai

P.17

Combination of leftist trees

Both insert and delete min operations can be


implemented by using the combine operation.

C-C Tsai

Insert: Treat the inserting node as a single node


binary tree. Combine with the original one.
Delete: Remove the node can get two separate
subtrees. Combine the two trees.

P.18

Weight-Based Leftist Tree (WBLT)

Define the weigth w(x) of node x to be the number of internal


nodes in the subtree with root x. The weight is 0 for an external
node and is 1+ sum of the children weights of an internal
node.
A binary tree is a WBLT iff at every internal node the w value of the
left child is greater than or equal to the w value of the right child.
For any node x in an extended binary tree. The length, rightmost(x),
of the rightmost path from x to an external node satisfies
rightmost(x) <= log2(w(x)+1)
6

C-C Tsai

2
1

0
0

1
0

P.19

Binomial Heaps
Definition of Binomial trees:
k
A binomial tree Bk has 2 nodes with height be k.

B0

B1

B2

B3

Bk

Bk-1

Bk-1

It has ( i )nodes at depth i.


The ith child of root is the root of subtree Bi-1.
k

B4

Depth 14 nodes.
Depth 26 nodes.
Depth 34 nodes.
C-C Tsai

B3

B2

B1 B0

P.20

10

Definition of Binomial Heaps


A min (max) binomial heap (B-heap) is a
collection of min (max) trees.
The min trees should be Binomial trees.
Example: a B-heap consists of three min trees

1
3
8

10

12
4

15

30

16

20

C-C Tsai

P.21

Representation of Binomial Heaps

The representation of B-heap:


Degree: number of children a node has.
Child: point to any one of its children.
Left_link, Right_link: maintain doubly linked circular
list of siblings.
The position of pointer a is the min element.
a
8
10

12

16

pointer
Siblings

15

30

parent
child

C-C Tsai

20

P.22

11

Combination of Binomial Heaps


Combine a min tree of 40

40

10

12

15

30

16

20

C-C Tsai

P.23

Combination of Binomial Heaps

Combine a min tree of 40


a

40

10

C-C Tsai

12

15

20

30

16

P.24

12

Combination of Binomial Heaps


Pairwise1 combine

10

12

15

30

20

40

16

20

C-C Tsai

P.25

Combination of Binomial Heaps

Pairwise1 combine
a

20

10

12

16

40
6

C-C Tsai

15

20

30

P.26

13

Combination of Binomial Heaps


Pairwise2 combine

20

10

12

16

40
15

30

20

C-C Tsai

P.27

Combination of Binomial Heaps

Pairwise2 combine
a

20

10

12

15

30

16

40
C-C Tsai

20

P.28

14

Combination of Binomial Heaps


Pairwise2 combine

20

10

12

15

30

16

40
20

C-C Tsai

P.29

Combination of Binomial Heaps

Pairwise3 combine
a

20

40
C-C Tsai

10

12

15

20

30

16

P.30

15

Combination of Binomial Heaps


Pairwise3 combine

a
1

20

C-C Tsai

10

12

15

30

16

20

P.31

40

Combination of Binomial Heaps

Pairwise3 combine
a
1

20

C-C Tsai

40

10

12

15

30

16

20

P.32

16

Combination of Binomial Heaps


The insertion is to combine a single vertex Bheap to original B-heap.
After we delete the min element, we get several
B-heaps that originally subtrees of the removed
vertex. Then combine them together.

C-C Tsai

P.33

Time Complexity of Binomial Heaps

Insert

Leftist trees Binomial heaps


Actual Amortized
O(log n)
O(1) O(1)

Delete min (or max) O(log n)

O(n)

O(log n)

Meld

O(1)

O(1)

C-C Tsai

O(log n)

P.34

17

Fibonacci Heaps
A min (max) Fibonacci heap (F-heap) is a
collection of min (max) trees.
The min trees need not be Binomial trees.
B-heaps are a special case of F-heaps.

So that what B-heaps can do can be done in F-heaps.


More than that, F-heap may delete an arbitrary node
and decrease key.

C-C Tsai

P.35

Deletion From an F-heap

The deletion of 12

10

C-C Tsai

12

15

20

30

16

P.36

18

Deletion From an F-heap


After the deletion of 12

10

15

30

16

20

C-C Tsai

P.37

Deletion From an F-heap

After the deletion of 12

10

C-C Tsai

15

20

30

16

P.38

19

Decrease Key From an F-heap


Decrease key of 15 to be 11

10

12

11 15

30

16

20

C-C Tsai

P.39

Decrease Key From an F-heap

After decrease key of 15=>11

10

C-C Tsai

12

11

20

30

16

P.40

20

Decrease Key From an F-heap


After decrease key of 15=>11

10

C-C Tsai

12

11

20

30

16

P.41

Cascading Cut

When theNode is cut out of its sibling list in a


remove or decrease key operation, follow path
from parent of theNode to the root.
Encountered nodes (other than root) with
ChildCut = true are cut from their sibling lists and
inserted into top-level list.
Stop at first node with ChildCut = false.
For this node, set ChildCut = true.

C-C Tsai

P.42

21

Cascading Cut Example


1

Decrease key by 2.

2 F

T 7

8
6

theNode
8

C-C Tsai

P.43

Cascading Cut Example

1
2 F

7
8

T 7

4
6

C-C Tsai

9
9

P.44

22

Cascading Cut Example


1

6
2 F

7
8

7
8

9
9

C-C Tsai

P.45

Cascading Cut Example


1

7
8

6
2 F

9
9

7
8

C-C Tsai

P.46

23

Cascading Cut Example


1
3

7
8

6
2 T

9
9

7
8

Actual complexity of cascading cut is O(h) = O(n).


C-C Tsai

P.47

Time Complexity of Fibonacci Heaps


Insert

Actual
O(1)

Amortized
O(1)

Delete min (or max)

O(n)

O(log n)

Meld

O(1)

O(1)

Delete

O(n)

O(log n)

Decrease key (or


increase)

O(n)

O(1)

C-C Tsai

P.48

24

Pairing Heaps
Insert

Fibonacci Pairing
O(1)
O(1)

Delete min (or max)

O(n)

O(n)

Meld

O(1)

O(1)

Delete

O(n)

O(n)

Decrease key (or


increase)

O(n)

O(1)

C-C Tsai

P.49

Pairing Heaps
Insert

Fibonacci Pairing
O(1)
O(log n)

Delete min (or max)

O(log n)

O(log n)

Meld

O(1)

O(log n)

Delete

O(log n)

O(log n)

Decrease key (or


increase)

O(1)

O(log n)

C-C Tsai

P.50

25

Pairing Heaps
Experimental results suggest that pairing heaps
are actually faster than Fibonacci heaps.

Simpler to implement.
Smaller runtime overheads.
Less space per node.

C-C Tsai

P.51

Definition

A min (max) pairing heap is a min (max) tree


in which operations are done in a specified
manner.
8

1
C-C Tsai

P.52

26

Node Structure
Child

Pointer to first node of children list.

Left and Right Sibling

Used for doubly linked linked list (not circular) of


siblings.
Left pointer of first node is to parent.
x is first node in list iff x.left.child = x.

Data
Note: No Parent, Degree, or ChildCut fields.

C-C Tsai

P.53

Meld Max Pairing Heap


Compare-Link Operation

Compare roots.
Tree with smaller root becomes leftmost subtree.

+
6

Actual cost = O(1).


C-C Tsai

P.54

27

Insert

Create 1-element max tree with new item


and meld with existing max pairing heap.

+ insert(2) =

C-C Tsai

P.55

Insert

Create 1-element max tree with new item


and meld with existing max pairing heap.
14

C-C Tsai

+ insert(14)=

Actual cost = O(1).

P.56

28

Worst-Case Degree

Insert 9, 8, 7, , 1, in this order.

Worst-case degree = n 1.
C-C Tsai

P.57

Worst-Case Height

Insert 1, 2, 3, , n, in this order.


5
4

Worst-case height = n.

3
2
1

C-C Tsai

P.58

29

IncreaseKey(theNode, theAmount)

Since nodes do not have parent fields, we


cannot easily check whether the key in theNode
becomes larger than that in its parent.
So, detach theNode from sibling doubly-linked
list and meld.

C-C Tsai

P.59

IncreaseKey(theNode, theAmount)
9

theNode
4=>18

C-C Tsai

If theNode is not the root, remove subtree


rooted at theNode from its sibling list.

P.60

30

IncreaseKey(theNode, theAmount)
9

18

Meld subtree with remaining tree.

C-C Tsai

P.61

IncreaseKey(theNode, theAmount)
18

Actual cost = O(1).


C-C Tsai

P.62

31

Delete Max
If empty => fail.
Otherwise, remove tree root and meld
subtrees into a single max tree.
How to meld subtrees?

Good way => O(log n) amortized complexity for


remove max.
Bad way => O(n) amortized complexity.

C-C Tsai

P.63

Bad Way To Meld Subtrees

currentTree = first subtree.


for (each of the remaining trees)
currentTree = compareLink (currentTree,
nextTree);

C-C Tsai

P.64

32

Example
9

Delete max.
8

Meld into one tree.


7
C-C Tsai

6
P.65

Example

Actual cost of insert is 1.


Actual cost of delete max is degree of root.
n/2 inserts (9, 7, 5, 3, 1, 2, 4, 6, 8) followed by
n/2 delete maxs.

C-C Tsai

Cost of inserts is n/2.


Cost of delete maxs is 1 + 2 + + n/2 1 = (n2).
If amortized cost of an insert is O(1), amortized cost
of a delete max must be (n).

P.66

33

Good Ways To Meld Subtrees


Two-pass scheme and Multipass scheme. Both have
same asymptotic complexity.
Two-pass scheme gives better observed performance.
Pass 1.

Examine subtrees from left to right.


Meld pairs of subtrees, reducing the number of subtrees to
half the original number.
If # subtrees was odd, meld remaining original subtree with
last newly generated subtree.

Pass 2.

Start with rightmost subtree of Pass 1. Call this the working


tree.
Meld remaining subtrees, one at a time, from right to left,
into the working tree.

C-C Tsai

P.67

Two-Pass Scheme Example


T1

T2

S1

T3

T4

S2

T5

T6

S3

T7

T8

S4

R1

R2

C-C Tsai

R3

P.68

34

Multipass Scheme
Place the subtrees into a FIFO queue.
Repeat until 1 tree remains.

Remove 2 subtrees from the queue.


Meld them.
Put the resulting tree onto the queue.

C-C Tsai

P.69

Multipass Scheme Example


T1

T2

T3

T4

T5

T6

T4

T5

T6

T7
C-C Tsai

T3

T7

T8

T5

T7

T8

S1

T6

T8

S1

S2

T7

T8

S1

S2

S3
P.70

35

Multipass Scheme--Example
T7

S1

T8

S1

S2

S2

S3

S3

S3

S4

R1

S4

R2

R1
C-C Tsai

P.71

Multipass Scheme--Example
R1

R2

Q1

Actual cost = O(n).


C-C Tsai

P.72

36

Delete Nonroot Element


Remove theNode from its sibling list.
Meld children of theNode using either 2-pass or
multipass scheme.
Meld resulting tree with whats left of original
tree.

C-C Tsai

P.73

Delete(theNode)
9

theNode

1
C-C Tsai

Remove theNode from its doubly-linked


sibling list.
P.74

37

Delete(theNode)
9

4
3

Meld children of theNode.

C-C Tsai

P.75

Delete(theNode)
9

3
4
2

1
C-C Tsai

Meld with whats left of original tree.

P.76

38

Delete(theNode)
9

Actual cost = O(n).

1
C-C Tsai

P.77

MIN-MAX Heaps

Complete binary tree.


Set root on a min level.
A node x in min level would have smaller key
value than all its descendents. (x is a min node.)
7

min

70

40

30
45
C-C Tsai

9
50

30

10
20

12

max

15

min

max
P.78

39

Insertion into a min-max heap


Insert 5
min

70

30

45

50

max

40

30

10

20

12

15

min

max

C-C Tsai

P.79

Insertion into a min-max heap


Insert 5
min

70

30

45

C-C Tsai

50

max

40

30

20

12

15

10

min

max

P.80

40

Insertion into a min-max heap


Insert 5
min

70

30

45

50

max

40

30

20

12

15

10

min

max

C-C Tsai

P.81

Insertion into a min-max heap

Check if it satisfies min heap.


(Compare with its parent.)
If no, move the key of current
parent to current position.
If yes, skip.

C-C Tsai

P.82

41

Insertion into a min-max heap


Initial Heap={7,70,40,30,9,10,15,45,50,30,20,12}

parent=7;
We want to insertion 80 into the position 13
*n=13, item=80;
80>10 verify_max(heap,13,80)
min

7
40

70
30
45

9
50

30

15 min

10
20

12

max

80

max

C-C Tsai

P.83

Insertion into a min-max heap

min

7
40

70
30
45

9
50

C-C Tsai

30

12

verify_max(Heap,13,80)
grandparent=3
80>40
heap[13]=heap[3]
i=3
grandparent=0

15 min

10
20

max

Input:

80

max

P.84

42

Insertion into a min-max heap

min

7
80

70
30
45

9
50

30

15 min

10
20

12

max

40

Input
verify_max(Heap,3,80)
grandparent=null
break;
heap[3]=80;

max

C-C Tsai

P.85

Insertion into a min-max heap

The time complexity of insertion into a minmax heap with n elements is O(log n).

C-C Tsai

A min-max heap with n elements has O(log n) levels.

P.86

43

Deletion of min element


The smallest element is in the root.
We do the deletion as follows:

Remove the root node and the node x which is the


end of the heap.
Reinsert the key of x into the heap.

C-C Tsai

P.87

Deletion of min element


Delete 7
min

70

30

45

C-C Tsai

50

max

40

30

10

20

12

15

min

max

P.88

44

Deletion of min element


Delete 7
12

70

50

max

40

30

45

min

30

10

15

min

max

20

C-C Tsai

P.89

Deletion of min element


The reinsertion may have 2 cases:
Case1: No child. (Only one node in the heap)

It means the item is the only one element, so


it should be at root in heap.

C-C Tsai

P.90

45

Deletion of min element


Case2: The root has at least one child
Find the min value. (Let this be node k.)
a. A item.key heap[k].key
It means the item is the min element, and the
min element should be at root in heap.
i

C-C Tsai

P.91

Deletion of min element


b. item.key> heap[k].key and k is child of root.
Since k is in max level, it has no descendants.
i
k

C-C Tsai

k
i

P.92

46

Deletion of min element


c. item.key> heap[k].key and k is grandchild of
root.
Example (p>i,): We should make sure the node in
max level contains the largest key. Then redo
insertion to the subtree with the root in red line.
i
p
k

C-C Tsai

k
p
i

P.93

Deletion of min element

Get the min value of the heap.


Move k to the root of this heap.
Swap i and p in case 2c.

C-C Tsai

P.94

47

Deletion of min element


12

Delete 7
70
9
50

max

40

30
45

min

30

10

15

max

20

min

70

45

12
50

max

40

30
30

10
20

min

15

min
max

C-C Tsai

P.95

Deletion of min element

Deletion of min element of a min-max heap


with n elements need O(log n) time.

C-C Tsai

In each iteration, i moves down two levels. Since a


min-max heap is a complete binary tree, heap has
O(log n) levels.

P.96

48

Symmetric Min-Max Heaps


Symmetric Min-Max Heap (SMMH) is a
complete binary tree.
SMMH is either empty or satisfies the
properties:

The root contains no element.


The left subtree is a min-heap.
The right subtree is a max-heap.
If the right subtree is not empty. Let i be any node
in left subtree, and j be the corresponding node in
the right subtree. If no, choose the parent one.
i_key j_key.

C-C Tsai

P.97

Example of a SMMH

Max-heap

Min-heap
5

45

10

15

C-C Tsai

19

25

30

40

20

P.98

49

SMMH
Let i be any node in left subtree and j be the
corresponding node in the right subtree. The relation
between i and j.

j = i + 2log2 i1;
if ( j > n)
j / = 2;

Ex1:

i = 4;
j = 4+2(2-1) = 6;

1
2

10

6
11

Ex2:
7

i = 9;
j = 9+2(3-1) = 13;
j =13 > 12 j = 6;

12

C-C Tsai

P.99

Insertion into a SMMH


The insertion steps

1.

2.

max_heap(n): Check iff n is a position in the maxheap of the SMMH.


min_partner(n) or max_partner(n) : Compute the
min-heap / max-heap node that corresponding to n.
(

1.
2.

C-C Tsai

n 2log2 n1

(n + 2 log 2 n 1 ) / 2 )

Compare key of i and j to satisfy SMMH.


min_insert or max_insert.

P.100

50

Insertion into a SMMH


Insert 4: i = 13-2(3-1) = 13-4 = 9 for min_heap

45

10

15

19

25

30

20

40

i
C-C Tsai

P.101

Insertion into a SMMH

45

10

15

C-C Tsai

25

30

20

40

19

P.102

51

Insertion into a SMMH

45

15

10

25

30

20

40

19

C-C Tsai

P.103

Insertion into a SMMH


Step 1. max_heap.
Step 2. min_partner.

Step 3. Compare key of i and j.


Step 4. min_insert or max_insert.

The time complexity is O(log n) as the height of the SMMH is O(log n).
C-C Tsai

P.104

52

Deletion of min element


The deletion steps

1.

2.

3.

Save the last element as temp and remove this


node from SMMH.
Find the node with smaller key from the children of
removed minimum element and loop down until
reaching leaves.
Insert temp into the left subtree of SMMH.

C-C Tsai

P.105

Deletion of min element


Delete 5:

45

10

15

C-C Tsai

19

25

30

20

40

temp

P.106

53

Deletion of min element

45

i
10

25

40

j
15

19

30

temp:20

C-C Tsai

P.107

Deletion of min element

45

10

25

40

i
15

19

30

temp:20

j
C-C Tsai

P.108

54

Deletion of min element

45

10

15

19

25

30

40

temp:20

i
C-C Tsai

P.109

Deletion of min element

45

10

15

C-C Tsai

19

20

25

40

30

P.110

55

Deletion of min element

Step 1. Save the min element.


Step 2. Find the node with
smaller key.

Step 3. (Exercise 2).

The time complexity is O(log n) as the height of the SMMH is O(log n).
C-C Tsai

P.111

Interval Heaps

Complete binary tree.


Each node (except possibly last one) has 2 elements. Last
node has 1 or 2 elements.
Let a and b be the elements in a node P, a <= b. [a, b] is
the interval represented by P.
The interval represented by a node that has just one
element a is [a, a].
The interval [c, d] is contained in interval [a, b] iff a <= c
<= d <= b.
In an interval heap each nodes (except for root)
interval is contained in that of its parent.

C-C Tsai

P.112

56

Example of an Interval Heap


10,90

15,80
15,20

20,70

25,60

28,55

30,50

30,60

16,19

17,17

35,50

45,60

50,55

47,58

40,45

40,43

Left end points define a min heap.

35

Right end points define a max heap.

Example of an Interval Heap


10,90

15,80
20,70

25,60

28,55

35

30,50

30,60
15,20

16,19

17,17

45,60

50,55

47,58

35,50

40,45

40,43

Min and max elements are in the root.


Store as an array
Height is ~log2 n.

57

Insert An Element
10,90

15,80
15,20

20,70

25,60

28,55

30,50

27,35
35

30,60

16,19

17,17

35,50

45,60

50,55

47,58

40,45

40,43

Insert 27.
New element becomes a left end point.
Insert new element into min heap.

Another Insert
10,90

15,80
20,70

25,60

28,55

30,50

35
18,35

30,60
15,20

16,19

17,17

35,50

45,60

50,55

47,58

40,45

40,43

Insert 18.
New element becomes a left end point.
Insert new element into min heap.

58

Another Insert
10,90

15,80
15,20

20,70

25,60
18,60

28,55

30,50

25,35

30,60

16,19

17,17

35,50

45,60

50,55

47,58

40,45

40,43

Insert 18.
New element becomes a left end point.
Insert new element into min heap.

Another Insert
10,90

15,80
20,70
18,70
,70

20,60

28,55

30,50

25,35

30,60
15,20

16,19

17,17

35,50

45,60

50,55

47,58

40,45

40,43

Insert 18.
New element becomes a left end point.
Insert new element into min heap.

59

Yet Another Insert


10,90

15,80
15,20

20,70

25,60

28,55

30,50

30,60

16,19

35,50

45,60

17,17

50,55

47,58

40,45

40,43

Insert 82.
New element becomes a right end point.

35
35,82

Insert new element into max heap.

After 82 Inserted
10,90

15,82
20,80

25,70

28,55

30,50

30,60
15,20

16,19

17,17

45,60

50,55

47,58

35,50

40,45

40,43

35,60

60

One More Insert Example


10,90

15,82
15,20

20,80

25,70

30,50

16,19

17,17

35,50

45,60

50,55

47,58

40,45

40,43

Insert 8.
New element becomes both a left and a
right end point.
Insert new element into min heap.

28,55

30,60

After 8 Is Inserted
8,90

10,82
15,80

20,70

28,55

30,50

30,60
15,20

16,19

17,17

45,60

50,55

47,58

35,50

40,45

40,43

25

61

Remove Min Element


Remove
If n =
If n =
If n =
If n >

Min element
0 => fail.
1 => heap becomes empty.
2 => only one node, take out left end point.
2 => not as simple.

C-C Tsai

P.123

Remove Min Element Example


10,90

35

15,82
20,80

25,70

28,55

30,50

35,60

30,60
15,20

16,19

17,17

45,60

50,55

47,58

35,50

40,45

40,43

Remove left end point from root.


Remove left end point from last node.
Delete last node if now empty.
Reinsert into min heap, begin at root.

62

Remove Min Element Example


15,90
35
15,82
,82
15,20

20,80

25,70

28,55

30,50

60

30,60

16,19

35,50

45,60

17,17

50,55

47,58

40,45

40,43

Swap with right end point if necessary.

Remove Min Element Example


15,90

15,82

30,60
35

20,80

25,70

28,55

60

30,50

15,20
,20

16,19

17,17

45,60

50,55

47,58

35,50

40,45

40,43

Swap with right end point if necessary.

63

Remove Min Element Example


15,90

15,82

30,60
16,35

20,80

35,50

45,60

20
25,70

28,55

30,50

60

16,19
,19

17,17

50,55

47,58

40,45

40,43

Swap with right end point if necessary.

Remove Min Element Example


15,90

15,82
20,80

25,70

28,55

30,50

30,60
16,35

19,20

17,17

45,60

50,55

47,58

35,50

40,45

40,43

60

64

Application Of Interval Heaps

Complementary range search problem.


Collection of 1D points (numbers).
Insert a point. O(log n)
Remove a point given its location in the structure. O(log n)
Report all points not in the range [a,b], a <= b.
O(k), where k is the number of points not in the range.

1. If the interval tree is empty, return.


2. If the root interval is contained in [a,b], then all points are in the
range, return.
3. Report the end points of the root interval that are not in the range [a,b].
4. Recursively search the left subtree of the root for additional points
that are not in the range [a,b].
5. Recursively search the right subtree of the root for additional points
that are not in the range [a,b].
6. return.
C-C Tsai

P.129

Example of Range Search


10,90

15,80
20,70

25,60

28,55

35

30,50

30,60
15,20

16,19

17,17

45,60

50,55

47,58

35,50

40,45

40,43

[5,100] : All points are in the range


[2,65]: Some points are in the range

65

You might also like