Leftist trees

A heap is a tree in which the root is larger in priority than its children. It is used to implement a priority queue. A leftist tree is a heap that allows efficent merging of priority queues.

A height balanced leftist tree is a binary tree such that for every internal node, the s value (distance to fall off the tree) of the left child is greater than or equal to the s value of the right tree.

node * meld(node * a, node * b)
{  if (a==null) return b;
   if (b==null) return a;
   node * big = max(a,b);
   node * small = min(a,b);
   node * t1 = big->left;
   node * t2 = meld(small,big->right)
   if (t1->s >t2->s)  return maketree(big->data, t1,t2);
   return maketree(big->data, t2,t1);
}
tex2html_wrap34 tex2html_wrap36

How is s (the distance to a leaf) updated? At your seats, modify the psuedocode to add the s update.

Notice that you are not allowed to keep asking a node to compute it's ``s'' value. You need to keep it locally. Like your first grade teacher taught you, everybody needs to clean up after themselves and leave the work area ready for the next person.

For each item, create a single node hblt and place if queue Q.

While (Q.size >1)
{
  t1 = Q.delete;
  t2 = Q.delete;
  Q.insert(meld(t1,t2));
}


Vicki Allan
Mon Jul 1 08:57:52 MDT 2002