{"record":{"id":"66e41f44a390c5f4","repo":"stanfordnlp/CoreNLP","slug":"old-root-not-a-child-of-it-s-parent","errorCode":null,"errorMessage":"Old root not a child of it's parent","messagePattern":"Old root not a child of it's parent","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/IntervalTree.java","lineNumber":498,"sourceCode":"    if (oldRoot == null || oldRoot.isEmpty() || oldRoot.left == null) return oldRoot;\n\n    TreeNode<E,T> oldLeftRight = oldRoot.left.right;\n\n    TreeNode<E,T> newRoot = oldRoot.left;\n    newRoot.right = oldRoot;\n    oldRoot.left = oldLeftRight;\n\n    // Adjust parents and such\n    newRoot.parent = oldRoot.parent;\n    newRoot.maxEnd = oldRoot.maxEnd;\n    newRoot.size = oldRoot.size;\n    if (newRoot.parent != null) {\n      if (newRoot.parent.left == oldRoot) {\n        newRoot.parent.left = newRoot;\n      } else if (newRoot.parent.right == oldRoot) {\n        newRoot.parent.right = newRoot;\n      } else {\n        throw new IllegalStateException(\"Old root not a child of it's parent\");\n      }\n    }\n\n    oldRoot.parent = newRoot;\n    if (oldLeftRight != null) oldLeftRight.parent = oldRoot;\n    adjust(oldRoot);\n    return newRoot;\n  }\n\n  // Moves this node to the left and the right child up and returns the new root\n  public TreeNode<E,T> leftRotate(TreeNode<E,T> oldRoot) {\n    if (oldRoot == null || oldRoot.isEmpty() || oldRoot.right == null) return oldRoot;\n\n    TreeNode<E,T> oldRightLeft = oldRoot.right.left;\n\n    TreeNode<E,T> newRoot = oldRoot.right;\n    newRoot.left = oldRoot;\n    oldRoot.right = oldRightLeft;","sourceCodeStart":480,"sourceCodeEnd":516,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/IntervalTree.java#L480-L516","documentation":"rightRotate moves oldRoot down to the left and its left child up. After rotating, if the new root still has a parent, that parent's child pointer must reference oldRoot before the rotation so it can be swapped to newRoot; if neither parent.left nor parent.right equals oldRoot, the tree linkage is inconsistent and the library throws instead of silently corrupting the tree.","triggerScenarios":"Calling rightRotate on a node whose parent pointer is stale or whose parent's child pointers no longer reference it — typically after manual node mutation or an out-of-band structural change.","commonSituations":"Hand-written tree surgery, subclass overrides, or debugging experiments that detach/reattach nodes without updating the symmetric pointer.","solutions":["Avoid direct manipulation of IntervalTree internals; use add/remove API","Verify parent/child back-pointers are symmetric before invoking rotation helpers","Rebuild the tree from its elements to restore invariants","File a bug with reproduction if reachable via public API"],"exampleFix":"// before\nsomeNode.parent = p; // p.left/right not updated\nIntervalTree.Node r = tree.rightRotate(someNode); // throws\n// after\n// reattach via the tree API so p.left/right and someNode.parent stay consistent","handlingStrategy":"validation","validationCode":"if (newRoot.parent != null && newRoot.parent.left != oldRoot && newRoot.parent.right != oldRoot) {\n  throw new IllegalStateException(\"rotation precondition violated\");\n}\nNode r = tree.rightRotate(oldRoot);","typeGuard":"boolean isChildOfParent(Node oldRoot) {\n  return oldRoot.parent == null || oldRoot.parent.left == oldRoot || oldRoot.parent.right == oldRoot;\n}","tryCatchPattern":"try {\n  Node r = tree.rightRotate(node);\n} catch (IllegalStateException e) {\n  // restore via rebuild; do not continue with corrupted tree\n  tree = rebuildFromElements(elements);\n}","preventionTips":["Keep parent/child back-pointers symmetric when doing any tree surgery","Prefer public API over calling rotation helpers directly","Run IntervalTree's debug check() after structural changes","Treat the tree as unusable after this exception"],"tags":["data-structure","rotation","invariant-violation"],"backgroundTag":"internal-invariant-violation","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}