22

I need to know the main difference between binary and binomial heaps regardless of the their structure difference that binary heaps can have only two child (tree representation) and binomial heaps can have any number of children.

I am actually just wondering that what so special in organizing the binomial tree structure in such a way that the first child have on one node second have two third have four and so on?

What if, if we use some normal tree for heaps without restriction of two child and then apply the union procedure and just make one heap the left child of the other heaps?

Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
Abdul Samad
  • 5,748
  • 17
  • 56
  • 70
  • 3
    Then you won't be able to do any balancing. The operations won't run in O(log n) time. Look through the proofs for binomial heaps and see where they would fail. – ShreevatsaR Jun 02 '11 at 16:34

3 Answers3

33

The key difference between a binary heap and a binomial heap is how the heaps are structured. In a binary heap, the heap is a single tree, which is a complete binary tree. In a binomial heap, the heap is a collection of smaller trees (that is, a forest of trees), each of which is a binomial tree. A complete binary tree can be built to hold any number of elements, but the number of elements in a binomial tree of some order n is always 2n. Consequently, we only need one complete binary tree to back a binary heap, but we may need multiple binomial trees to back a binomial heap. Interestingly, the orders of the binomial trees used in a binomial heap correspond to the 1 bits set in the binary representation of the number of elements in the forest.

The reason for organizing binomial heaps as they are is that a binomial tree of order n always has exactly 2n nodes in it. This allows us to make assumptions about the number of elements in the binomial tree without actually having to inspect the structure of that tree. On the other hand, a complete binary tree of some height h may have a variable number of nodes in it depending on how the last row is filled out. The fact that each of the children must have a very precisely-defined structure can also be used to prove that the number of children is at most O(log n), where n is the total number of nodes in the heap, which means that the cost of a delete-min is not too large.

An important detail behind this is that a binomial heap is not any tree that happens to have k children. It's a tree that's rigorously defined as

  • The binomial tree of order 0 is a single node, and
  • The binomial tree of order n is a single node with binomial trees of order 0, 1, 2, ..., n - 1 as children.

(Technically, the order 0 special case isn't necessary here). You can see this here:

Binomial trees!

Note that there is exactly one tree of each order, with no flexibility at all in the number or position of the nodes.

However, an important alternative definition is the following:

  • The binomial tree of order 0 is a single node, and
  • The binomial tree of order n is two binomial trees of order n - 1, with one of the trees made a child of the other.

(Take a minute to see why these are equivalent). Using this second definition, it's a quick induction proof to show that the number of nodes in the tree is 2n. As a base case, a tree of order 0 has 20 = 1 nodes as required. For the inductive step, if we have two trees of order n - 1, they collectively have 2n-1 + 2n-1 = 2n nodes as required. Thus the total number of nodes in a binomial tree of order n is exactly 2n.

The idea for a heap that you're describing in your final paragraph does not always lead to an efficient runtime. In particular, if you have trees with a huge branching factor and no other structural constraints, then you could in theory build a heap of n nodes consisting of a single node with (n - 1) children. In that case, after deleting the minimum element from the heap, you'd have to look at all n - 1 children to determine which was the new minimum, giving a runtime of O(n). The other structural constraints on trees like complete binary trees, binomial trees, etc. guarantee that this worst-case doesn't happen.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
2

add on to great answer above provided by templatetypedef. Here is a visual table to show different time complexity on different operations

╔══════════════╦═══════════════════════╦════════════════════════╗
║  Operation   ║       Binary          ║      Binomial          ║
╠══════════════╬═══════════════════════╬════════════════════════╣
║              ║                       ║                        ║                              
║   insert     ║      O(logN)          ║      O(logN)           ║
║              ║                       ║                        ║                              
╠══════════════╬═══════════════════════╬════════════════════════╣
║  find Min    ║       O(1)            ║      O(logN)           ║
║              ║                       ║                        ║
╠══════════════╬═══════════════════════╬════════════════════════╣
║              ║                       ║                        ║
║   Revmove    ║       O(logN)         ║      O(logN)           ║
║              ║                       ║                        ║
╠══════════════╬═══════════════════════╬════════════════════════╣
║              ║                       ║                        ║
║ Decrease Key ║       O(logN)         ║      O(logN)           ║
║              ║                       ║                        ║
╠══════════════╬═══════════════════════╬════════════════════════╣
║              ║                       ║                        ║
║    Union     ║         O(N)          ║      O(logN)           ║
║              ║                       ║                        ║
╠══════════════╬═══════════════════════╬════════════════════════╣
║              ║ ■ Min element is root ║order k binomial tree Bk║
║              ║ ■ Heap height = logN  ║ ■ Number of nodes = 2k.║
║              ║                       ║ ■ Height = k.          ║                  
║              ║                       ║ ■ Degree of root = k.  ║
║  Useful      ║                       ║ ■ Deleting root yields ║
║  Properties  ║                       ║   binomial trees       ║
║              ║                       ║   Bk-1, … , B0.        ║
║              ║                       ║   (see graph below)    ║
║              ║                       ║                        ║
║              ║                       ║                        ║
║              ║                       ║                        ║
║              ║                       ║                        ║        
╚══════════════╩═══════════════════════╩════════════════════════╝

I got this image from the Princeton lecture slides

Binary Heap: (almost complete binary tree) Binary Heap

Binomial Heap:

enter image description here k bonomial trees

OLIVER.KOO
  • 5,654
  • 3
  • 30
  • 62
1

A binary heap can be created by jointing of any two child full binary trees of the same rank onto the root node. It is a tree with a bit free style - some leaves can be cut from the right

A binomial tree of rank N is not a forest of trees. It is a root node with connected to it binomial trees of rank N-1, N-2,...,1,0. Binomial heap is a tree with absolutely fixed structure.

(I am afraid, somebody had read Wiki in a wrong way.) A binomial tree of order k has a root node whose children are roots of binomial trees of orders k−1, k−2, ..., 2, 1, 0 (in this order).

Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • There's a bit more structure to a binary heap. The tree has to be a complete binary tree, not just any ordinary binary tree, so merging two complete binary trees of different heights takes time O(n + m). – templatetypedef Feb 07 '12 at 22:54
  • http://en.wikipedia.org/wiki/Binary_heap. Look at pictures at upper left corner. I don't insist, wiki is not an absolute authority, but are you sure? – Gangnus Feb 07 '12 at 23:05
  • @Gagnus- Those are complete binary trees - note that they're only missing nodes at the bottom level. :-) – templatetypedef Feb 07 '12 at 23:10
  • so, what is the difference? Way how we come to the result? – Gangnus Feb 07 '12 at 23:16
  • @Gangnus- I agree that a binary heap uses a binary tree, but you can't just use any ordinary binary tree and have to use complete binary trees. Moreover, you can't just merge two complete binary trees in the way you've suggested, since if they have different heights, the merged tree is not necessarily a complete binary tree. Try merging a single node and a complete tree of height 100 the way you suggest; it's definitely not a complete binary tree when you're done! – templatetypedef Feb 07 '12 at 23:19
  • But when you cut some leaves from the bottom, it is not a complete binary tree, either. – Gangnus Feb 07 '12 at 23:28
  • @Gangnus- Provided that you cut the leaves from right-to-left, yes, it would be a complete binary tree. – templatetypedef Feb 07 '12 at 23:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/7440/discussion-between-templatetypedef-and-gangnus) – templatetypedef Feb 07 '12 at 23:34