3

I am reading Binomial Heap in Purely Functional Data Structures.

The implementation of insTree function confused me quite much. Here are the set of codes

datatype Tree = Node of int * Elem.T * Tree list

fun link (t1 as Node (r, x1, c1), t2 as Node (_, x2, c2)) = 
  if Elem.leq (x1, x2) then Node (r+1, x1, t2::c1)
  else Node (r+1, x2, t1::c2)

fun rank (Node (r, x, c)) = r

fun insTree (t, []) = [t]
  | insTree (t, ts as t' :: ts') =
      if rank t < rank t' then t::ts else insTree (link (t, t'), ts')

My confusion lies on the bit in insTree that why it does not consider the situation of rank t > rank t'?

In if rank t < rank t' then t::ts else insTree (link (t, t'), ts'),

  1. if t's rank is less than t''s rank, then put the t into the heap, no question asked
  2. the else has two cases: equal and bigger.
  3. For equal, yes, we can link two trees (we link only two trees with the same rank), and then try to insert the new linked tree into the heap, no question asked.
  4. but even the bigger case would have the same as equal, why? Even if rank t > rank t', we still link them?

Edit

I thought the process of inserting a binomial tree into a binomial heap should be like this:

  1. We get the tree t and the heap
  2. In the heap (actually a list), we compare rank of the tree t with every tree in the heap
  3. We find a missing rank (increasing order in the heap) which matches the rank of t, we put t at that slot
  4. We find a tree in the heap with same rank as t, then we link two trees and process a rank+1 new tree and try again insert the new tree to the heap.

So, I think the correct fun insTree could be like this:

fun insTree (t, []) = [t]
      | insTree (t, ts as t' :: ts') =
          if rank t < rank t' then t::ts 
          else if rank t = rank t' then insTree (link (t, t'), ts')
          else t'::(insTree (t, ts'))
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271

2 Answers2

2

insTree is a helper function that is not visible to the user. The user calls insert, which in turn calls insTree with a tree of rank 0, and a list of trees of increasing rank. insTree has an invariant that the rank of t is <= the rank of the first tree in the list. So if it's not <, then it must be =.

You're right that if insTree was a general-purpose public function, rather than a special-purpose private function, then it would have to deal with the missing case.

Chris Okasaki
  • 4,875
  • 1
  • 20
  • 19
  • Thanks for your answer Chris. I thought so too, but in the `fun merge` (on the same page 22), insTree is used as a general-purpose function, no matter it is public or private. `else insTree (link (t1, t2), merge (tsp', tsp'))`, obviously `link(t1,t2)` may not necessarily be rank 0. – Jackson Tale Oct 31 '13 at 23:00
  • 1
    Right, link(t1,t2) is not rank 0, but it's still <= the rank of the first tree in merged list. – Chris Okasaki Nov 01 '13 at 00:14
0

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.

This answer, explain why the insert function, which is the one required to construct a binomial heap, do not manage these cases (In theory they cannot happen). Maybe the cases you are proposing make sense for a merging operation (but the underlying implementation should differ).

Community
  • 1
  • 1
zurgl
  • 1,930
  • 1
  • 14
  • 20
  • The insTree is `not inserting a tree to another tree`, but inserting a tree to a heap. `to a heap` may have those cases – Jackson Tale Oct 31 '13 at 12:15
  • Yes the purpose of `insTree` is to build a binomial heap using a binomial heap and a tree. And this can only occurs if your initial binomial heap is well formed (else this is not a binomial heap), and the tree (the one going to be inserted) have a rank upper than any tree present into the initial binomial heap. This explain why you won't find these extract case managed by your insTree function. – zurgl Oct 31 '13 at 12:21
  • Could you please define `well formed binomial heap`? and why `the to be inserted tree` must have a bigger rank? – Jackson Tale Oct 31 '13 at 12:28
  • Well, you're right it appears that I confused binomial heap and binomial tree. Your question is a but more complex. sorry for the disagreement – zurgl Oct 31 '13 at 12:58
  • no problem. I myself just wants to figure everything more clearly – Jackson Tale Oct 31 '13 at 14:07