{"record":{"id":"c9ad0837c7def604","repo":"stanfordnlp/CoreNLP","slug":"not-on-parent-s-left-or-right-branches","errorCode":null,"errorMessage":"Not on parent's left or right branches.","messagePattern":"Not on parent's left or right branches\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/IntervalTree.java","lineNumber":472,"sourceCode":"      if (median.right != null) todo.push(median.right);\n    }\n    if (newRoot == null) return node;\n    else return newRoot;\n  }\n\n  // Moves this node up the tree until it replaces the target node\n  public void rotateUp(TreeNode<E,T> node, TreeNode<E,T> target) {\n    TreeNode<E,T> n = node;\n    boolean done = false;\n    while (n != null && n.parent != null && !done) {\n      // Check if we are the left or right child\n      done = (n.parent == target);\n      if (n == n.parent.left) {\n        n = rightRotate(n.parent);\n      } else if (n == n.parent.right) {\n        n = leftRotate(n.parent);\n      } else {\n        throw new IllegalStateException(\"Not on parent's left or right branches.\");\n      }\n      if (debug) check(n);\n    }\n  }\n\n  // Moves this node to the right and the left child up and returns the new root\n  public TreeNode<E,T> rightRotate(TreeNode<E,T> oldRoot) {\n    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;","sourceCodeStart":454,"sourceCodeEnd":490,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/IntervalTree.java#L454-L490","documentation":"IntervalTree.rotateUp walks up the tree splaying/rotating nodes toward the root. Each node must be linked to its parent via the parent's left or right child pointer; if a node's parent reference is set but the parent does not point back to it, the tree's link invariants are broken and the library refuses to guess a rotation direction.","triggerScenarios":"Calling rotateUp (directly or via balance) on a node whose parent pointer is non-null but whose parent's left/right references were corrupted by manual mutation of IntervalTree nodes, incorrect custom rebalancing, or a stale parent reference after an out-of-band remove.","commonSituations":"Custom code poking at the internal node fields, subclassing IntervalTree and overriding adjust/remove, or a bug in another operation that left parent/child pointers asymmetric.","solutions":["Do not mutate IntervalTree node parent/left/right fields directly; only use the public add/remove/query API","Rebuild the tree from its elements if corruption is suspected (new IntervalTree + re-add all values)","Check any overridden or wrapped IntervalTree operations for missed adjust()/parent reassignment steps","Report the bug with a minimal reproduction if it occurs through public API only"],"exampleFix":"// before: manual mutation\nnode.parent.left = otherNode; // breaks back-pointer, rotateUp throws\n// after\nintervalTree.remove(node.value);\nintervalTree.add(otherValue); // let the tree maintain invariants","handlingStrategy":"validation","validationCode":"if (n.parent == null || (n.parent.left != n && n.parent.right != n)) {\n  throw new IllegalStateException(\"node not linked to parent; tree corrupted\");\n}\ntree.rotateUp(n); // safe","typeGuard":"boolean isLinkedToParent(Node n) {\n  return n.parent != null && (n.parent.left == n || n.parent.right == n);\n}","tryCatchPattern":"try {\n  tree.rotateUp(node);\n} catch (IllegalStateException e) {\n  // tree invariants broken: rebuild\n  tree = rebuildFromElements(elements);\n}","preventionTips":["Never mutate IntervalTree node pointers directly","Use only public add/remove/contains API","Rebuild the tree instead of patching corrupted links","Add debug-mode check() calls after custom operations"],"tags":["data-structure","invariant-violation","interval-tree"],"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"}