# bst Companion code for the ankurm.com article **Binary Search Tree in Java: Insert, Delete, Search, Validate BST and LCA (With Dry Runs)**. Insert and search (recursive and iterative), the three delete cases, three broken variants, BST validation, kth smallest, lowest common ancestor, floor/ceiling, height and balance, and ten interview problems checked against brute force on seeded random trees. Every figure and console line in the article is quoted from a file in `output/`, which the JUnit tests write while asserting the same results. There is no `docs/` folder: the explanations live in the article itself. ## Versions | Component | Version | Verified against | |---|---|---| | JDK | Temurin 25.0.5.1+2 (LTS) | `java -version` | | JUnit | 6.1.3 | `junit-bom` `maven-metadata.xml` on Maven Central (latest GA) | | Maven | 3.9.11 | `mvn -version` | ## Quickstart ```bash cd bst export JAVA_HOME=/path/to/jdk-25 PATH="$JAVA_HOME/bin:$PATH" ./scripts/run-all.sh # runs 25 tests and regenerates output/*.txt java -cp target/classes com.ankurm.interviewlab.bst.DeepTreeDemo 100000 ``` The first Maven run must be online so the plugins land in the local cache. ## Source layout | File (`src/main/java/com/ankurm/interviewlab/bst/`) | Contents | |---|---| | `Node.java` | key plus two child links | | `Insert.java`, `Search.java`, `Delete.java` | recursive and iterative versions; min and max | | `Validate.java` | two wrong checks, the min/max-bounds check, the inorder check | | `Queries.java` | kth smallest, LCA, floor, ceiling, inorder | | `Balance.java` | height, two balance checks with visit counters | | `Problems.java` | interview problems 4 to 10 | | `Mistakes.java` | lost insert, delete that leaves a duplicate | | `TreePrinter.java`, `DeepTreeDemo.java` | tree outline; recursion on a 100,000-node chain | Tests: `DryRunTest` (transcripts 01-10), `ShapeTest` (11-14), `CrossCheckTest` (15-16). ## Captured output | File | What it shows | |---|---| | `01` to `05` | insert, search and the three delete cases, step by step | | `06-mistakes.txt` | lost insert, ignored return value, duplicate left by a half-finished delete | | `07-validate.txt` | children-only check fooled; int sentinels rejecting `Integer.MIN_VALUE` / `MAX_VALUE` | | `08` to `10` | kth smallest, LCA, floor and ceiling traces | | `11-insertion-order.txt` | height and average search cost for sorted, shuffled and middle-first insertion | | `12-treeset-sorted-input.txt` | comparisons in `java.util.TreeSet` fed sorted keys | | `13-balance-visits.txt` | node visits of the two balance checks, including the shape that hurts the top-down one | | `14-deep-chain.txt` | `StackOverflowError` from recursive search; the loop returns (depth depends on stack size) | | `15`, `16` | random operations against `TreeSet`; ten problems against brute force |