3

Leftist heap maintains a key and a rank for every node. The rank of a node is the number of nodes along in the shortest path to a leaf.

The whole tree needs two properties to be maintained:

  1. node.key < node.left.key && node.key < node.right.key
  2. node.left.rank >= node.right.rank

I can understand the first property as it is a heap and it is natural.

But what's the purpose of the 2nd property? Why we need to keep the right shorter than left?

For balancing purpose?

Jackson Tale
  • 25,428
  • 34
  • 149
  • 271

1 Answers1

0

The article you cited said:

These conditions ensure that the leftist tree is a heap (the element with the minimum key is on the top of the heap), and that the shortest path to a leaf node is obtained by following the rightlink

The condition you asked about and

P.rank = 1 + min( (P.left).rank, (P.right).rank );

Ensures the second.

Because the meld operation is along the right nodes, this ensures the logarithmic performance of meld.

zw324
  • 26,764
  • 16
  • 85
  • 118
  • Edited. That was typed on a phone so bad sport maybe, but you can -1 instead of insulting people. – zw324 Jul 23 '13 at 16:58
  • Ensuring the path is shortest makes sure that the path is at most log(n), which ensures the meld operation's log(n) complexity. – zw324 Jul 23 '13 at 17:43
  • How did `meld` get in there? **`logarithmic performance`**?? Are you sure you are answering the right question? This may be an answer to another question. – darksky Jul 23 '13 at 19:30
  • @darksky: It seems there are different terminologies: Tarjan call "merge" as "meld" in his book, and also many other sources. Also, merge/meld in leftist heaps have log(n) performance - or am I missing something in this question? Happy to be corrected if that's the case. – zw324 Jul 23 '13 at 20:10
  • To be honest, the real deeper question I wanna ask is that how this rank/right thing can keep logN performance? I mean why not seeking a way to balance the tree which also gives logN performance? I don't know how to ask better, but if you can understand me and improve your answer even more, I will be appreciated. – Jackson Tale Jul 23 '13 at 21:26