Data Structures and Algorithms Discussion Board
February 22, 2012, 09:31:32 PM *
Welcome, Guest. Please login or register.
Login with username, password and session length
News: Looking for a reliable webhosting provider? Read HostGator review to find 7 arguments in support of HostGator.
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: BST Remove Help  (Read 1117 times)
jvanbev
Newbie
*

Rating: 0
Offline Offline

Posts: 1


« on: March 26, 2011, 01:14:31 AM »

public class BinarySearchTree {
      …
     
      public boolean remove(int value) {
            if (root == null)
                  return false;
            else {
                  if (root.getValue() == value) {
                        BSTNode auxRoot = new BSTNode(0);
                        auxRoot.setLeftChild(root);
                        boolean result = root.remove(value, auxRoot);
                        root = auxRoot.getLeft();
                        return result;
                  } else {
                        return root.remove(value, null);
                  }
            }
      }
}
public class BSTNode {
      …

      public boolean remove(int value, BSTNode parent) {
            if (value < this.value) {
                  if (left != null)
                        return left.remove(value, this);
                  else
                        return false;
            } else if (value > this.value) {
                  if (right != null)
                        return right.remove(value, this);
                  else
                        return false;
            } else {
                  if (left != null && right != null) {
                        this.value = right.minValue();
                        right.remove(this.value, this);
                  } else if (parent.left == this) {
                        parent.left = (left != null) ? left : right;
                  } else if (parent.right == this) {
                        parent.right = (left != null) ? left : right;
                  }
                  return true;
            }
      }
 
      public int minValue() {
            if (left == null)
                  return value;
            else
                  return left.minValue();
      }
}

In the last few lines of the remove(int, node) function...There are a few lines I don't understand...
                  } else if (parent.left == this) {
                        parent.left = (left != null) ? left : right;
                  } else if (parent.right == this) {
                        parent.right = (left != null) ? left : right;
                  }

in the second line.... what is the parent.left = (left != null) ? left:right?Huh
I'm guessing it's saying something like if parent.left...while left != null, swap the left and right...But I don't fully understand it.. Please explain! Thanks!
Logged
Pages: [1]
  Print  
 
Jump to:  

 
Partners Ads        new online film free        play games