示例#1
0
  // Private method for deleting a node. Called from the public delete method. Works recursively
  // Method works by recursively deleting a node, and then once backing out of the recursion all
  // data members are updated for each node
  private boolean delete(TreeNode t, Point p, boolean cd) {
    boolean status = false;
    // Base case, not found so return false
    if (t == null) return false;
    if (t.p.equals(p)) { // Found the node, now delete it
      if (t.right != null) {
        TreeNode min = findMin(t.right, cd);
        swapData(t, min);
        status = delete(min, min.p, min.cd);
      } else if (t.left != null) {
        TreeNode min = findMin(t.left, cd);
        swapData(t, min);
        t.right = t.left;
        t.left = null;
        status = delete(min, min.p, min.cd);
      } else { // At a leaf, remove the node
        size--;
        status = deleteFromParent(t);
      }
    } else if (cd) {
      if (p.x() < t.p.x()) status = delete(t.left, p, !cd);
      else if (p.x() > t.p.x()) status = delete(t.right, p, !cd);
      else { // They must be equal, cut on y
        if (p.y() < t.p.y()) status = delete(t.left, p, !cd);
        else status = delete(t.right, p, !cd);
      }
    } else {
      if (p.y() < t.p.y()) status = delete(t.left, p, !cd);
      else if (p.y() > t.p.y()) status = delete(t.right, p, !cd);
      else { // They must be equal, cut on x
        if (p.x() < t.p.x()) status = delete(t.left, p, !cd);
        else status = delete(t.right, p, !cd);
      }
    }
    // Backing out of recursion, need to update data members
    updateMinMaxOnDeleteAndRebuild(t);
    t.height = maxHeight(t.left, t.right) + 1;

    // Check if becoming unbalanced
    if (unbalanced(t)) {
      Point[] points = collectSatanSpawn(t);
      TDTree newSubTree = new TDTree(points, cd);
      swapDataMembers(t, newSubTree.root);
    }
    return status;
  } // End private delete method
示例#2
0
 /**
  * Private method created for inserting into the tree. Works recursively returns false if point
  * already in tree Method for inserting a node into the tree. Works by traversing through the
  * tree, branches off x value for even cuts, and y for odd cuts. Uses other value as a tie
  * breaker. Also keeps track of height and size for constant return time on those function calls.
  */
 private boolean insert(TreeNode t, TreeNode prev, TreeNode newNode, boolean cd, boolean left) {
   if (t == null) { // Found where node should live
     if (left) {
       prev.left = newNode;
     } else {
       prev.right = newNode;
     }
     newNode.cd = cd;
     newNode.parent = prev;
     size++;
     return true;
   } else if (t.p.equals(newNode.p)) return false; // Found the point, don't insert it
   // Recursive calls to left or right tree
   else if (cd) {
     if (newNode.p.x() < t.p.x()) insert(t.left, t, newNode, !cd, true);
     else if (newNode.p.x() > t.p.x()) insert(t.right, t, newNode, !cd, false);
     else { // Tie breaker
       if (newNode.p.y() < t.p.y()) insert(t.left, t, newNode, !cd, true);
       else insert(t.right, t, newNode, !cd, false);
     }
   } else {
     if (newNode.p.y() < t.p.y()) insert(t.left, t, newNode, !cd, true);
     else if (newNode.p.y() > t.p.y()) insert(t.right, t, newNode, !cd, false);
     else { // Tie breaker
       if (newNode.p.x() < t.p.x()) insert(t.left, t, newNode, !cd, true);
       else insert(t.right, t, newNode, !cd, false);
     }
   }
   updateMinMax(t, newNode);
   t.height = maxHeight(t.left, t.right) + 1;
   // Now check if tree became unbalanced
   if (unbalanced(t)) {
     // System.out.println("unbalanced");
     Point[] points = collectSatanSpawn(t);
     TDTree newSubTree = new TDTree(points, cd);
     swapDataMembers(t, newSubTree.root);
   }
   return true;
 } // End private insert() method
示例#3
0
  // ## operation height()
  public int height() {
    // #[ operation height()
    return root.height();

    // #]
  }