00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 package utils;
00025
00026 import java.util.*;
00027
00037 public class TwoDTree extends Object {
00038
00039 protected Object info;
00040 protected double xval;
00041 protected double yval;
00042 protected TwoDTree parent;
00043 protected TwoDTree llink;
00044 protected TwoDTree rlink;
00045 protected int level;
00046
00050 public TwoDTree() {
00051 initialize();
00052 }
00053
00060 public TwoDTree(Object element, double xPos, double yPos) {
00061 initialize();
00062 xval = xPos;
00063 yval = yPos;
00064 info = element;
00065 }
00066
00069 public void initialize() {
00070 info = null;
00071 xval = 0.0d;
00072 yval = 0.0d;
00073 parent = null;
00074 llink = null;
00075 rlink = null;
00076 level = 0;
00077 }
00078
00079 public Object info() {
00080 return info;
00081 }
00082
00083 public void rlink(TwoDTree n) {
00084 rlink = n;
00085 }
00086
00087 public TwoDTree rlink() {
00088 return rlink;
00089 }
00090
00091 public void llink(TwoDTree n) {
00092 llink = n;
00093 }
00094
00095 public TwoDTree llink() {
00096 return llink;
00097 }
00098
00099 public void parent(TwoDTree n) {
00100 parent = n;
00101 }
00102
00103 public TwoDTree parent() {
00104 return parent;
00105 }
00106
00107 public int level() {
00108 return level;
00109 }
00110
00111 public void level(int l) {
00112 level = l;
00113 }
00114
00115 public boolean isTopNode() {
00116 return parent == null;
00117 }
00118
00119 public boolean isLeafNode() {
00120 return (llink == null) && (rlink == null);
00121 }
00122
00128 public void addElement(Object element, double xPos, double yPos) {
00129 addElement(element,xPos,yPos,(level%2)==1);
00130 }
00131
00140 public void addElement(Object element, double xPos, double yPos,
00141 boolean evenlevel) {
00142 if ((xval == xPos) && (yval == yPos)) {
00143 info = element;
00144 } else {
00145 if (evenlevel) {
00146 if (yval < yPos) {
00147 if (llink == null) {
00148 llink = new TwoDTree(element,xPos,yPos);
00149 llink.parent(this);
00150 llink.level(level + 1);
00151 } else {
00152 llink.addElement(element,xPos,yPos,false);
00153 }
00154 } else {
00155 if (rlink == null) {
00156 rlink = new TwoDTree(element,xPos,yPos);
00157 rlink.parent(this);
00158 rlink.level(level + 1);
00159 } else {
00160 rlink.addElement(element,xPos,yPos,false);
00161 }
00162 }
00163 } else {
00164 if (xval < xPos) {
00165 if (llink == null) {
00166 llink = new TwoDTree(element,xPos,yPos);
00167 llink.parent(this);
00168 llink.level(level + 1);
00169 } else {
00170 llink.addElement(element,xPos,yPos,true);
00171 }
00172 } else {
00173 if (rlink == null) {
00174 rlink = new TwoDTree(element,xPos,yPos);
00175 rlink.parent(this);
00176 rlink.level(level + 1);
00177 } else {
00178 rlink.addElement(element,xPos,yPos,true);
00179 }
00180 }
00181 }
00182 }
00183 }
00184
00189 public void removeElement(double xPos, double yPos) {
00190 TwoDTree node = this.getNode(xPos,yPos);
00191 if (node != null) {
00192 if (node.isLeafNode()) {
00193 if ((node.parent()).llink() == node) {
00194 node.parent().llink(null);
00195 } else {
00196 node.parent().rlink(null);
00197 }
00198 node.parent(null);
00199 } else {
00200 if (node.rlink() == null) {
00201 } else {
00202 }
00203 }
00204 }
00205 }
00206
00209 public void removeAllElements() {
00210 initialize();
00211 }
00212
00216 public int size() {
00217 int lcount = 0, rcount = 0;
00218 if (llink != null) {
00219 lcount = llink.size();
00220 }
00221 if (rlink != null) {
00222 rcount = rlink.size();
00223 }
00224 return 1 + lcount + rcount;
00225 }
00226
00230 public Enumeration elements() {
00231 return (elementsVector()).elements();
00232 }
00233
00234 public Vector elementsVector() {
00235 return collectElements(new Vector());
00236 }
00237
00238 public Vector collectElements(Vector v) {
00239 if (info != null) {
00240 if (llink != null) {
00241 llink.collectElements(v);
00242 }
00243 v.addElement(info);
00244 if (rlink != null) {
00245 rlink.collectElements(v);
00246 }
00247 }
00248 return v;
00249 }
00250
00257 public Object get(double xPos, double yPos) {
00258 TwoDTree node = getNode(xPos,yPos);
00259 if (node == null) {
00260 return null;
00261 } else {
00262 return node.info();
00263 }
00264 }
00265
00266 public TwoDTree getNode(double xPos, double yPos) {
00267 return getNode(xPos,yPos,(level%2)==1);
00268 }
00269
00270 public TwoDTree getNode(double xPos, double yPos, boolean evenlevel) {
00271 if ((xval == xPos) && (yval == yPos)) {
00272 return this;
00273 } else {
00274 if (evenlevel) {
00275 if (yval < yPos) {
00276 if (llink == null) {
00277 return null;
00278 } else {
00279 return llink.getNode(xPos,yPos,false);
00280 }
00281 } else {
00282 if (rlink == null) {
00283 return null;
00284 } else {
00285 return rlink.getNode(xPos,yPos,false);
00286 }
00287 }
00288 } else {
00289 if (xval < xPos) {
00290 if (llink == null) {
00291 return null;
00292 } else {
00293 return llink.getNode(xPos,yPos,true);
00294 }
00295 } else {
00296 if (rlink == null) {
00297 return null;
00298 } else {
00299 return rlink.getNode(xPos,yPos,true);
00300 }
00301 }
00302 }
00303 }
00304 }
00305
00309 public String toString() {
00310 StringBuffer buf = new StringBuffer();
00311 this.toString(buf,0);
00312 return buf.toString();
00313 }
00314
00315 public void toString(StringBuffer buf, int level) {
00316 for (int i = level; i>0; i--) {
00317 buf.append("\t");
00318 }
00319 if (info == null) {
00320 buf.append("NO INFO");
00321 } else {
00322 buf.append(info.toString());
00323 buf.append("(" + (new Integer(level)).toString() +
00324 " | " + (new Double(xval)).toString());
00325 buf.append("," + (new Double(yval)).toString() + ")");
00326
00327 }
00328 buf.append("\r\n");
00329 if (llink != null) {
00330 llink.toString(buf,level + 1);
00331 }
00332 if (rlink != null) {
00333 rlink.toString(buf,level + 1);
00334 }
00335 }
00336
00346 public Vector getAll(double xPos1, double yPos1,
00347 double xPos2, double yPos2) {
00348 Vector v = new Vector();
00349 return getAll(xPos1,yPos1,xPos2,yPos2,(level%2)==1,v);
00350 }
00351
00352 public Vector getAll(double xPos1, double yPos1,
00353 double xPos2, double yPos2,
00354 boolean evenlevel, Vector v) {
00355
00356 if ((xval >= xPos1) && (xval <= xPos2) &&
00357 (yval >= yPos1) && (yval <= yPos2)) {
00358 v.addElement(info); }
00359
00360 if (evenlevel) {
00361 if ((xval >= xPos1) && (llink != null)) {
00362 llink.getAll(xPos1,yPos1,xPos2,yPos2,false,v);
00363 }
00364 if ((xval <= xPos2) && (rlink != null)) {
00365 rlink.getAll(xPos1,yPos1,xPos2,yPos2,false,v);
00366 }
00367 } else {
00368 if ((yval >= yPos1) && (llink != null)) {
00369 llink.getAll(xPos1,yPos1,xPos2,yPos2,true,v);
00370 }
00371 if ((yval <= yPos2) && (rlink != null)) {
00372 rlink.getAll(xPos1,yPos1,xPos2,yPos2,true,v);
00373 }
00374 }
00375 return v;
00376 }
00377 }
00378