You are on page 1of 144

Heap Structures

MIN-MAX Heaps

Definition
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.)
MIN-MAX Heaps
7 min

70 40 max

30 9 10 15 min

45 50 30 20 12 max
MIN-MAX Heaps

Insertion into a min-max heap


Insertion into a min-max heap
7 min

70 40 max

30 9 10 15 min

45 50 30 20 12 5 max
Insertion into a min-max heap
7 min

70 40 max

30 9 5 15 min

45 50 30 20 12 10 max
Insertion into a min-max heap
5 min

70 40 max

30 9 7 15 min

45 50 30 20 12 10 max
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.
Insertion into a min-max heap
 Initial
7 min
Heap={7,70,40,30,9,10,15
70 40 max ,45,50,30,20,12}
*n=13
30 9 10 15 min
item=80
45 50 30 20 12 80 max parent=6

80>10
→verify_max(heap,13,80)
Insertion into a min-max heap
7 min
 Input
70 40 max
verify_max(Heap,13,80)
30 9 10 15 min grandparent=3
80>40
45 50 30 20 12 80 max
→heap[13]=heap[3]
i=3
grandparent=0
Insertion into a min-max heap
7 min
 Input
70 80 max
verify_max(Heap,3,80)
30 9 10 15 min → grandparent=null
break;
45 50 30 20 12 40 max
heap[3]=80;
Insertion into a min-max heap
 The 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.
MIN-MAX Heaps

Deletion of min element


Deletion of min element
 The smallest element is in the root.
 We do the deletion as follows:
1. Remove the root node and the node x
which is the end of the heap.
2. Reinsert the key of x into the heap.
Deletion of min element
7 min

70 40 max

30 9 10 15 min

45 50 30 20 12 max
Deletion of min element
12 min

70 40 max

30 9 10 15 min

45 50 30 20 max
Deletion of min element
 The reinsertion may have 2 cases:
1. No child. (Only one node in the heap)

i i

It means the item is the only one element, so


it should be at root in heap.
Deletion of min element
2. The root has at least one child
Find the min value. (Let this be node k.)
a. A item.key≦ heap[k].key
i i

It means the item is the min element, and the


min element should be at root in heap.
Deletion of min element
b. item.key> heap[k].key and k is child of root.

i k

k i

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


Deletion of min element
c. item.key> heap[k].key and k is grandchild of
root.
i k

p p

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


Deletion of min element
 Deletion 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.
Deaps

Definition
Deaps
 Complete binary tree.
 Either empty or satisfies the properties
1. The root contains no element.
2. The left subtree is a min-heap.
3. The right subtree is a max-heap.
4. 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.
Deaps
 The relation between i and j.
j  i  2 log 2 i 1 ;
if ( j  n)
j /  2;

1 Ex1:
i=4;
2 3 j=4+2^(2-1)=6;
Ex2:
4 5 6 7
i=9;
j=9+2^(3-1)=13;
8 9 10 11
j>12
12
j=6;
Deaps

5 45

10 8 25 40

15 19 9 30 20
Deaps

Insertion into a deap


Insertion into a deap
 The insertion steps
1. max_heap(n): Check iff n is a position in
the max-heap of the deap.
2. min_partner(n) or max_partner(n) :
Compute the min-heap/max-heap node
that corresponding to n. ( /
n  2 log) n 1 (n  2 log n 1 ) / 2
2 2

3. Compare key of i and j to satisfy deap.


4. min_insert or max_insert.
Insertion into a deap

5 45

10 8 25 40

15 19 9 30 20 4
j
Insertion into a deap

5 45

10 8 25 40

15 19 9 30 20 4
j
i
Insertion into a deap

5 45

10 8 25 40

15 4 9 30 20 19
Insertion into a deap

4 45

5 8 25 40

15 10 9 30 20 19
Insertion into a deap

Step 1. max_heap.

Step 2. min_partner.

Step 3. Compare key of i and j.

Step 4. min_insert or
max_insert.
Insertion into a deap
 The time complexity is O(log n) as the
height of the deap is O(log n).
Deaps

Deletion of min element


Insertion into a deap
 The insertion steps
1. Save the last element as temp and
remove this node from deap.
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 deap.
Deletion of min element

5 45

10 8 25 40

15 19 9 30 20
temp
Deletion of min element

45
i

10 8 25 40
j

15 19 9 30 temp:20
Deletion of min element

8 45

10 25 40
i

15 19 9 30 temp:20

j
Deletion of min element

8 45

10 9 25 40

15 19 30 temp:20

i
Deletion of min element

8 45

10 9 25 40

15 19 30 temp:20

i
Deletion of min element

8 45

10 9 25 40

15 19 20 30
Deletion of min element

Step 1. Save the min


element.
Step 2. Find the node with
smaller key.

Step 3. (Exercise 2).


Deletion of min element
 The time complexity is O(log n) as the
height of the deap is O(log n).
Leftist Trees

Definition
Leftist Trees
 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.
 For any node x in an extended binary tree,
let shortest(x) be the length of a shortest
path from x to an external node in the
subtree rooted at x.
shortest ( x)  10ifmin{
x is an external node
shortest( left _ child ( x )), shortest( right _ child ( y ))} otherwise
Leftist Trees
 Two binary trees

A G

B C H I

D E F J
Leftist Trees
 Extended binary trees

A G

B C H I

D E F J
Leftist Trees
 The number inside each internal node x
is shortest(x).
2 2

2 1 1 1

1 1 1 1
Leftist Trees
 Definition
A leftist tree is a binary tree such that if it
is not empty, then for every internal node
x:shortest (left _ child ( x))  shortest (right _ child ( x))
2 2

2 1 1 1

1 1 1 1
Leftist Trees
 By the definition
Let x be the root of a leftist tree that has n
internal nodes.
a) n  2 shortest ( x )  1
x A binary tree whose shortest
height is shortest(x) means for
every path from root to leaf has at
shortest(x least shortest(x) nodes.
) Such that it has at least 2shortest(x)-1
nodes.
Leftist Trees
b) The rightmost root to external node path
is the shortest root to external node path.

x
Base on the definition of leftist tree.
shortest (left _ child ( x))  shortest (right _ child ( x))
shortest(x
)
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).
2

7 50

11 80
13
Leftist Trees

Combination of leftist trees


Combination of leftist trees
 The combination step (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.
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’s
right_child. Else,
recursively combine a’s
right_child and b.

Step 3. Make the


combined tree satisfied
the leftist tree property.
Combination of leftist trees

2 5

7 50 9 8

11 80 12 10
13
20 18 15
Combination of leftist trees
2

11
50 5
13 80
9 8

12 10

20 18 15
Combination of leftist trees
2

7
5
11
9

13 50
12 8
80
10
20 18
15
Combination of leftist trees
2

7
5
11
9 8
13
12 10 50

20 18 15 80
Combination of leftist trees
2
2

1 7 5 2

1 11 1 9 8 2

1 13 2 12 10 1 50 1

1 20 1 18 15 1 80 1
Combination of leftist trees
2
2

5 2 1 7

8 2 1 9 1 11

10 1 50 1 2 12 1 13

15 1 80 1 1 20 1 18
Combination of leftist trees
 Both 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.
Binomial Heaps

Definition
Binomial Trees
 [Definition] Binomial trees
 A binomial tree Bk has 2k nodes with height
be k.

B0 B1 B2 B3 Bk

Bk-1

Bk-1
Binomial Trees
k
 It has i ) nodes at depth i.
(
 The ith child of root is the root of subtree
Bi-1. B4

Depth 1 : 4 nodes.

Depth 2 : 6 nodes.

Depth 3 : 4 nodes.

B3 B2 B1 B 0
Binomial Heaps
 Collection of min (max) trees.
 The min trees should be Binomial trees.
1

3 12 7 16

8 5 4 15 30 9

10 6 20
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.
Binomial Heaps
a

8 3 1

10 a pointer
5 4 12 7 16
Siblings

6 15 30 9 parent

child

20
Binomial Heaps

Combination of binomial heaps


Combination of binomial
heaps
a

40 8 3 1

10
5 4 12 7 16

6 15 30 9

20
Combination of binomial
heaps
a

40 8 3 1

10
5 4 12 7 16

6 15 30 9

20
Combination of binomial
heaps
 Pairwise combine a

8 3 1 20

10 40
5 4 12 7 16

6 15 30 9

20
Combination of binomial
heaps
 Pairwise combine a

8 3 1 20

10 40
5 4 12 7 16

6 15 30 9

20
Combination of binomial
heaps
 Pairwise combine a

8 3 1

20 10
5 4 12 7 16

40
6 15 30 9

20
Combination of binomial
heaps
 Pairwise combine a

8 3 1

20 10
5 4 12 7 16

40
6 15 30 9

20
Combination of binomial
heaps
 Pairwise combine a

3 1

8 5 4 12 7 16

20 10 15 30 9
6

40
20
Combination of binomial
heaps
 Pairwise combine a

3 1

8 5 4 12 7 16

20 10 6 15 30 9

40
20
Combination of binomial
heaps
 Pairwise combine a

3 1

8 5 4 12 7 16

20 10 6 15 30 9

40
20
Combination of binomial
heaps
 Pairwise combine a
1

3 12 7 16

8 5 4 15 30 9

20 10 6 20

40
Combination of binomial
heaps
a
1

3 12 7 16

8 5 4 15 30 9

20 10 6 20

40
Combination of binomial
heaps
 The 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.
Time complexity

Leftist trees Binomial heaps


Actual 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)
Fibonacci Heaps

Definition
Fibonacci Heaps
 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.
Fibonacci Heaps
 The size of a subtree rooted in a node of
degree k is at least Fk + 2, where Fk is the kth
Fibonacci number.
 merge is implemented simply by
concatenating the lists of tree roots of the
two heaps.
 Operation insert works by creating a new
heap with one element and doing merge.
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps

Deletion and Decrease key


Deletion and Decrease key
 Deletion

8 3 1

10
5 4 12 7 16

6 15 30 9

20
Deletion and Decrease key
 Deletion

8 3 1

10
5 4 7 16

6 15 30 9

20
Deletion and Decrease key
 Deletion

8 3 1

10
5 4 7 16

6 15 30 9

20
Deletion and Decrease key
 Decrease key

8 3 1

10
5 4 12 7 16

6 11 15 30 9

20
Deletion and Decrease key
 Decrease key

8 3 1

10
5 4 12 7 16

6 11 30 9

20
Deletion and Decrease key
 Decrease key

8 3 1

10
5 4 12 7 16

6 11 30 9

20
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Time complexity
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 O(n) O(1)
increase)
Skew Heaps
Skew Heap
• No need for null path length
• Definition of skew heap

Building a Skew Heap


• Sequence of inserts
• Swap children at every merge step
Motivation
 Simplify leftist heap by
 not maintaining null path lengths

 swapping children at every merge step


Definition
A Skew (min)Heap is a binary tree that satisfies the
follow-ing conditions. If X is a node and L and R
are its left and right children, then:
1. X.value ≤ L.value
2. X.value ≤ R.value

A Skew (max)Heap is a binary tree that satisfies the


follow-ing conditions. If X is a node and L and R
are its left and right children, then:
1. X.value ≥ L.value
2. X.value ≥ R.value
Merging Skew Heaps
Consider two skew heaps …

4 6

19 8 8 7

27 20 12 14

43 15 25

Task: merge them into a single skew heap


Merging Skew Heaps
4 6

19 8 8 7

27 20 12 14

43 15 25

First, instantiate a Stack


Merging Skew Heaps
x y
4 6

19 8 8 7

27 20 12 14

43 15 25

Compare root nodes


merge(x,y)
Merging Skew Heaps
x y
4 6

19 8 8 7

27 20 12 14

43 15 25

Remember smaller value


Merging Skew Heaps
y
4 x 6

19 8 8 7

27 20 12 14

43 15 25

Repeat the process with the right child


of the smaller value
Merging Skew Heaps
y
4 x 6

19 8 8 7

27 20 12 14

43 15 25

6
4

Remember smaller value


Merging Skew Heaps
4 x 6
y
19 8 8 7

27 20 12 14

43 15 25

6
4

Repeat the process with the right child


of the smaller value
Merging Skew Heaps
4 x 6
y
19 8 8 7

27 20 12 14

43 15 25

7
6
4

Remember smaller value


Merging Skew Heaps
4 x 6

19 8 8 7
y
27 20 12 14
null
43 15 25

7
6
4

Repeat the process with the right child


of the smaller value
Merging Skew Heaps
4 x 6

19 8 8 7

27 20 12 14

43 15 25

8
7
6
4

Because one of the arguments is null,


return the other argument
Merging Skew Heaps
4 6

19 Refers to node 8 7 x
27 20 8 14 8

43 12

15 25
8

7
6
4

Make 8 the right child of 7


Merging Skew Heaps
4 6

19 Refers to node 8 7

27 20 8 14 8

43 12

15 25
8

7
6
4

Swap children of node 7


Merging Skew Heaps
4 6

19 Refers to node 8 7

27 20 8 14 8

43 12

15 25

6
4

Return node 7
Merging Skew Heaps
4 6

19 Refers to node 8 7

27 20 8 14 8

43 12

15 25

6
4

Make 7 the right child of 6


(which it already is)
Merging Skew Heaps
4 6

19 Refers to node 7 8

27 20 8 8 14

43 12

15 25

6
4

Swap children of node 6


Merging Skew Heaps
4 6

19 Refers to node 7 8

27 20 8 8 14

43 12

15 25

Return node 6
Merging Skew Heaps
4

19 6

27 20 7 8
8 14
43
12

15 25

Make 6 the right child of 4


Merging Skew Heaps
4
6 19

7 8 27 20
8 14 43
12

15 25

Swap children of node 4


Final Skew Heap
4
6 19

7 8 27 20
8 14 43
12

15 25

• Verify that the tree is heap


• Verify that the heap is skew 4

Return node 4
Analysis
 Height of a skew heap ≈ O(log n)
 Maximum number of values stored in
Stack ≈ 2 * O(log n) ≈ O(log n)
 Total cost of merge ≈ O(log n)
Inserts and Deletes
• To insert a node into a skew heap,
merge the leftist heap with the node
• After deleting root X from a skew heap,
merge its left and right subheaps
• In summary, there is only one
operation, a merge.

You might also like