1 / 35

單元 7: Heap

單元 7: Heap. (Chapter 9). 定義 for priority queues Leftist trees Binomial heap Min-Max heap. 1. 定義. A double-ended priority queue is a data structure that supports the following operation:. Return an element with minimum priority. Return an element with maximum priority.

imelda
Download Presentation

單元 7: Heap

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 單元7: Heap (Chapter 9) • 定義 for priority queues • Leftist trees • Binomial heap • Min-Max heap

  2. 1.定義 A double-ended priority queue is a data structure that supports the following operation: • 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.

  3. 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 C D E

  4. 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 C 1 1 D E 0 0 0 0 0 0 • shortest(x) = 1+ min {shortest(leftChild(x)), shortest(rightChild(x))}

  5. 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 A 2 A 2 1 B C 1 2 B C 1 1 1 1 D E D E

  6. 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).

  7. 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.

  8. 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

  9. 2.Leftist Trees meld 2 2 2 5 1 1 1 7 50 1 2 8 9 1 2 1 1 12 11 80 10 1 50 1 1 1 1 1 20 18 15 13 80

  10. 2.Leftist Trees meld 2 2 2 5 1 7 1 2 8 9 1 1 11 2 1 12 1 10 50 1 13 1 1 1 1 20 18 15 80

  11. 2.Leftist Trees meld 2 2 1 2 7 5 1 1 2 8 9 11 1 2 1 12 1 10 50 13 1 1 1 1 1 20 18 15 80 The complexity O(log n)

  12. 2.Leftist Trees A min leftist tree ( operation) : insert(7) 2 7 3 2 2 4 3 2 1 1 1 1 2 7 6 8 5 1 1 1 9 8 6

  13. 2.Leftist Trees A min leftist tree ( operation) : DELETE(2) 2 2 2 4 3 2 1 1 1 1 2 7 6 8 5 1 1 1 9 8 6

  14. 2.Leftist Trees A min leftist tree ( operation) : DELETE(2) 2 3 2 1 4 5 1 1 2 1 6 7 8 9 1 1 1 8 8 6

  15. 2.Leftist Trees A min leftist tree ( operation) : DELETE(2) 2 3 2 1 5 4 1 2 1 6 7 9 1 1 1 8 8 6

  16. 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 3 1 B C 2 1 B C 1 1 1 1 D E D E 0 0 0 0 0 0 0 0 0 0 0 0 • w(x) = 1+ w(leftChild(x))+w(rightChild(x))

  17. 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.

  18. 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. B3 B0 B1 B2

  19. 3. Binomial heap operation:Insert, Delete-min, Meld Insert 6, 5, 3, 4, 7, 9, 3 7 5 4 6 9 Delete-min 1 7 16 12 15 30 9 20

  20. 3. Binomial heap Analysis Of Binomial Heaps

  21. 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.

  22. 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.

  23. 4. Min-Max heap The min-max heap stores in a one-dimension array h min 7 max 40 70 min 30 9 10 15 max 20 45 50 7 7 8 12 30 Insert 5

  24. 4. Min-Max heap Insert x i Check parent i 為max 或min (1) min x 7 7 8 If x<i then x 和i 互換 對x執行verifymin 以找正確位置 (2) max If x>i then x 和i 互換 對x執行verifymax 以找正確位置

  25. 4. Min-Max heap Insert 5 min 7 max 40 70 verifymin min 30 9 10 15 max 20 5 45 50 7 7 8 12 30

  26. 4. Min-Max heap Insert 80 min 5 max 40 70 min 30 9 7 15 verifymax max 20 45 50 7 7 8 12 10 30 80

  27. 4. Min-Max heap Delete of min element • 將root (最小值)拿掉 • 準備delete array最後元素x,再重新insert 最後元素x • 尋找第2小元素k • If x < k then root 設為x • else if k 為root 之child k x max x max k 7 7 8 7 7 8

  28. 4. Min-Max heap Delete of min element (6) else if k 為root 之grandchild k k x p x p max max max 7 7 7 7 7 7 min min min p x 7 7 8 7 7 8 k 7 7 8 check check p > x p < x

  29. 4. Min-Max heap Delete of min element 7 x min 7 max 99 70 k min 30 9 10 15 max 20 45 50 7 7 8 90 30

  30. 4. Min-Max heap Deaps • A deap is a double-ended heap that supports the double-ended priority operations of insert, delete-min, and delete-max. • Similar to min-max heap but deap is faster on these operations by a constant factor, and the algorithms are simpler.

  31. 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.

  32. 4. Min-Max heap Deaps 小 大 45 5 10 8 25 40 30 15 19 7 7 8 20 9 Max heap Min heap 5 < 45 ; 10 <25 ; 8 <40; 15 < 20; 19 < 25; 9 < 40; 30 < 40

  33. 4 4. Min-Max heap Deaps-Insert 4 45 5 10 8 25 40 30 15 19 7 7 8 20 9 比較 Min heap Max heap

  34. 4 60 4. Min-Max heap Deaps-Insert 60 45 5 10 8 25 40 30 15 19 7 7 8 20 9 比較 Min heap Max heap

  35. 20 4. Min-Max heap Deaps-delete 5 45 5 10 8 25 19 10 15 18 7 7 8 9 Max heap Last element Min heap 比較

More Related