private void CondenseTree(Node<T> l) { // CT1 [Initialize] Set n=l. Set the list of eliminated // nodes to be empty. var n = l; // TIntStack eliminatedNodeIds = new TIntStack(); var eliminatedNodeIds = new Stack<int>(); // CT2 [Find parent entry] If N is the root, go to CT6. Otherwise // let P be the parent of N, and let En be N's entry in P while (n.level != treeHeight) { var parent = GetNode(parents.Pop()); var parentEntry = parentsEntry.Pop(); // CT3 [Eliminiate under-full node] If N has too few entries, // delete En from P and add N to the list of eliminated nodes if (n.entryCount < minNodeEntries) { parent.DeleteEntry(parentEntry, minNodeEntries); eliminatedNodeIds.Push(n.nodeId); } else { // CT4 [Adjust covering rectangle] If N has not been eliminated, // adjust EnI to tightly contain all entries in N if (!n.mbr.Equals(parent.entries[parentEntry])) { oldRectangle.Set(parent.entries[parentEntry]._min, parent.entries[parentEntry]._max); parent.entries[parentEntry].Set(n.mbr._min, n.mbr._max); parent.RecalculateMBR(oldRectangle); } } // CT5 [Move up one level in tree] Set N=P and repeat from CT2 n = parent; } // CT6 [Reinsert orphaned entries] Reinsert all entries of nodes in set Q. // Entries from eliminated leaf nodes are reinserted in tree leaves as in // Insert(), but entries from higher level nodes must be placed higher in // the tree, so that leaves of their dependent subtrees will be on the same // level as leaves of the main tree while (eliminatedNodeIds.Count > 0) { var e = GetNode(eliminatedNodeIds.Pop()); for (var j = 0; j < e.entryCount; j++) { Add(e.entries[j], e.ids[j], e.level); e.entries[j] = null; } e.entryCount = 0; deletedNodeIds.Push(e.nodeId); } }
// oldRectangle is a rectangle that has just been deleted or made smaller. // Thus, the MBR is only recalculated if the OldRectangle influenced the old MBR internal void RecalculateMBR(Rectangle deletedRectangle) { if (mbr.EdgeOverlaps(deletedRectangle)) { mbr.Set(entries[0]._min, entries[0]._max); for (var i = 1; i < entryCount; i++) { mbr.Add(entries[i]); } } }