当前位置:
文档之家› 数据结构、算法与应用 第10章
数据结构、算法与应用 第10章
46
More Tournament Tree Applications
• k-way merging of runs during an external merge sort • Truck loading
47
Truck Loading
n packages to be loaded into trucks each package has a weight each truck has a capacity of c tons minimize number of trucks
1 3
6
3
2
4 4 3 6
8 8 1
5 5 7
7 3 2
6 6 9
9 4 5 2 5 8
38
Min Loser Tree For 16 Players
1 3
2
6
3
4
2
4 4 3 6
8 8 1
5 5 7
7 3 2
6 6 9
9 4 5
5 2 5
8 8
39
Min Loser Tree For 16 Players
2 3
2
3
3
2
2
3 4 1 3 2 6
6 8 1
5 5 7
3 3 2
6 6 9
4 4 5
2 2 5
5 8
Sorted array.
17
Sort 16 Numbers
2 3
2
3
3
4
2
3 4 1 3 2 6
6 8 1
5 5 7
3 3 2
6 6 9
4 4 5
2 2 5
5 8
Sorted array.
18
44
1 2 2 3
Winner
3
2
6
3 5
4
5
4 4 3 6
8 8 9 1
5 9 5 7
7 3 2
6 6 9
9 4 5
5 2 5
8 8
Replace winner with 9 and replay matches. 45
Complexity Of Replay
• One match at each level that has a match node. • O(log n) • More precisely Theta(log n).
6 8 1
5 5 7
3 3 2
6 6 9
4 4 5
5 2 5
5 8
Sorted array.
24
Sort 16 Numbers
3 3
4
3
3
4
5
3 4 1 3 2 2 6
6 8 1
5 5 7
3 3 2
6 6 9
4 4 5
5 2 5
5 8
Sorted array.
25
Sort 16 Numbers
3 3
• Get winner
O(1) time
• Remove/replace winner and replay
O(log n) time more precisely Theta(log n)
数据结构算法与应用
28
10.3 The CLASS WinnerTree
The left-most internal node at the lowest level is numbered 2s where s = ⎣log 2 ( n − 1) ⎦ . Therefore the number of internal nodes at the lowest level is n-2s, and the number LowExt of external nodes at the lowest level is twice this number.
Sorted array.
20
Sort 16 Numbers
2 3
2
3
3
4
2
3 4 1 3 2 2 6
6 8 1
5 5 7
3 3 2
6 6 9
4 4 5
2 2 5
5 8
Sorted array.
21
Sort 16 Numbers
2 3
2
3
3
4
2
3 4 1 3 2 2 6
6 8 1
5 5 7
3 3 2
Chapter 10 Tournament Trees
信息科学与技术学院 黄方军
huangfj@
东校区实验中心B502
数据结构算法与应用
1
10.1 Introduction
• • Winner trees. Loser Trees.
数据结构算法与应用
2
Winner Trees
7
Applications
Sorting. Put elements to be sorted into a winner tree. Repeatedly extract the winner and replace by a large value.
8
Sort 16 Numbers
1 1
2
3
3 3 2
2 6 9
4 4 5
2 2 5
5 8
Sorted array.
11
Sort 16 Numbers
1 1
2
3
1
2
2
3 4 1 3 6
6 8 1
5 5 7
3 3 2
2 6 9
4 4 5
2 2 5
5 8
Sorted array.
12
Sort 16 Numbers
1 1
2
3
3
2
2
3 4 1 3 6
4
3
3
4
5
3 4 1 3 2 2 6
6 8 3 1
5 5 7
3 3 2
6 6 9
4 4 5
5 2 5
5 8
Sorted array.
26
Time To Sort
• Initialize winner tree.
O(n) time
• Remove winner and replay.
O(log n) time
35
Min Loser Tree For 16 Players
3
4 4 3 6
8 8 1 5 7 3 2 6 9 4 5 2 5 8
36
Min Loser Tree For 16 Players
3
6
1
4 4 3 6
8 8 1
5 5 7
7 3 2 6 9 4 5 2 5 8
37
Min Loser Tree For 16 Players
Sort 16 Numbers
2 3
2
3
3
4
2
3 4 1 3 2 6
6 8 1
5 5 7
3 3 2
6 6 9
4 4 5
2 2 5
5 8
Sorted array.
19
Sort 16 Numbers
2 3
2
3
3
42ຫໍສະໝຸດ 3 4 1 3 2 66 8 1
5 5 7
3 3 2
6 6 9
4 4 5
2 2 5
5 8
2 3
2
6
3
4
5
4 4 3 6
8 8 1
5 5 7
7 3 2
6 6 9
9 4 5
5 2 5
8 8
42
1 2
Winner
3
2
6
3
4
5
4 4 3 6
8 8 1
5 5 7
7 3 2
6 6 9
9 4 5
5 2 5
8 8
43
Complexity Of Loser Tree Initialize
• • • • One match at each match node. One store of a left child winner. Total time is O(n). More precisely Theta(n).
1 1
2
3
1
2
2
3 4 3 6
6 8 1
1 5 7
3 3 2
2 6 9
4 4 5
2 2 5
5 8
Replace winner with 6.
31
Replace Winner And Replay
1 1
2
3
1
2
2
3 4 3 6
6 8 6
1 5 7
3 3 2
2 6 9
4 4 5
2 2 5
5 8
Replay matches on path to root.
• Complete binary tree with n external nodes and n - 1 internal nodes. • External nodes represent tournament players. • Each internal node represents a match played between its two children; the winner of the match is stored at the internal node. • Root has overall winner.