單元7: Heap 1. 2. 3. 4. (Chapter 9) 定義 for priority queues Leftist trees Binomial heap Min-Max heap 1 1.定義 A double-ended priority queue is a data structure that supports the following operation: 1. 2. 3. 4. 5. Return an element with minimum priority. Return an element with maximum priority. Insert an element with arbitrary priority. Delete an element with minimum priority. Delete an element with maximum priority. 2 2.Leftist Trees An extended binary tree Leftist trees are defined using the concept of an extended binary tree. An extended binary tree is a binary tree in which all empty binary subtrees have been replaced by a square node. The square nodes in an extended binary tree are called external nodes. The original nodes of the binary tree are called internal nodes. A B D C E 3 2.Leftist Trees An extended binary tree Define shortest(x) to be the length of a shortest path from x to an external node. 2 A 2 1 B D E 1 0 0 0 0 1 C 0 0 shortest(x) = 1+ min {shortest(leftChild(x)), shortest(rightChild(x))} 4 2.Leftist Trees leftist tree 定義: A leftist tree is a binary tree such that if it is not empty, then shortest(LeftChild(x)) ≥ hortest(RightChild(x)) for every internal node x. 2 2 1 D A B 2 C E 1 1 1 A B C 2 1 D E 1 5 2.Leftist Trees Lemma : Let x be the root of a leftist tree that has n (internal) nodes (a) n ≥ 2shortest(x) – 1 (b) The rightmost root to external node path is the shortest root to external node path. Its length is shortest(x) ≤ log2(n+1). 6 2.Leftist Trees 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). In other words, a min (max) leftist tree is a leftist tree that is also a min (max) tree. 7 2.Leftist Trees A min leftist tree ( operation) : Insert; delete; combine (meld) meld 1. 找兩者之root 相比, 較小者為root, 且其左child 留下。 2. 其右child 與另一較大之tree 以相同步驟combine, 結果作為 right child。 3. 調整為符合leftist tree 8 2.Leftist Trees meld 2 1 1 1 13 11 2 2 50 1 7 80 1 2 12 1 1 20 5 12 9 8 1 10 1 18 1 15 50 1 80 1 9 2.Leftist Trees meld 2 1 1 1 2 2 12 8 1 15 9 1 11 1 10 13 5 7 1 50 1 80 1 2 12 20 1 18 10 2.Leftist Trees meld 2 2 12 1 15 1 5 8 1 10 2 1 1 50 1 80 1 2 12 20 1 9 1 18 7 11 13 1 The complexity O(log n) 11 2.Leftist Trees A min leftist tree ( operation) : insert(7) 32 2 7 2 4 1 2 6 1 6 3 1 1 8 1 8 21 5 7 1 9 12 2.Leftist Trees A min leftist tree ( operation) : DELETE(2) 2 2 2 4 1 2 6 1 6 3 1 1 8 1 8 21 5 7 1 9 13 2.Leftist Trees A min leftist tree ( operation) : DELETE(2) 2 3 2 1 4 5 1 6 9 8 7 8 1 1 1 1 2 6 1 8 14 2.Leftist Trees A min leftist tree ( operation) : DELETE(2) 2 3 2 1 5 4 1 2 6 7 1 1 8 6 1 9 1 8 15 2.Leftist Trees Weight-Biased Leftist tree Define w(x) to be the number of internal nodes in the subtree with root x 4 A 2 A 2 1 B D 1 1 0 0 0 0 1 C E 0 3 B D E 1 0 0 0 0 0 1 C 0 0 w(x) = 1+ w(leftChild(x))+w(rightChild(x)) 16 3. Binomial heap • A binomial heap is a data structure that supports the same functions as those supported by leftist trees. • Unlike leftist trees, where an individual operation can be performed in O(log n) time, it is possible that certain individual operations performed on a binomial heap may take O(n) time. • By amortizing part of the cost of expensive operations over the inexpensive ones, then the amortized complexity of an individual operation is either O(1) or O(log n) depending on the type of operations. 17 3. Binomial heap 定義: The binomial tree Bk, of degree k is a tree such that if k = 0, then the tree has exactly one node, and if k > 0, then the tree consists of a root whose degree is k and whose subtrees are B0, B1, …, Bk-1. B0 B1 B2 B3 18 3. Binomial heap operation:Insert, Delete-min, Meld Insert 6, 5, 3, 4, 7, 9, 3 5 4 6 Delete-min 7 9 1 7 12 15 20 30 16 9 19 3. Binomial heap Analysis Of Binomial Heaps 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) 20 4. Min-Max heap 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 A Min-Max Heap supports all of the above operations. 21 4. Min-Max heap 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. 22 4. Min-Max heap The min-max heap stores in a one-dimension array h min 7 40 70 30 45 Insert 5 9 50 7 30 10 20 8 12 max 15 min max 23 4. Min-Max heap Insert x i Check parent i 為max 或min (1) min 7 8x If x<i then x 和i 互換 對x執行verifymin 以找正確位置 (2) max If x>i then x 和i 互換 對x執行verifymax 以找正確位置 24 4. Min-Max heap Insert 5 min 7 70 verifymin 30 45 40 9 50 7 30 15 min 10 20 8 12 max 5 max 25 4. Min-Max heap Insert 80 min 5 30 45 9 50 max 40 70 7 30 7 20 8 12 verifymax 10 80 15 min max 26 4. Min-Max heap Delete of min element (1) (2) (3) (4) (5) 將root (最小值)拿掉 準備delete array最後元素x,再重新insert 最後元素x 尋找第2小元素k If x < k then root 設為x else if k 為root 之child k x 7 8k max 7 8x max 27 4. Min-Max heap Delete of min element (6) else if k 為root 之grandchild k k x p 7 7 max 8k min p 7 7 p>x max 8x x 7 min check 7 p<x max 8p min check 28 4. Min-Max heap Delete of min element 7 7 x min 99 70 30 45 9 50 7 30 k 20 8 10 90 max 15 min max 29 4. Min-Max heap Deaps • A deap is a double-ended heap that supports the double-ended priority operations of insert, deletemin, and delete-max. • Similar to min-max heap but deap is faster on these operations by a constant factor, and the algorithms are simpler. 30 4. Min-Max heap Def: A deap is a complete binary tree that is either empty or satisfies the following 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, 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. 31 4. Min-Max heap Deaps 小 大 45 5 10 15 8 19 97 Min heap 25 30 8 40 20 Max heap 5 < 45 ; 10 <25 ; 8 <40; 15 < 20; 19 < 25; 9 < 40; 30 < 40 32 4. Min-Max heap Deaps-Insert 4 45 5 10 15 8 19 97 Min heap 25 30 8 比較 20 40 4 Max heap 33 4. Min-Max heap Deaps-Insert 60 45 5 10 15 8 19 97 Min heap 25 30 8 20 40 4 60 比較 Max heap 34 4. Min-Max heap Deaps-delete 5 45 5 10 15 8 18 97 Min heap 25 10 8 19 20 Last element Max heap 比較 35