Download presentation
Presentation is loading. Please wait.
1
Review of Chapter 9 張啟中
2
Inheritance Hierarchy Min PQ Max PQ Min Heap Mergeable Min PQ DeapMin-Max Min-LeftistMin-SkewMinFHeap MinBHeap DEPQMax Heap Symmetric Max Data Structures
3
Double-Ended Priority Queue A double-ended priority queue is a data structure that supports the following operations: inserting an element with an arbitrary key deleting an element with the largest key deleting an element with the smallest key There are two kinds of DEPQ Min-Max Heap Deap
4
Min-Max Heaps Definition A min-max heap is a complete binary tree such that if it is not empty, Each element has a data member called key. Alternating levels of this tree are min levels and max levels, respectively. The root is on a min level. Let x be any node in a min-max heap. If x is on a min (max) level then the element in x has the minimum (maximum) key from among all elements in the subtree with root x. A node on a min (max) level is called a min (max) node.
5
Example 7 30 4550 9 3020 70 10 12 15 40 min max
6
Insertion of a Min-Max Heap Step 1 將欲新增的元素插入 Min-Max Heap 最後一個節點 Step 2 將新增節點與其父節點做比較,若父節點位於 Min (Max) Level ,且新增節點小於 ( 大於 ) 父節點,則交換二者的位置。 Step 3 若交換後新增節點的位於 Min (Max) Level ,則依序往上與 各 Min (Max) Level 的節點比較,若新增節點較小 ( 大 ) ,則二 者交換。 Step 4 重複 Step 3 ,直至不能再交換或到 root 為止。
7
Insertion of a Min-Max Heap 7 30 4550 9 3020 70 10 12 15 40 min max 5 O(logn)
8
Insertion of a Min-Max Heap 7 30 4550 9 3020 70 10 12 15 40 min max 80
9
Deletion of The Min Element Step 1 刪除 root Step 2 將最後一個節點刪除,並重新插入 Min-Max Heap 的 root
10
Deletion of The Min Element Step 3 Case 1 The root has no children. In this case x is to be inserted into the root. Case 2 The root has at least one child. Find the smallest key from the children or grandchildren of root. Assume node k has the smallest key. x is inserted node. x.key <= h[k].key Insert x to the root x.key > h[k].key and k is child of the root Interchange x and k x.key > h[k].key and k is grandchild of the root (1) h[k] is moved to the root. (2) Let p is parent of node k. If x.key > h[p].key then h[p] and x are to interchanged. Repeat Step 3 with root k.
11
Deletion of The Min Element 7 30 4550 9 3020 70 10 12 15 40 min max O(logn)
12
Deaps Definition A deap is a complete binary tree that is either empty or satisfies the following 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, then let i be any node in the left subtree. Let j be the corresponding node in the right subtree. If such a j does not exist, then let j be the node in the right subtree that corresponds to the parent of i. The key in node i is less than or equal to that of j.
13
Deap 10 1519 8 930 5 2540 45 20 Min Heap Max Heap
14
Insertion Into a Deap Step 1 將新增的元素插入 Deap 的最後面一個節點 Step 2 與新增節點相對位置的節點比較大小,若該新增節 點的位於 Max (Min) Heap ,則新增節點必須大於 相對節點,否則必須交換。 Step 3 若新增節點位於 Min (Max) Heap ,則依 Min (Max) Heap 的新增方式進行。
15
Insertion Into a Deap 10 1519 8 930 5 2540 45 20 4 j i
16
Insertion Into a Deap 10 1519 8 930 5 2540 45 20 30 j i
17
Deletion of Min Element Step 1 刪除 Min Heap 的 root Step 2 比較 Min Heap 中 root 的兩個 child ,將較小的值 移到 root ,直到 leaf 為止。 Step 3 將最後一個節點刪除,插入空出的 Leaf ,並依照 Deap 新增的方式操作
18
Deletion of Min Element 10 1519 8 930 5 2540 45 20 j i
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.