示例#1
0
 private void insert1(Point2D p) {
   if (contains(p)) return;
   boolean newAdded = false;
   if (root == null) {
     initRoot(p);
   } else {
     TreeNode curr = root;
     while (true) {
       if (TreeNode.slightlyEquals(p, curr.value)) {
         return;
       }
       if (curr.even) {
         if (p.x() <= curr.value.x()) {
           if (curr.left == null) {
             TreeNode left = newNode(p, curr);
             left.rect =
                 new RectHV(curr.rect.xmin(), curr.rect.ymin(), curr.value.x(), curr.rect.ymax());
             curr.left = left;
             break;
           } else {
             curr = curr.left;
           }
         } else {
           if (curr.right == null) {
             TreeNode right = newNode(p, curr);
             right.rect =
                 new RectHV(curr.value.x(), curr.rect.ymin(), curr.rect.xmax(), curr.rect.ymax());
             curr.right = right;
             break;
           } else {
             curr = curr.right;
           }
         }
       } else {
         if (p.y() < curr.value.y()) {
           if (curr.left == null) {
             TreeNode left = newNode(p, curr);
             left.rect =
                 new RectHV(curr.rect.xmin(), curr.rect.ymin(), curr.rect.xmax(), curr.value.y());
             curr.left = left;
             break;
           } else {
             curr = curr.left;
           }
         } else {
           if (curr.right == null) {
             TreeNode right = newNode(p, curr);
             right.rect =
                 new RectHV(curr.rect.xmin(), curr.value.y(), curr.rect.xmax(), curr.rect.ymax());
             curr.right = right;
             break;
           } else {
             curr = curr.right;
           }
         }
       }
     }
   }
   size++;
 }
示例#2
0
 public void insert(Point2D p) {
   if (isEmpty()) {
     initRoot(p);
     size++;
   } else {
     if (!contains(p)) {
       final boolean insert = root.insert(p);
       if (insert) {
         size++;
       }
       assert insert;
     }
   }
 }