chapter 9 - 歡迎蒞臨南華大學chun/ds(ii)-ch09-priorityqueues.pdf3 c-c tsai p.5 double-ended...

65
1 C-C Tsai P.1 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.2 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: 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();

Upload: vothuan

Post on 15-Apr-2019

234 views

Category:

Documents


0 download

TRANSCRIPT

1

C-C Tsai P.1

Chapter 9 Priority Queues

Single- and Double-Ended Priority QueuesLeftist TreesBinomial HeapsFibonacci HeapsPairing HeapsMin-Max HeapsInterval Heaps

C-C Tsai P.2

Single- and Double-Ended Priority QueuesA 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:

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();

2

C-C Tsai P.3

Illustration of Priority Queue

{}-deleteMin()

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

{(9,C)}-deleteMin()

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

{}trueisEmpty()

{}“error”deleteMin()

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

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

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

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

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

{(3,B),(5,A),(9,C)}-insert(3,B)

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

{(5,A)}-insert(5,A)

Priority QueueoutputOperation

C-C Tsai P.4

Heaps Applied for Priority QueueMax 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 treethat 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 treethat is also a min tree.

[4]

14

12 7

810 6

[1]

[2] [3]

[5] [6]

2

7 4

810 6

[1]

[2] [3]

[5] [6][4]

max heap tree min heap tree

3

C-C Tsai P.5

Double-Ended Priority QueuesFor 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 operationsInsertDelete MaxDelete Min

Note that a single-ended priority queue supports just one of the above remove operations.

C-C Tsai P.6

Leftist TreesLeftist 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.

4

C-C Tsai P.7

Two extended binary trees

Extended Binary Trees

A G

B HC I

ED F J

Internal node

External node

C-C Tsai P.8

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 xto 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

2 11 1

11 1 1

5

C-C Tsai P.9

The Definition of Leftist TreesA 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))

2 2

2 11 1

11 1 1

Is not a leftist tree Is a leftist tree

C-C Tsai P.10

Lemma of Leftist TreesLet x be the root of a leftist tree that has n internal nodes.a) n >= 2shortest(x) – 1b) 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)

6

C-C Tsai P.11

Min-Leftist TreesDefinition: 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 50

11 80

13

5

9 8

12 10

20 1518

C-C Tsai P.12

Combination of leftist treesThe 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.

7

C-C Tsai P.13

Example1: Meld Two Leftist Trees

2

7

5011

8013

5

9 8

12 10

20 1518

2

7 50

11 80

13

5

9 8

12 10

20 1518

2

1

2

1

21

1

1

1

1

11

1

1

combine

C-C Tsai P.14

Example1: Meld Two Leftist Trees

2

7

11

13

5

9

12

20 18

8

10

15

2

7

11

13

5

9

12

20 18

50

80

8

10

15

50

80

8

C-C Tsai P.15

Example1: Meld Two Leftist Trees

2

7

50

11

80

13

5

9 8

12 10

20 1518

8

10

15

2

7

11

13

5

9

12

20 18

50

80

C-C Tsai P.16

Example1: Meld Two Leftist Trees2

7

50

11

80

13

5

9 8

12 10

20 1518

2

1

1

1

2

2

1

1111

2

1

1

2

7

50

11

80

13

5

98

1210

2015 18

2

1

1

1

2

2

1

11 11

2

1

1

2

7

50

11

80

13

5

9 8

12 10

20 1518

2

1

1

1

2

2

1

1111

2

1

1

exchange1 exchange2

9

C-C Tsai P.17

Combination of leftist trees

Step 1. Choose smaller root and set as a.

Step 2. If a has no right_chlid, set b as a’sright_child. Else, recursively combine a’sright_child and b.

Step 3. Make the combined tree satisfied the leftist tree property.

O(log n)

C-C Tsai P.18

Combination of leftist treesBoth insert and delete min operations can be implemented by using the combine operation.

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.

10

C-C Tsai P.19

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 4

3 22 1

11 1 1

0

0

0 00 00

0 0 0

C-C Tsai P.20

Binomial HeapsDefinition of Binomial trees:

A binomial tree Bk has 2k nodes with height be k.Bk

Bk-1

Bk-1

B0 B1 B2 B3

It has nodes at depth i.The ith child of root is the root of subtree Bi-1. B4

B2B3 B1 B0

Depth 1:4 nodes.

Depth 2:6 nodes.

Depth 3:4 nodes.

)(ki

11

C-C Tsai P.21

Definition of Binomial HeapsA 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

10

8 5

3

6

4 15

12

20

30

7

1

16

9

C-C Tsai P.22

Representation of Binomial HeapsThe 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.

8

10

3

5 4

6

1

7 1612

15 30

20

9

Siblings

parent

child

a

pointera

12

C-C Tsai P.23

Combination of Binomial Heaps

8

10

3

5 4

6

1

7 1612

15 30

20

9

a

40

Combine a min tree of 40

C-C Tsai P.24

Combination of Binomial Heaps

8

10

3

5 4

6

1

7 1612

15 30

20

9

a

40

Combine a min tree of 40

13

C-C Tsai P.25

Pairwise1 combine

Combination of Binomial Heaps

8

10

3

5 4

6

1

7 1612

15 30

20

9

a

20

40

C-C Tsai P.26

Pairwise1 combine

Combination of Binomial Heaps

8

10

3

5 4

6

1

7 1612

15 30

20

9

a

20

40

14

C-C Tsai P.27

Pairwise2 combine

Combination of Binomial Heaps

8

10

3

5 4

6

1

7 1612

15 30

20

9

a

20

40

C-C Tsai P.28

Pairwise2 combine

Combination of Binomial Heaps

8

10

3

5 4

6

1

7 1612

15 30

20

9

a

20

40

15

C-C Tsai P.29

Pairwise2 combine

Combination of Binomial Heaps

8

10

3

5 4

6

1

7 1612

15 30

20

9

a

20

40

C-C Tsai P.30

Pairwise3 combine

Combination of Binomial Heaps

8

10

3

5 4

6

1

7 1612

15 30

20

9

a

20

40

16

C-C Tsai P.31

Pairwise3 combine

Combination of Binomial Heaps

8

10

3

5 4

6

1

7 1612

15 30

20

9

a

20

40

C-C Tsai P.32

Combination of Binomial Heaps

8

10

3

5 4

6

1

7 1612

15 30

20

9

a

20

40

Pairwise3 combine

17

C-C Tsai P.33

Combination of Binomial HeapsThe insertion is to combine a single vertex B-heap 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.34

Time Complexity of Binomial Heaps

Binomial heaps Leftist treesActual Amortized

Insert O(log n) O(1) O(1)

Delete min (or max) O(log n) O(n) O(log n)

Meld O(log n) O(1) O(1)

18

C-C Tsai P.35

Fibonacci HeapsA 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 nodeand decrease key.

C-C Tsai P.36

Deletion From an F-heapThe deletion of 12

8

10

3

5 4

6

1

7 1612

15 30

20

9

19

C-C Tsai P.37

Deletion From an F-heapAfter the deletion of 12

8

10

3

5 4

6

1

7 16

15 30

20

9

C-C Tsai P.38

Deletion From an F-heapAfter the deletion of 12

8

10

3

5 4

6

1

7 16

15 30

20

9

20

C-C Tsai P.39

Decrease Key From an F-heapDecrease key of 15 to be 11

8

10

3

5 4

6

1

7 1612

15 30

20

911

C-C Tsai P.40

Decrease Key From an F-heapAfter decrease key of 15=>11

8

10

3

5 4

6

1

7 1612

11 30

20

9

21

C-C Tsai P.41

Decrease Key From an F-heapAfter decrease key of 15=>11

8

10

3

5 4

6

1

7 1612

11 30

20

9

C-C Tsai P.42

Cascading CutWhen 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.

22

C-C Tsai P.43

Cascading Cut Example

8

7 3

1

6

5 9

2

8

6 7

4

9

9 8

theNode

T

T

F

Decrease key by 2.

C-C Tsai P.44

Cascading Cut Example

8

7 3

1

6

5 9

2

6 7

4

6

9

9 8

T

T

F

23

C-C Tsai P.45

Cascading Cut Example

8

7 3

1

6

5 9

2

6

7

4

6

9

9 8

T

F

C-C Tsai P.46

Cascading Cut Example

8

7 3

1

6

5 9

2 6

7 46

9

9 8F

24

C-C Tsai P.47

Cascading Cut Example

8

7 3

1

6

5 9

2 6

7 46

9

9 8T

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

C-C Tsai P.48

Time Complexity of Fibonacci Heaps

Actual Amortized Insert O(1) 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)

25

C-C Tsai P.49

Pairing Heaps Fibonacci Pairing Insert 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.50

Pairing Heaps Fibonacci Pairing Insert 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)

26

C-C Tsai P.51

Pairing HeapsExperimental 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.52

DefinitionA min (max) pairing heap is a min (max) tree in which operations are done in a specified manner.

3

5 3

8

1

2 1

4

1

4 5

6

2

3

27

C-C Tsai P.53

Node StructureChild

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.

DataNote: No Parent, Degree, or ChildCut fields.

C-C Tsai P.54

Meld – Max Pairing HeapCompare-Link Operation

Compare roots.Tree with smaller root becomes leftmost subtree.

6

7

3

6 7

9

+ =

3

6 7

9

6

7

•Actual cost = O(1).

28

C-C Tsai P.55

InsertCreate 1-element max tree with new item and meld with existing max pairing heap.

3

6 7

9

6

7+ insert(2) =

3

6 7

9

6

72

C-C Tsai P.56

InsertCreate 1-element max tree with new item and meld with existing max pairing heap.

3

6 7

9

6

7+ insert(14)=

3

6 7

9

6

7

14

•Actual cost = O(1).

29

C-C Tsai P.57

Worst-Case DegreeInsert 9, 8, 7, …, 1, in this order.

9

871 …

•Worst-case degree = n –1.

C-C Tsai P.58

Worst-Case HeightInsert 1, 2, 3, …, n, in this order.

1

2

•Worst-case height = n.3

4

5

30

C-C Tsai P.59

IncreaseKey(theNode, theAmount)Since nodes do not have parent fields, we cannot easily check whether the key in theNodebecomes larger than that in its parent.So, detach theNode from sibling doubly-linked list and meld.

C-C Tsai P.60

IncreaseKey(theNode, theAmount)

If theNode is not the root, remove subtreerooted at theNode from its sibling list.

1

2 6

9

4

5 1

6

1

3 2

4 2

4

3

3

theNode4=>18

31

C-C Tsai P.61

IncreaseKey(theNode, theAmount)

Meld subtree with remaining tree.

1

2 6

9

4

5 1

6

1

3 2

4

2

18

3

3

C-C Tsai P.62

IncreaseKey(theNode, theAmount)

1

2 6

9

4

5 1

6

1

3 2

4

2

18

3

3

•Actual cost = O(1).

32

C-C Tsai P.63

Delete MaxIf 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.64

Bad Way To Meld SubtreescurrentTree = first subtree.for (each of the remaining trees)

currentTree = compareLink (currentTree,nextTree);

33

C-C Tsai P.65

Example

Delete max.

75268 4 1 3

• Meld into one tree.

9

75268 4 1 3

8

6157 3 2 4

C-C Tsai P.66

ExampleActual 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.

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).

34

C-C Tsai P.67

Good Ways To Meld SubtreesTwo-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.68

Two-Pass Scheme – ExampleT2 T3 T4T1 T6 T7 T8T5

S1 S2 S3 S4

R1

R2

R3

35

C-C Tsai P.69

Multipass SchemePlace 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.70

Multipass Scheme – Example

T2T1 T3 T4 T6 T7 T8T5

S2S1T6 T7 T8T5

S1T3 T4 T6 T7 T8T5

S3S2S1T7 T8

36

C-C Tsai P.71

Multipass Scheme--Example

S3S2S1T7 T8

S4S3S2S1

S4S3 R1

R1 R2

C-C Tsai P.72

Multipass Scheme--Example

R1 R2

Q1

•Actual cost = O(n).

37

C-C Tsai P.73

Delete Nonroot ElementRemove theNode from its sibling list.Meld children of theNode using either 2-pass or multipass scheme.Meld resulting tree with what’s left of original tree.

C-C Tsai P.74

Delete(theNode)

Remove theNode from its doubly-linked sibling list.

1

2 6

9

4

5 1

6

1

3 2

4 2

4

3

3

theNode

38

C-C Tsai P.75

Delete(theNode)

Meld children of theNode.

2

4

3

3

1

2 6

9

4

5 1

6

1

3 2

4

C-C Tsai P.76

Delete(theNode)

Meld with what’s left of original tree.

1

2 6

9

4

5 1

6

1

3 2

4

2 3

3

39

C-C Tsai P.77

Delete(theNode)

1

2 6

9

4

5 1

6

1

3 2

42 3

3

•Actual cost = O(n).

C-C Tsai P.78

MIN-MAX HeapsComplete 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

70 40

30 109 15

5045 2030 12

min

max

min

max

40

C-C Tsai P.79

Insertion into a min-max heap

7

70 40

30 109 15

5045 2030 12

min

max

min

max5

Insert 5

C-C Tsai P.80

Insertion into a min-max heap

7

70 40

30 59 15

5045 2030 12

min

max

min

max10

Insert 5

41

C-C Tsai P.81

Insertion into a min-max heap

5

70 40

30 79 15

5045 2030 12

min

max

min

max10

Insert 5

C-C Tsai P.82

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.

42

C-C Tsai P.83

Insertion into a min-max heapInitial 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)

7

70 40

30 109 15

5045 2030 12

min

max

min

max80

C-C Tsai P.84

Insertion into a min-max heapInput: verify_max(Heap,13,80)grandparent=380>40→heap[13]=heap[3]

i=3grandparent=0

7

70 40

30 109 15

5045 2030 12

min

max

min

max80

43

C-C Tsai P.85

Insertion into a min-max heapInputverify_max(Heap,3,80)→ grandparent=null

break;heap[3]=80;

7

70 80

30 109 15

5045 2030 12

min

max

min

max40

C-C Tsai P.86

Insertion into a min-max heapThe time complexity of insertion into a min-max heap with n elements is O(log n).

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

44

C-C Tsai P.87

Deletion of min elementThe 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.88

Deletion of min element

7

70 40

30 109 15

5045 2030 12

min

max

min

max

Delete 7

45

C-C Tsai P.89

Deletion of min element

70 40

30 109 15

5045 2030

min

max

min

max

12

Delete 7

C-C Tsai P.90

Deletion of min element

i i

It means the item is the only one element, so it should be at root in heap.

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

46

C-C Tsai P.91

Deletion of min element

i i

Case2: The root has at least one childFind 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.

C-C Tsai P.92

Deletion of min elementb. item.key> heap[k].key and k is child of root.

i

Since k is in max level, it has no descendants.

k

k

i

47

C-C Tsai P.93

Deletion of min elementc. item.key> heap[k].key and k is grandchild of

root.

i

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.

p

k

k

p

i

C-C Tsai P.94

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.

48

C-C Tsai P.95

Deletion of min elementmin

max

min

max

70 40

30 109 15

5045 2030

12Delete 7

min

max

min

max

70 40

30 1012 15

5045 2030

9

C-C Tsai P.96

Deletion of min elementDeletion of min element of a min-max heap with n elements need O(log n) time.

In each iteration, i moves down two levels. Since a min-max heap is a complete binary tree, heap has O(log n) levels.

49

C-C Tsai P.97

Symmetric Min-Max HeapsSymmetric 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.98

Example of a SMMH

5 45

10 258 40

1915 309 20

Min-heap Max-heap

50

C-C Tsai P.99

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

⎣ ⎦

;2/ )(if

;2 1log2

=>+= −

jnj

ij i

1

2 3

4 65 7

98 1110 12

Ex1:

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

Ex2:

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

j =13 > 12 j = 6;

C-C Tsai P.100

Insertion into a SMMHThe insertion steps

1. max_heap(n): Check iff n is a position in the max-heap of the SMMH.

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

( / )

1. Compare key of i and j to satisfy SMMH.2. min_insert or max_insert.

⎣ ⎦ 1log22 −− nn ⎣ ⎦ 2/)2( 1log2 −+ nn

51

C-C Tsai P.101

Insertion into a SMMH

5 45

10 258 40

1915 309 20 4

ji

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

C-C Tsai P.102

Insertion into a SMMH

5 45

10 258 40

415 309 20 19

52

C-C Tsai P.103

Insertion into a SMMH

4 45

5 258 40

1015 309 20 19

C-C Tsai P.104

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).

53

C-C Tsai P.105

Deletion of min elementThe deletion steps

1. Save the last element as temp and remove this node from SMMH.

2. Find the node with smaller key from the children of removed minimum element and loop down until reaching leaves.

3. Insert temp into the left subtree of SMMH.

C-C Tsai P.106

Deletion of min element

5 45

10 258 40

1915 309 20temp

Delete 5:

54

C-C Tsai P.107

Deletion of min element

45

10 258 40

1915 309

j

temp:20

i

C-C Tsai P.108

Deletion of min element

8 45

10 25 40

1915 309

j

i

temp:20

55

C-C Tsai P.109

Deletion of min element

8 45

10 259 40

1915 30

i

temp:20

C-C Tsai P.110

Deletion of min element

8 45

10 259 40

1915 3020

56

C-C Tsai P.111

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.112

Interval HeapsComplete 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 node’s (except for root) interval is contained in that of its parent.

57

Example of an Interval Heap

28,55 35

25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43

35,5045,6015,2020,70

15,80 30,60

10,90

Left end points define a min heap.

Right end points define a max heap.

28,55 35

25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43

35,5045,6015,2020,70

15,80 30,60

10,90

Min and max elements are in the root.Store as an array Height is ~log2 n.

Example of an Interval Heap

58

Insert An Element

28,55 35

25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43

35,5045,6015,2020,70

15,80 30,60

10,90

Insert 27.3527,35

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

Another Insert

28,55 35

25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43

35,5045,6015,2020,70

15,80 30,60

10,90

Insert 18.18,35 New element becomes a left end point.

Insert new element into min heap.

59

28,55 25,35

25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43

35,5045,6015,2020,70

15,80 30,60

10,90

Insert 18.

18,60

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

Another Insert

28,55 25,35

20,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43

35,5045,6015,2020,70

15,80 30,60

10,90

Insert 18.

,70

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

18,70

Another Insert

60

Yet Another Insert

28,55 35

25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43

35,5045,6015,2020,70

15,80 30,60

10,90

Insert 82.35,82New element becomes a right end point.Insert new element into max heap.

After 82 Inserted

28,55 35,60

25,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43

35,5045,6015,2020,80

15,82 30,60

10,90

61

28,55

25,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43

35,5045,6015,2020,80

15,82 30,60

10,90

One More Insert Example

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

8

2528,55

20,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43

35,5045,6015,2015,80

10,82 30,60

8,90

After 8 Is Inserted

62

C-C Tsai P.123

Remove Min Element

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

Remove Min Element Example

28,55 35,60

25,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43

35,5045,6015,2020,80

15,82 30,60

10,90

Remove left end point from root.Remove left end point from last node.

Reinsert into min heap, begin at root.

10,90

35,60

Delete last node if now empty.

35

63

28,55 60

25,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43

35,5045,6015,2020,80

15,82 30,60

15,90

Swap with right end point if necessary.

,82

35

Remove Min Element Example

28,55 60

25,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43

35,5045,6015,2020,80

15,82 30,60

15,90

Swap with right end point if necessary.

,2035

Remove Min Element Example

64

28,55 60

25,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43

35,5045,6016,3520,80

15,82 30,60

15,90

Swap with right end point if necessary.

,19

20

Remove Min Element Example

28,55 60

25,70 30,50 19,20 17,17 50,55 47,58 40,45 40,43

35,5045,6016,3520,80

15,82 30,60

15,90

Remove Min Element Example

65

C-C Tsai P.129

Application Of Interval HeapsComplementary 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.

Example of Range Search

28,55 35

25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43

35,5045,6015,2020,70

15,80 30,60

10,90

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

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