{"record":{"id":"8c878f80c22b0ce3","repo":"stanfordnlp/CoreNLP","slug":"ancestor-height-cannot-be-negative","errorCode":null,"errorMessage":"ancestor: height cannot be negative","messagePattern":"ancestor: height cannot be negative","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/Tree.java","lineNumber":2282,"sourceCode":"    for (Tree kid : kids) {\n      size += kid.size();\n    }\n    return size;\n  }\n\n  /**\n   * Return the ancestor tree node {@code height} nodes up from the current node.\n   *\n   * @param height How many nodes up to go. A parameter of 0 means return\n   *               this node, 1 means to return the parent node and so on.\n   * @param root The root node that this Tree is embedded under\n   * @return The ancestor at height {@code height}.  It returns null\n   *         if it does not exist or the tree implementation does not keep track\n   *         of parents\n   */\n  public Tree ancestor(int height, Tree root) {\n    if (height < 0) {\n      throw new IllegalArgumentException(\"ancestor: height cannot be negative\");\n    }\n    if (height == 0) {\n      return this;\n    }\n    Tree par = parent(root);\n    if (par == null) {\n      return null;\n    }\n    return par.ancestor(height - 1, root);\n  }\n\n\n  private static class TreeIterator implements Iterator<Tree> {\n\n    private final List<Tree> treeStack;\n\n    protected TreeIterator(Tree t) {\n      treeStack = new ArrayList<>();","sourceCodeStart":2264,"sourceCodeEnd":2300,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/Tree.java#L2264-L2300","documentation":"Argument-validation guard in Tree.ancestor(int height, Tree root): the height parameter counts how many levels up to walk (0 = this node, 1 = parent, ...), so a negative value has no meaning and is rejected immediately before any traversal begins. It indicates the caller passed height < 0 — the faulty input is the height argument itself, not tree structure.","triggerScenarios":"Calling ancestor(-1, root) or similar, usually from a loop computing relative height as `someIndex - currentIndex` where the index arithmetic can go negative.","commonSituations":"Off-by-one arithmetic in sentence-segmentation or clause-level extraction code; passing a difference of depths that is negative because the 'ancestor' is actually a descendant.","solutions":["Clamp the height: if (h < 0) return null; before calling ancestor","Compute the level difference with depth/pathLength checks and only call ancestor when height >= 0","Fix the index arithmetic producing the negative height"],"exampleFix":"// before\nTree anc = node.ancestor(node.depth() - target.depth(), root); // can be negative\n// after\nint h = node.depth() - target.depth();\nTree anc = h >= 0 ? node.ancestor(h, root) : null;","handlingStrategy":"validation","validationCode":"int h = computedHeight; if (h < 0) return null; Tree anc = node.ancestor(h, root);","typeGuard":"boolean validAncestorHeight(int h) { return h >= 0; }","tryCatchPattern":"try { return node.ancestor(h, root); } catch (IllegalArgumentException e) { return null; }","preventionTips":["Clamp or validate computed heights before calling ancestor","Remember height 0 returns the node itself","Verify depth arithmetic sign when comparing node levels"],"tags":["nlp","trees","argument-out-of-range"],"backgroundTag":"argument-out-of-range","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}