浙江大学高级数据结构课件DS03_Ch5a_A
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
X in the heap, the null path length of the left child is at least as large as that of the right child. 1 1 0 0 0 0 0 0 1 1 0 1 0 The tree is biased to get deep toward the left.
Trouble makers: Insert and Merge
Note: Insertion is merely a special case of merging.
3/10
§6 Leftist Heaps
Declaration:
struct TreeNode { ElementType PriorityQueue PriorityQueue int };
3 10 21 23 14 26 8 17 18 33 12 24 37 6 7 18 8 3
Not really a special 6 10 case, but a natural 7stop in the recursions. 12 21 14
37 18 17 26 33 24 23
H1
H2
p.181 H2
p.183 5.22
26
DeleteMin: BuildHeap in O(N)
7 8 17 18
Step 1: Delete the root Step 2: Merge the two
subtrees
Tp = O(log N)
§7 Skew Heaps
-- a simple version of the leftist heaps
Target : Any M consecutive operations take at
most O(M log N) time.
Merge: Always swap the left and right children except that the
largest of all the nodes on the right paths does not have its children swapped. No Npl.
5.16 18 24 8 37 33 17 18 Step 1: Sort Merge two given leftist heaps the right paths without
H1
changing their left children
3 10 21 23 14 18 33 26 6/10 12 24 37 6
18
This is NOT always the case.
7/10
§7 Skew Heaps
【Example】Insert 15 】
3 6 7 8 18 17 26 37 18 33 12 24 21 23 10 14 15 14 23 15 10 21 8 18 17 26 3 6 7 24 37 33 18 8 18 17 26 7 37 18 33 12 24 21 23 10 14 7 37 18 33 3 6 12 24
the length of the shortest path from X to a node without two children. Define Npl(NULL) = –1.
Note: Npl(X) = min { Npl(C) + 1 for all C as children of X } 【Definition】The leftist heap property is that for every node 】
Merge (recursive version):
3 10 21 23 14 26 8 17 18 33 12 24 37 6 7
37 18
Step 3: H2
Swap(H1->Right, H1->Left ) if necessary
H1
4/10
§6 Leftist Heaps
PriorityQueue Merge ( PriorityQueue H1, PriorityQueue H2 ) { if ( H1 == NULL ) return H2; if ( H2 == NULL ) return H1; if ( H1->Element < H2->Element ) return Merge1( H1, H2 ); else return Merge1( H2, H1 ); } static PriorityQueue Merge1( PriorityQueue H1, PriorityQueue H2 ) { if ( H1->Left == NULL ) /* single node */ H1->Left = H2; /* H1->Right is already NULL and H1->Npl is already 0 */ else { H1->Right = Merge( H1->Right, H2 ); /* Step 1 & 2 */ if ( H1->Left->Npl < H1->Right->Npl ) SwapChildren( H1 ); /* Step 3 */ H1->Npl = H1->Right->Npl + 1; } /* end else */ return H1; What if Npl is NOT } updated? T = O(log N)
2/10
§6 Leftist Heaps
【Theorem】A leftist tree with r nodes on the right path must 】
have at least 2r – 1 nodes. Proof: By induction on p. 162.
Discussion 6: How long is the right path of a leftist tree of N nodes? What does this conclusion mean to us?
10/10
Order Property – the same Structure Property – binary tree, but unbalanced
1/10
§6 Leftist Heaps
【Definition】The null path length, Npl(X), of any node X is 】
Home work:
p.183 5.24 Insertions
9/10
Research Project 3
Heap Merging (30)
This project requires you to implement Merge operation on ordinary heaps, leftist heaps, and skew heaps. You are to analyze and compare the performances of a sequence of merges on these heap structures. Detailed requirements can be downloaded from http://www.cs.zju.edu.cn/ds/
p
5/10
§6 Leftist Heaps
Merge (iterative version):
3 10 21 23 14 26 8 17 18 33 12 24 37 6 7
Step 2: Swap children if necessary
3 18 6 7 21 23 10 14
Байду номын сангаас
Home work: 12
Step 1:
Merge( H1->Right, H2 )
12
6 7 24 33 8 17 26 18 37
Element; Left; Right; Npl;
18
Step 2:
Attach( H2, H1->Right )
3 10 21 14 23 18 18 33 12 24 8 17 26 6 7
CHAPTER 5
Graph Algorithms
§6 Leftist Heaps Target : Speed up merging in O(N).
Heap: Structure Property + Order Property Discussion 5: How fast can we merge two heaps if we simply use the original heap structure? Leftist Heap:
Merge (iterative version):
3 10 21 23 14 26 8 17 18 12 6
H1
8/10
H2
§7 Skew Heaps
Note:
Skew heaps have the advantage that no extra space is required to maintain path lengths and no tests are required to determine when to swap children. It is an open problem to determine precisely the expected right path length of both leftist and skew heaps.
Trouble makers: Insert and Merge
Note: Insertion is merely a special case of merging.
3/10
§6 Leftist Heaps
Declaration:
struct TreeNode { ElementType PriorityQueue PriorityQueue int };
3 10 21 23 14 26 8 17 18 33 12 24 37 6 7 18 8 3
Not really a special 6 10 case, but a natural 7stop in the recursions. 12 21 14
37 18 17 26 33 24 23
H1
H2
p.181 H2
p.183 5.22
26
DeleteMin: BuildHeap in O(N)
7 8 17 18
Step 1: Delete the root Step 2: Merge the two
subtrees
Tp = O(log N)
§7 Skew Heaps
-- a simple version of the leftist heaps
Target : Any M consecutive operations take at
most O(M log N) time.
Merge: Always swap the left and right children except that the
largest of all the nodes on the right paths does not have its children swapped. No Npl.
5.16 18 24 8 37 33 17 18 Step 1: Sort Merge two given leftist heaps the right paths without
H1
changing their left children
3 10 21 23 14 18 33 26 6/10 12 24 37 6
18
This is NOT always the case.
7/10
§7 Skew Heaps
【Example】Insert 15 】
3 6 7 8 18 17 26 37 18 33 12 24 21 23 10 14 15 14 23 15 10 21 8 18 17 26 3 6 7 24 37 33 18 8 18 17 26 7 37 18 33 12 24 21 23 10 14 7 37 18 33 3 6 12 24
the length of the shortest path from X to a node without two children. Define Npl(NULL) = –1.
Note: Npl(X) = min { Npl(C) + 1 for all C as children of X } 【Definition】The leftist heap property is that for every node 】
Merge (recursive version):
3 10 21 23 14 26 8 17 18 33 12 24 37 6 7
37 18
Step 3: H2
Swap(H1->Right, H1->Left ) if necessary
H1
4/10
§6 Leftist Heaps
PriorityQueue Merge ( PriorityQueue H1, PriorityQueue H2 ) { if ( H1 == NULL ) return H2; if ( H2 == NULL ) return H1; if ( H1->Element < H2->Element ) return Merge1( H1, H2 ); else return Merge1( H2, H1 ); } static PriorityQueue Merge1( PriorityQueue H1, PriorityQueue H2 ) { if ( H1->Left == NULL ) /* single node */ H1->Left = H2; /* H1->Right is already NULL and H1->Npl is already 0 */ else { H1->Right = Merge( H1->Right, H2 ); /* Step 1 & 2 */ if ( H1->Left->Npl < H1->Right->Npl ) SwapChildren( H1 ); /* Step 3 */ H1->Npl = H1->Right->Npl + 1; } /* end else */ return H1; What if Npl is NOT } updated? T = O(log N)
2/10
§6 Leftist Heaps
【Theorem】A leftist tree with r nodes on the right path must 】
have at least 2r – 1 nodes. Proof: By induction on p. 162.
Discussion 6: How long is the right path of a leftist tree of N nodes? What does this conclusion mean to us?
10/10
Order Property – the same Structure Property – binary tree, but unbalanced
1/10
§6 Leftist Heaps
【Definition】The null path length, Npl(X), of any node X is 】
Home work:
p.183 5.24 Insertions
9/10
Research Project 3
Heap Merging (30)
This project requires you to implement Merge operation on ordinary heaps, leftist heaps, and skew heaps. You are to analyze and compare the performances of a sequence of merges on these heap structures. Detailed requirements can be downloaded from http://www.cs.zju.edu.cn/ds/
p
5/10
§6 Leftist Heaps
Merge (iterative version):
3 10 21 23 14 26 8 17 18 33 12 24 37 6 7
Step 2: Swap children if necessary
3 18 6 7 21 23 10 14
Байду номын сангаас
Home work: 12
Step 1:
Merge( H1->Right, H2 )
12
6 7 24 33 8 17 26 18 37
Element; Left; Right; Npl;
18
Step 2:
Attach( H2, H1->Right )
3 10 21 14 23 18 18 33 12 24 8 17 26 6 7
CHAPTER 5
Graph Algorithms
§6 Leftist Heaps Target : Speed up merging in O(N).
Heap: Structure Property + Order Property Discussion 5: How fast can we merge two heaps if we simply use the original heap structure? Leftist Heap:
Merge (iterative version):
3 10 21 23 14 26 8 17 18 12 6
H1
8/10
H2
§7 Skew Heaps
Note:
Skew heaps have the advantage that no extra space is required to maintain path lengths and no tests are required to determine when to swap children. It is an open problem to determine precisely the expected right path length of both leftist and skew heaps.