1 單元 7: heap (chapter 9) 1. 定義 for priority queues 2.leftist trees 3.binomial heap 4.min-max...

35
1 單單 7: Heap (Chapter 9) 1.單單 for priority queues 2.Leftist trees 3.Binomial heap 4.Min-Max heap

Upload: nigel-hutchinson

Post on 02-Jan-2016

248 views

Category:

Documents


0 download

TRANSCRIPT

  • 7: Heap(Chapter 9) for priority queuesLeftist treesBinomial heapMin-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. Insert an element with arbitrary priority.Delete an element with minimum priority.Delete an element with maximum priority.

  • 2.Leftist TreesLeftist 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.ABCDEAn extended binary tree

  • 2.Leftist TreesABCDEAn extended binary treeDefine shortest(x) to be the length of a shortest path from x to an external node.11212shortest(x) = 1+ min {shortest(leftChild(x)), shortest(rightChild(x))}000000

  • 2.Leftist TreesA 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.leftist tree :ABCDE11212ABCDE11122

  • 2.Leftist TreesLemma : 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).

  • 2.Leftist TreesA min leftist tree (max leftist tree)-is a leftist tree in which the key value in eachnode is no larger (smaller) than the keyvalues in its children (if any). In other words,a min (max) leftist tree is a leftist tree that isalso a min (max) tree.

  • 2.Leftist TreesA min leftist tree ( operation) :Insert; delete; combine (meld)1. root , root, child 2. child tree combine, right child3. leftist tree

    meld

  • 2.Leftist Treesmeld27501180111129122018112158210151113115080112

  • 2.Leftist Treesmeld27111129122018112158210151113115080112

  • 2.Leftist Treesmeld27111129122018112158210151113115080112The complexity O(log n)

  • 2.Leftist TreesA min leftist tree ( operation) :insert(7)86968543277212112111123

  • 2.Leftist TreesA min leftist tree ( operation) :DELETE(2)869685432721211211112

  • 2.Leftist TreesA min leftist tree ( operation) :DELETE(2)86968543721211111281

  • 2.Leftist TreesA min leftist tree ( operation) :DELETE(2)869685437212111112

  • 2.Leftist TreesABCDEWeight-Biased Leftist treeDefine w(x) to be the number of internal nodes in the subtree with root x11212w(x) = 1+ w(leftChild(x))+w(rightChild(x))000000ABCDE11314000000

  • 3. Binomial heap A binomial heap is a data structure that supports the samefunctions as those supported by leftist trees. Unlike leftist trees, where an individual operation can beperformed in O(log n) time, it is possible that certain individualoperations 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 anindividual operation is either O(1) or O(log n) depending on the type of operations.

  • 3. Binomial heapThe binomial tree Bk, of degree k is a tree such thatif 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.:B0B1B2B3

  • 3. Binomial heapoperation:Insert, Delete-min, MeldInsert 6, 5, 3, 4, 7, 9,6534793012116791520Delete-min

  • Analysis Of Binomial Heaps3. Binomial heap

    Leftist trees

    Binomial heaps

    Actual

    Amortized

    Insert

    O(log n)

    O(1)

    O(1)

    Delete min (or max)

    O(log n)

    O(n)

    O(log n)

    Meld

    O(log n)

    O(1)

    O(1)

  • 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 smallest4. Min-Max heapA Min-Max Heap supports all of the above operations.

  • 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 hasthe 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.4. Min-Max heap

  • 8307101545504073072097012minmaxminmaxThe min-max heap stores in a one-dimension array hInsert 54. Min-Max heap

  • Check parent i max minInsert x877xi(1) minIf xi then x i xverifymax 4. Min-Max heap

  • 8307101545504073072097012minmaxminmaxInsert 55verifymin4. Min-Max heap

  • 830771545504073052097012minmaxminmaxInsert 8010verifymax804. Min-Max heap

  • Delete of min elementroot ()delete arrayx,insert x2kIf x < k then root xelse if k root child 877kx877xkmaxmax4. Min-Max heap

  • Delete of min element(6) else if k root grandchild 77xmax877kmin77kmax877xminppp > x77kmax877pminxp < xcheckcheck4. Min-Max heap

  • 8307101545509973072097090minmaxminmaxDelete of min element 7kx4. Min-Max heap

  • Deaps A deap is a double-ended heap that supportsthe 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, andthe algorithms are simpler.4. Min-Max heap

  • 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 thatcorresponds to the parent of i. The key in node i is less than or equal to that of j.Def:4. Min-Max heap

  • 8107254015194579308520DeapsMin heapMax heap5 < 45 ; 10
  • 8107254015194579308520Deaps-Insert 4Min heapMax heap4. Min-Max heap

  • 8107254015194579308520Deaps-Insert 60Min heapMax heap4. Min-Max heap

  • 81072519151845791085Deaps-delete 5Min heapMax heapLast element4. Min-Max heap