Skip to content

Commit

Permalink
Fixed bug where tighten would not be called on insert; ensured that n…
Browse files Browse the repository at this point in the history
…on-leaf root node always has >= 2 entries
  • Loading branch information
Russ Weeks committed Aug 24, 2012
1 parent 5dcce9b commit 9f5b8d2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
37 changes: 21 additions & 16 deletions src/com/newbrightidea/util/RTree.java
Expand Up @@ -165,12 +165,8 @@ public boolean delete(float[] coords, float[] dimensions, T entry)
assert (coords.length == numDims);
assert (dimensions.length == numDims);
Node l = findLeaf(root, coords, dimensions, entry);
if (l == null )
{
System.out.println( "WTF?" );
findLeaf(root, coords, dimensions, entry);
}
assert (l.leaf);
assert (l != null) : "Could not find leaf for entry to delete";
assert (l.leaf) : "Entry is not found at leaf?!?";
ListIterator<Node> li = l.children.listIterator();
T removed = null;
while (li.hasNext())
Expand Down Expand Up @@ -264,6 +260,11 @@ else if (!n.leaf && (n.children.size() < minEntries))
{
root = buildRoot(true);
}
else if ( (root.children.size() == 1) && (!root.leaf) )
{
root = root.children.get(0);
root.parent = null;
}
else
{
tighten(root);
Expand All @@ -277,6 +278,15 @@ else if (!n.leaf && (n.children.size() < minEntries))
size -= q.size();
}

/**
* Empties the RTree
*/
public void clear()
{
root = buildRoot(true);
// let the GC take care of the rest.
}

/**
* Inserts the given entry into the RTree, associated with the given
* rectangle.
Expand Down Expand Up @@ -309,15 +319,6 @@ public void insert(float[] coords, float[] dimensions, T entry)
}
}

/**
* Empties the RTree
*/
public void clear()
{
root = buildRoot(true);
// let the GC take care of the rest.
}

private void adjustTree(Node n, Node nn)
{
if (n == root)
Expand All @@ -344,7 +345,7 @@ private void adjustTree(Node n, Node nn)
adjustTree(splits[0], splits[1]);
}
}
else if (n.parent != null)
if (n.parent != null)
{
adjustTree(n.parent, null);
}
Expand Down Expand Up @@ -372,13 +373,17 @@ private Node[] splitNode(Node n)
{
nn[1].children.addAll(cc);
cc.clear();
tighten(nn[0]); // Not sure this is required.
tighten(nn[1]);
return nn;
}
else if ((nn[1].children.size() >= minEntries)
&& (nn[0].children.size() + cc.size() == minEntries))
{
nn[0].children.addAll(cc);
cc.clear();
tighten(nn[0]); // Not sure this is required.
tighten(nn[1]);
return nn;
}
Node c = cc.pop();
Expand Down
32 changes: 17 additions & 15 deletions test/com/newbrightidea/util/TestRTree.java
Expand Up @@ -262,23 +262,25 @@ class DataObject {
}
}

RTree<Integer> tree = new RTree<Integer>(4, 2, 3);
List<DataObject> rects = new ArrayList<DataObject>();
for ( int j = 0; j < 500; j++ )
{
RTree<Integer> tree = new RTree<Integer>(10, 2, 3);
List<DataObject> rects = new ArrayList<DataObject>();

for (int i = 0; i < 15; i++) {
rects.add(new DataObject(
new float[]{i, i * 2, i * 3},
new float[]{0, 0, 0},
i));
DataObject dataObject = rects.get(i);
tree.insert(dataObject.val, dataObject.dim, dataObject.id);
}
for (int i = 0; i < 100; i++) {
rects.add(new DataObject(
new float[]{i, i * 2, i * 3},
new float[]{0, 0, 0},
i));
DataObject dataObject = rects.get(i);
tree.insert(dataObject.val, dataObject.dim, dataObject.id);
}

for (int i = 0; i < 15; i++) {
DataObject dataObject = rects.get(i);
System.out.print(" " + i);
boolean deleted = tree.delete(dataObject.val, dataObject.dim, dataObject.id);
assert deleted;
for (int i = 0; i < 100; i++) {
DataObject dataObject = rects.get(i);
boolean deleted = tree.delete(dataObject.val, dataObject.dim, dataObject.id);
assert deleted;
}
}
}

Expand Down

0 comments on commit 9f5b8d2

Please sign in to comment.