資料結構範本 - National Tsing Hua University

Download Report

Transcript 資料結構範本 - National Tsing Hua University

Chapter 9
Heap Structures
Instructors:
C. Y. Tang and J. S. Roger Jang
All the material are integrated from the textbook "Fundamentals of Data Structures in
C" and some supplement from the slides of Prof. Hsin-Hsi Chen (NTU).
Outline





MIN-MAX Heaps
Deaps
Leftist Trees
Binomial Heaps
Fibonacci Heaps
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
min
7
70
30
45
9
50
max
40
30
10
20
12
15
min
max
MIN-MAX Heaps
Insertion into a min-max heap
Insertion into a min-max heap
min
7
70
30
45
9
50
max
40
30
10
20
12
15
5
min
max
Insertion into a min-max heap
min
7
70
30
45
9
50
max
40
30
5
20
12
15
10
min
max
Insertion into a min-max heap
min
5
70
30
45
9
50
max
40
30
7
20
12
15
10
min
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

min
7
40
70
30
45
9
50
30
15 min
10
20
12
max
80
max
Initial
Heap={7,70,40,30,9,10,1
5,45,50,30,20,12}
*n=13
item=80
parent=6
80>10
→verify_max(heap,13,80)
Insertion into a min-max heap
min
7

40
70
30
45
9
50
30
15 min
10
20
12
max
80
max
Input
verify_max(Heap,13,80)
grandparent=3
80>40
→heap[13]=heap[3]
i=3
grandparent=0
Insertion into a min-max heap
min
7

80
70
30
45
9
50
30
15 min
10
20
12
max
40
max
Input
verify_max(Heap,3,80)
→ grandparent=null
break;
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.
2.
Remove the root node and the node x
which is the end of the heap.
Reinsert the key of x into the heap.
Deletion of min element
min
7
70
30
45
9
50
max
40
30
10
20
12
15
min
max
Deletion of min element
12
70
9
50
max
40
30
45
min
30
10
20
15
min
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
p
k
k
p
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.
2.
3.
4.
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.
Deaps
The relation between i and j.

j  i  2 log2 i 1 ;
if ( j  n)
j /  2;
Ex1:
1
2
3
4
8
Ex2:
5
9
i=4;
j=4+2^(2-1)=6;
10
6
11
12
7
i=9;
j=9+2^(3-1)=13;
j>12
j=6;
Deaps
5
45
10
15
8
19
9
25
30
20
40
Deaps
Insertion into a deap
Insertion into a deap

The insertion steps
1.
2.
max_heap(n): Check iff n is a position in
the max-heap of the deap.
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)
Compare key of i and j to satisfy deap.
min_insert or max_insert.
2
3.
4.
2
Insertion into a deap
5
45
10
15
8
19
9
25
30
20
40
4
j
Insertion into a deap
5
45
10
15
8
19
9
i
25
30
20
40
4
j
Insertion into a deap
5
45
10
15
8
4
9
25
30
20
40
19
Insertion into a deap
4
45
5
15
8
10
9
25
30
20
40
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.
2.
3.
Save the last element as temp and
remove this node from deap.
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 deap.
Deletion of min element
5
45
10
15
8
19
9
25
30
20
40
temp
Deletion of min element
45
i
10
8
25
j
15
19
9
30
temp:20
40
Deletion of min element
8
45
10
25
i
15
19
j
9
30
temp:20
40
Deletion of min element
8
10
15
45
9
19
i
25
30
temp:20
40
Deletion of min element
8
10
15
45
9
19
i
25
30
temp:20
40
Deletion of min element
8
45
10
15
9
19
20
25
30
40
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.
x is an external node
shortest ( x)  10ifmin{
shortest(left _ child ( x )), shortest( right_ child ( y ))} otherwise

Leftist Trees

Two binary trees
A
B
D
G
C
E
H
F
J
I
Leftist Trees

Extended binary trees
A
B
D
G
C
E
H
F
J
I
Leftist Trees

The number inside each internal node x
is shortest(x).
2
2
1
2
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
1
2
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
shortest(x)
A binary tree whose shortest
height is shortest(x) means for
every path from root to leaf has at
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
11
13
50
80
Leftist Trees
Combination of leftist trees
Combination of leftist trees

The combination step (min-leftist trees)
1.
2.
3.
Choose minimum root of the two trees, A
and B.
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.
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
7
11
13
5
50
9
80
8
12
20
10
18
15
Combination of leftist trees
2
7
11
13
50
5
80
8
9
12
20
10
18
15
Combination of leftist trees
2
7
5
11
9
13
50
12
8
80
20
10
18
15
Combination of leftist trees
2
7
5
11
9
13
8
12
20
10
18
15
50
80
Combination of leftist trees
2
2
1 7
5 2
1 9
1 11
1 13
8 2
10 1
2 12
1 20
1 18
15 1
50 1
80 1
Combination of leftist trees
2
2
1 7
5 2
8 2
10 1
15 1
1 9
50 1
80 1
2 12
1 20
1 18
1 11
1 13
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

B0
A binomial tree Bk has 2k nodes with height
be k.
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.
B
4
Depth 1:4 nodes.
Depth 2:6 nodes.
Depth 3:4 nodes.
B3
B2
B1 B0
Binomial Heaps


Collection of min (max) trees.
The min trees should be Binomial trees.
1
3
8
5
10
6
12
4
15
20
30
7
9
16
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
10
3
5
1
4
12
7
16
a
pointer
Siblings
6
15
30
9
parent
child
20
Binomial Heaps
Combination of binomial heaps
Combination of binomial heaps
a
40
8
10
3
5
6
1
4
12
15
20
30
7
9
16
Combination of binomial heaps
a
40
8
10
3
5
6
1
4
12
15
20
30
7
9
16
Combination of binomial heaps

Pairwise combine
8
10
a
3
5
6
1
4
12
15
20
30
7
9
16
20
40
Combination of binomial heaps

Pairwise combine
8
10
a
3
5
6
1
4
12
15
20
30
7
9
16
20
40
Combination of binomial heaps

Pairwise combine
8
20
10
a
3
5
1
4
12
7
40
6
15
20
30
9
16
Combination of binomial heaps

Pairwise combine
8
20
10
a
3
5
1
4
12
7
40
6
15
20
30
9
16
Combination of binomial heaps

Pairwise combine
a
3
8
20
10
5
6
1
4
12
15
40
20
30
7
9
16
Combination of binomial heaps

Pairwise combine
a
3
20
8
5
10
6
1
4
12
15
40
20
30
7
9
16
Combination of binomial heaps

Pairwise combine
a
3
20
40
8
5
10
6
1
4
12
15
20
30
7
9
16
Combination of binomial heaps

Pairwise combine
a
1
3
20
40
8
5
10
6
4
12
15
20
30
7
9
16
Combination of binomial heaps
a
1
3
20
40
8
5
10
6
4
12
15
20
30
7
9
16
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
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)
O(log n)
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
Deletion and Decrease key
Deletion and Decrease key

Deletion
8
10
3
5
6
1
4
12
15
20
30
7
9
16
Deletion and Decrease key

Deletion
8
10
3
5
6
1
4
7
15
20
30
9
16
Deletion and Decrease key

Deletion
8
10
3
5
6
1
4
7
15
20
30
9
16
Deletion and Decrease key

Decrease key
8
10
3
5
6
1
4
12
11 15
20
30
7
9
16
Deletion and Decrease key

Decrease key
8
10
3
5
6
1
4
12
11
20
30
7
9
16
Deletion and Decrease key

Decrease key
8
10
3
5
6
1
4
12
11
20
30
7
9
16
Time complexity
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)