diff --git a/src/RTree/RTree.cs b/src/RTree/RTree.cs index 1b03e42..7c655bd 100644 --- a/src/RTree/RTree.cs +++ b/src/RTree/RTree.cs @@ -204,7 +204,7 @@ private void Insert(RTreeNode item, int level) } // adjust bboxes along the insertion path - AdjutsParentBounds(envelope, insertPath, level); + AdjustParentBounds(envelope, insertPath, level); } private static int CombinedArea(Envelope what, Envelope with) @@ -416,24 +416,26 @@ public void Remove(T item, Envelope envelope) private void CondenseNodes(IList> path) { + var count = path.Count; + // go through the path, removing empty nodes and updating bboxes - for (var i = path.Count - 1; i >= 0; i--) + for (var i = 0; i < count; i++) { - if (path[i].Children.Count == 0) + if (path[i].Children.Count == 0) { - if (i == 0) + if (i == count - 1) { - Clear(); + Clear(); } else { - var siblings = path[i - 1].Children; - siblings.Remove(path[i]); - } + var siblings = path[i + 1].Children; + siblings.Remove(path[i]); + } } else { - RefreshEnvelope(path[i]); + RefreshEnvelope(path[i]); } } } @@ -456,7 +458,7 @@ private static Envelope SumChildBounds(RTreeNode node, int startIndex, int en return retval; } - private static void AdjutsParentBounds(Envelope bbox, List> path, int level) + private static void AdjustParentBounds(Envelope bbox, List> path, int level) { // adjust bboxes along the given tree path for (var i = level; i >= 0; i--)