Is this Leftist Tree piece of code from Wikipedia correct?

Posted by they changed my name on Stack Overflow See other posts from Stack Overflow or by they changed my name
Published on 2010-05-06T20:34:51Z Indexed on 2010/05/06 20:38 UTC
Read the original article Hit count: 126

Filed under:
|

Link

public Node merge(Node x, Node y) {
  if(x == null)
    return y;
  if(y == null) 
    return x;

  // if this was a max height biased leftist tree, then the 
  // next line would be: if(x.element < y.element)
  if(x.element.compareTo(y.element) > 0) {  
    // x.element > y.element
    Node temp = x;
    x = y;
    y = temp;
  }

  x.rightChild = merge(x.rightChild, y);

  if(x.leftChild == null) {
    // left child doesn't exist, so move right child to the left side
    x.leftChild = x.rightChild;
    x.rightChild = null;
    x.s = 1;
  } else {
    // left child does exist, so compare s-values
    if(x.leftChild.s < x.rightChild.s) {
      Node temp = x.leftChild;
      x.leftChild = x.rightChild;
      x.rightChild = temp;
    }
    // since we know the right child has the lower s-value, we can just
    // add one to its s-value
    x.s = x.rightChild.s + 1;
  }
  return x;
}

What makes me ask this question is:

  if(x.element.compareTo(y.element) > 0) {  
    // x.element > y.element
    Node temp = x;
    x = y;
    y = temp;
  }

Isn't that just not gonna work, since the references are only switched inside the method?

© Stack Overflow or respective owner

Related posts about java

Related posts about reference