コード例 #1
0
ファイル: TDTree.java プロジェクト: CDargis/TDTree
  // Works by calling private insertion method
  public boolean insert(Point pt) {
    if (pt == null) return false;

    // Tree is empty
    if (root == null) {
      root = new TreeNode(pt);
      root.cd = true;
      size++;
      return true;
    } else return insert(root, null, new TreeNode(pt), true, true);
  }
コード例 #2
0
ファイル: TDTree.java プロジェクト: CDargis/TDTree
 /**
  * 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