@@ -259,21 +259,40 @@ private List<LeafNode> collectLeafVertices(TreeNode parent, List<LeafNode> leafV
259259 * @param element
260260 * @return
261261 */
262+ // public static <T> RTree<T> remove(RTree<T> rtree, T element) {
263+ // log.trace("want to remove {} from tree size {}", element, rtree.count());
264+ // if (rtree.root.isEmpty()) {
265+ // // this tree is empty
266+ // return new RTree();
267+ // }
268+ // Node<T> rootVertex = rtree.root.get();
269+ // Node<T> newRoot = rootVertex.remove(element);
270+ //
271+ // // if the newRoot is empty, return a new empty RTree, otherwise, return this
272+ // if (newRoot.count() == 0) {
273+ // return RTree.create();
274+ // } else {
275+ // return rtree;
276+ // }
277+ // }
278+
279+ /**
280+ * Remove an element from the RTree. Fixed version: always returns a fresh RTree with the updated
281+ * root.
282+ */
262283 public static <T > RTree <T > remove (RTree <T > rtree , T element ) {
263- log .trace ("want to remove {} from tree size {}" , element , rtree .count ());
264284 if (rtree .root .isEmpty ()) {
265- // this tree is empty
266- return new RTree ();
285+ return rtree ;
267286 }
268- Node <T > rootVertex = rtree .root .get ();
269- Node <T > newRoot = rootVertex .remove (element );
270287
271- // if the newRoot is empty, return a new empty RTree, otherwise, return this
272- if (newRoot .count () == 0 ) {
273- return RTree .create ();
274- } else {
275- return rtree ;
288+ Node <T > newRoot = rtree .root .get ().remove (element );
289+
290+ if (newRoot == null || newRoot .count () == 0 ) {
291+ return create ();
276292 }
293+
294+ // Always return a new RTree wrapper with the updated root
295+ return new RTree <>(newRoot );
277296 }
278297
279298 /**
0 commit comments