Editing Binomial heap

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 19: Line 19:
 
Since a binomial heap is only allowed to contain at most one power-of-two tree of each size, the structure of a binomial heap is uniquely determined by the binary representation of its size. For example, if a binomial heap contains 13 elements, that is, {{Binary|1101}}, then we see that <math>13 = 2^0 + 2^2 + 2^3 = 1 + 4 + 8</math>, so a binomial heap of size 13 contains three power-of-two trees, of sizes 1, 4, and 8, respectively.
 
Since a binomial heap is only allowed to contain at most one power-of-two tree of each size, the structure of a binomial heap is uniquely determined by the binary representation of its size. For example, if a binomial heap contains 13 elements, that is, {{Binary|1101}}, then we see that <math>13 = 2^0 + 2^2 + 2^3 = 1 + 4 + 8</math>, so a binomial heap of size 13 contains three power-of-two trees, of sizes 1, 4, and 8, respectively.
  
It follows that the number of power-of-two trees in a binomial heap is no more than <math>\lceil\log_2(n+1)\rceil</math>, since that is the length of the number <math>n</math> expressed as a bit string.
+
It follows that the number of power-of-two trees in a binomial heap is no more than <math>\lceil\log(n+1)\rceil</math>, since that is the length of the number <math>n</math> expressed as a bit string.
  
 
==Finding the maximum==
 
==Finding the maximum==
Line 25: Line 25:
  
 
==Merging==
 
==Merging==
Let two binomial heaps be denoted <math>A</math> and <math>B</math>, with sizes <math>n</math> and <math>m</math>, respectively. Let <math>A_k</math> denote <math>A</math>'s power-of-two tree of size <math>2^k</math>, if it has one, and <math>B_k</math> denote likewise a power-of-two tree of <math>B</math>. We want to create a new binomial heap <math>S</math> that contains all the nodes from either <math>A</math> or <math>B</math> and has size <math>m+n</math>. After this operation, <math>A</math> and <math>B</math> will no longer exist as binomial heaps.
+
Let two binomial heaps be denoted <math>A</math> and <math>B</math>, with sizes <math>n</math> and <math>m</math>, respectively. Let <math>A_k</math> denote <math>A</math>'s power-of-two tree of size <math>2^k</math>, if it has one, and <math>B_k</math> denote likewise a power-of-two tree of <math>B</math>. We want to create a new binomial tree <math>S</math> that contains all the nodes from either <math>A</math> or <math>B</math> and has size <math>m+n</math>. After this operation, <math>A</math> and <math>B</math> will no longer exist as binomial heaps.
  
 
(In theory, we could create a new binomial heap <math>S</math> that contained all the elements from both <math>A</math> and <math>B</math> ''without'' also destroying <math>A</math> and <math>B</math>. However, this would require copying over all the data from both heaps, which would take linear time (in the sum of their sizes). This would then be no more efficient than simply using the [[binary heap]]s. For this reason, we assume we usually do not want to do this.)
 
(In theory, we could create a new binomial heap <math>S</math> that contained all the elements from both <math>A</math> and <math>B</math> ''without'' also destroying <math>A</math> and <math>B</math>. However, this would require copying over all the data from both heaps, which would take linear time (in the sum of their sizes). This would then be no more efficient than simply using the [[binary heap]]s. For this reason, we assume we usually do not want to do this.)
Line 74: Line 74:
 
</pre>
 
</pre>
 
This procedure performs at most one merge (of a pair of power-of-two trees) at each iteration of the loop, which executes a logarithmic number of times; so it is <math>O(\log n)</math>, overall.
 
This procedure performs at most one merge (of a pair of power-of-two trees) at each iteration of the loop, which executes a logarithmic number of times; so it is <math>O(\log n)</math>, overall.
 
We can make the merge operation run faster if we notice that we cam perform the merge in-place, i.e. merge the smaller heap into the larger one. We can stop once we have BOTH exhausted the smaller heap AND there is no notional "carry" propagating over to the next Binomial Tree. This trick allows us to perform an amortized O(1) insert into the Binomial Heap.
 
  
 
==Insertion==
 
==Insertion==
Line 82: Line 80:
 
==Deletion==
 
==Deletion==
 
Once we have identified the minimum element in the binomial heap, which takes logarithmic time, we can remove it, as follows. Suppose the minimum element is the root of the power-of-two tree with size <math>2^k</math>. Then, we remove this tree from the heap and split the tree into two trees of size <math>2^{k-1}</math>. Whichever one does not contain the minimum element, we split again, so that now we have a tree of size <math>2^{k-1}</math> and two trees of size <math>2^{k-2}</math>. One of these two new trees again does not contain the minimum element, so we split it again. We continue splitting until finally we end up with one power-of-two tree of size <math>2^{k-1}</math>, one of size <math>2^{k-2}</math>, and so on down; one of size 4, one of size 2, and two of size 1. One of the two singletons will be the minimum element, so we simply destroy that node. The rest of the power-of-two trees constitute an entire binomial heap in and of themselves, which is then merged back into the original heap from which we removed the tree of size <math>2^k</math>. This procedure uses a logarithmic number of splits, which takes logarithmic time, followed by a merge of two binomial heaps, which takes logarithmic time; so it is <math>O(\log n)</math> overall.
 
Once we have identified the minimum element in the binomial heap, which takes logarithmic time, we can remove it, as follows. Suppose the minimum element is the root of the power-of-two tree with size <math>2^k</math>. Then, we remove this tree from the heap and split the tree into two trees of size <math>2^{k-1}</math>. Whichever one does not contain the minimum element, we split again, so that now we have a tree of size <math>2^{k-1}</math> and two trees of size <math>2^{k-2}</math>. One of these two new trees again does not contain the minimum element, so we split it again. We continue splitting until finally we end up with one power-of-two tree of size <math>2^{k-1}</math>, one of size <math>2^{k-2}</math>, and so on down; one of size 4, one of size 2, and two of size 1. One of the two singletons will be the minimum element, so we simply destroy that node. The rest of the power-of-two trees constitute an entire binomial heap in and of themselves, which is then merged back into the original heap from which we removed the tree of size <math>2^k</math>. This procedure uses a logarithmic number of splits, which takes logarithmic time, followed by a merge of two binomial heaps, which takes logarithmic time; so it is <math>O(\log n)</math> overall.
 
Note that this procedure can be used to delete ''any'' element in the binomial heap, not just the minimum; we keep splitting power-of-two trees until we have the node all by itself, then we just get rid of it, and merge the other power-of-two trees back in. However, implementing this requires that each node maintain a pointer to its parent, and not just to its children (so that, given an arbitrary node, we can always figure out which power-of-two tree it is in, even after splitting).
 
  
 
==Binomial tree representation==
 
==Binomial tree representation==
Line 89: Line 85:
 
* A node in a binomial tree can have more than two children.
 
* A node in a binomial tree can have more than two children.
 
* Binomial trees are fully max-heap-ordered, rather than simply left-max-heap-ordered like power-of-two trees.
 
* Binomial trees are fully max-heap-ordered, rather than simply left-max-heap-ordered like power-of-two trees.
A binomial tree of height 0 is a single node. A binomial tree of height <math>k > 0</math> consists of a root node with <math>k</math> children. One of these children is the root of a binomial tree with height 0, one is the root of a binomial tree with height 1, and so on up to <math>k-1</math>.
+
A binomial tree of height 0 is a single node. A binomial tree of height <math>k > 0</math> consists of a root node with <math>k</math> children. The subtree rooted at each child is a tree of a different height.
 
When we merge two binomial trees of the same size, we simply make the smaller root the child of the larger root. Observe that this instantly gives a binomial tree of twice the size (whose height is then one greater than the heights of the two original trees). When we split a binomial tree of height <math>k > 0</math>, we simply detach the subtree of height <math>k-1</math>, giving two binomial trees of size <math>k-1</math>.
 
When we merge two binomial trees of the same size, we simply make the smaller root the child of the larger root. Observe that this instantly gives a binomial tree of twice the size (whose height is then one greater than the heights of the two original trees). When we split a binomial tree of height <math>k > 0</math>, we simply detach the subtree of height <math>k-1</math>, giving two binomial trees of size <math>k-1</math>.
  

Please note that all contributions to PEGWiki are considered to be released under the Attribution 3.0 Unported (see PEGWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)

Templates used on this page: