There is no commentSelect some text and then click Comment, or simply add a comment to this page from below to start a discussion.
CH 9 Heap
Min-Max heap
Definition
A mix-max heap is a complete binary tree such that if it is not empty, each element has a field called key.
Alternating levels of this tree are min levels and max levels, respectively.
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. We call this node a min (max) node.
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 (i.e. i.key <= j.key)
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
Shortest(x)
Let x be a node in an extended binary tree. Let LeftChild(x) and RightChild(x), respectively, denote the left and right children of the internal node x
Define shortest(x) to be the length of a shortest path from x to an external node. It is easy to see that shortest(x) satisfies the following recurrence:
A leftist tree is a binary tree such that if it is not empty, then shortest(LeftChild(x)) ≥ shortest(RightChild(x)) for every internal node x
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).
Min (Max) Leftist tree
Definition: 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
Operations
Like any other tree structures, the popular operations on the min (max) leftist trees are insert, delete, and combine
The insert and delete-min operations can both be done by using the combine operation
To insert an element x into a min leftist tree, we first create a min leftist tree that contains the single element x. Then we combine the two min leftist trees
To delete the min element from a nonempty min leftist tree, we combine the min leftist trees root->LeftChild and root->RightChild and delete the node root.
To combine two leftist trees:
a new binary tree containing all elements in both trees is obtained by following the rightmost paths in one or both trees
Next, the left and right subtrees of nodes are interchanged as necessary to convert this binary tree into a leftist tree
The complexity of combining two leftist trees is O(log n)
Binomial tree Bk is an ordered tree defined recursively.
Bk is degree k binomial tree.
Bk , k > 0, is:
Lemma (Properties of binomial trees)
For each binomial tree Bk
There are 2k nodes
The height of the tree is k
There are exactly k! / i!(k-i)! nodes at depth i
The root has degree k, which is greater than that of any other node Theorem: The height of Bk , the binomial tree of order k, is k Theorem: The number of nodes at level l in , the binomial tree of order k, where 0≦ l ≦ k , is given by the binomial coefficient C k取l
Number of Nodes in Bk
Nk = number of nodes in Bk.
N0 = 1
Bk , k > 0, is:
Nk = N0 + N1 + N2 + …+ 1 = 2k.
Equivalent Definition
Bk , k > 0, is two Bk-1s.
One of these is a subtree of the other. N0 = 1 Nk = 2N(k-1) = 2^k. If we start with zero elements and perform operations as described, then all trees in all binomial heaps are binomial trees. So, MaxDegree = O(log n).
insert and combine is O(1), and that of each delete-min operation is O(log n)
What’s BH&FH – Definition( Important)
A binomial heap is a collection of binomial trees.
The binomial tree Bk is an ordered tree defined recursively. The binomial tree B0 consists of a single node. The binomial tree Bk consists of two binomial trees Bk-1 that are linked together: the root of one is the leftmost child of the root of the other.
Like a binomial heap, a Fibonacci heap is a collection of min-heap-ordered trees. The trees in a Fibonacci heap are not constrained to be binomial trees.
Binomial heap is a special case Fibonacci heap
Binomial heap node: data, link, degree and child
Fibonacci heap node: data, parent, childcut, child, link and degree.
考題
99年
Fibonacci Heaps(F-heaps)
Definition
A Fibonacci heap is a data structure that supports the three binomial heap operations: insert, delete-min (or delete-max),and combine, as well as the following additional operations:
(1) decrease key: Decrease the key of a specified node by a given positive amount
(2) delete: Delete the element in a specified node
this two operations are followed by cascading cut
Two varieties of Fibonacci heaps:
Min Fibonacci heap: is a collection of min trees
Max Fibonacci heap: is a collection of max trees
B-heaps are a special case of F-heaps.
Operations
Delete
Follow the below steps to delete a node b from an F-heap:
If min = b, then do a delete-min; otherwise do Steps 2, 3, and 4 below
Delete b from its doubly linked list
Combine the doubly linked list of b’s children with the doubly linked list pointed at by min into a single doubly linked list. Trees of equal degree are not joined as in a delete-min operation
Dispose of node b
Example: delete 12
Decrease Key
To decrease the key in node b, do the following:
(1) Reduce the key in b
(2) If b is not a min tree root and its key is smaller than that in its parent, then delete b from its doubly linked list and insert it into the doubly linked list of min tree roots
(3) Change min to point to b if the key in b is smaller than that in min.
Cascading cut
whenever a delete or decrease-key operation deletes a node q that is not a min tree root from its doubly linked list, then the cascading cut step is invoked
If two children of x were deleted, then x must be cut and moved to the ring of roots.(Important)
Extract Min
考題
99 Below figure is a Fabonacci heap on 10 keys, Note that it consists of two trees. If we apply the following sequence of 4???(老師算術不太好) operations:
delete-minimum
insert 15
delete 44
decrease 72 to 13
delete 63
What will be the resulting Fibonocci heap for each operation?
[誰會啊???有人能確認答案嗎?]
ch10
Optimal binary search tree
Path length
External(E):Sum over all external nodes of the lengths of the paths from the root to those nodes.
Internal(I): Sum over all internal nodes of the lengths of the paths from the root to those nodes.
Binary trees with maximum E also have maximum I. E=I+2n
For all binary trees with n internal nodes
Cost
Example
CODE
考題
M way search tree
節點的型態是n, A0, (K1, A1), (K2, A2),…,(Kn, An) 其中Ai是子樹的指標 0 ≤ i ≤ n < m; n為節點上的鍵值數,Ki是鍵值1 ≤ i ≤ n < m。