Add bst module: recursive and iterative insert/search, three delete cases, BST validation, kth smallest, LCA, floor/ceiling, balance check and 10 problems cross-checked against brute force (25 tests, 16 transcripts)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b6vDJ5fWY5uEZaJFNi
This commit is contained in:
Claude
2026-12-03 23:32:04 +05:30
parent 6e5764978f
commit 5419e5057d
36 changed files with 1712 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
Insert 50 30 70 20 40 60 80 35 65, one key at a time
====================================================
insert 50: -> becomes the root
insert 30: 50 L -> new left child of 50
insert 70: 50 R -> new right child of 50
insert 20: 50 L, 30 L -> new left child of 30
insert 40: 50 L, 30 R -> new right child of 30
insert 60: 50 R, 70 L -> new left child of 70
insert 80: 50 R, 70 R -> new right child of 70
insert 35: 50 L, 30 R, 40 L -> new left child of 40
insert 65: 50 R, 70 L, 60 R -> new right child of 60
insert 40 again: no change (set semantics), still 9 nodes
Tree (recursive and iterative insert give the same outline):
50
L: 30
L: 20
R: 40
L: 35
R: 70
L: 60
R: 65
R: 80
inorder: [20, 30, 35, 40, 50, 60, 65, 70, 80]
+15
View File
@@ -0,0 +1,15 @@
Search the sample tree
======================
search 65:
at 50: 65 > 50, go right
at 70: 65 < 70, go left
at 60: 65 > 60, go right
at 65: equal, found after 4 nodes
search 45:
at 50: 45 < 50, go left
at 30: 45 > 30, go right
at 40: 45 > 40, go right
null reached after 3 nodes: 45 is not in the tree
min = 20 (keep going left), max = 80 (keep going right)
+31
View File
@@ -0,0 +1,31 @@
Delete case 1: a leaf
=====================
before:
50
L: 30
L: 20
R: 40
L: 35
R: 70
L: 60
R: 65
R: 80
delete 20:
at 50: 20 < 50, go left
at 30: 20 < 30, go left
at 20: equal, found after 3 nodes
leaf: return null to the parent
after:
50
L: 30
R: 40
L: 35
R: 70
L: 60
R: 65
R: 80
inorder: [30, 35, 40, 50, 60, 65, 70, 80]
+31
View File
@@ -0,0 +1,31 @@
Delete case 2: a node with one child
====================================
before:
50
L: 30
L: 20
R: 40
L: 35
R: 70
L: 60
R: 65
R: 80
delete 60:
at 50: 60 > 50, go right
at 70: 60 < 70, go left
at 60: equal, found after 3 nodes
one child (65): return that child to the parent
after:
50
L: 30
L: 20
R: 40
L: 35
R: 70
L: 65
R: 80
inorder: [20, 30, 35, 40, 50, 65, 70, 80]
+30
View File
@@ -0,0 +1,30 @@
Delete case 3: a node with two children (the root)
==================================================
before:
50
L: 30
L: 20
R: 40
L: 35
R: 70
L: 60
R: 65
R: 80
delete 50:
at 50: equal, found after 1 nodes
two children: successor = smallest key in right subtree = 60
copy 60 into the node, then delete 60 from the right subtree
after:
60
L: 30
L: 20
R: 40
L: 35
R: 70
L: 65
R: 80
inorder: [20, 30, 35, 40, 60, 65, 70, 80]
+13
View File
@@ -0,0 +1,13 @@
Two broken variants, run
========================
insertLost(root, 45), then search 45: false
inorder: [20, 30, 35, 40, 50, 60, 65, 70, 80]
tree 50,70: Delete.recursive(root, 50) with the result thrown away
inorder from the old root variable: [50, 70]
root = Delete.recursive(root, 50): [70]
deleteLeavesDuplicate(sample, 50)
inorder: [20, 30, 35, 40, 60, 60, 65, 70, 80]
isBst: false
+24
View File
@@ -0,0 +1,24 @@
Validate BST: the children-only check, the bounds check, the sentinel trap
==========================================================================
A tree where 55 sits in the LEFT subtree of 50:
50
L: 30
L: 20
R: 55
R: 70
L: 60
R: 80
childrenOnly: true
isBst: false
byInorder: false inorder = [20, 30, 55, 50, 60, 70, 80]
Bounds trace (stops at the first violation):
node 50 allowed (-inf, +inf) ok
node 30 allowed (-inf, 50) ok
node 20 allowed (-inf, 30) ok
node 55 allowed (30, 50) VIOLATION
single node 2147483647: intSentinels=false isBst=true
single node -2147483648: intSentinels=false isBst=true
+14
View File
@@ -0,0 +1,14 @@
kth smallest, k = 4, on the sample tree: stack after each step
==============================================================
push 50 stack (top first) [50]
push 30 stack (top first) [30, 50]
push 20 stack (top first) [20, 30, 50]
pop 20 stack (top first) [30, 50] k left 3
pop 30 stack (top first) [50] k left 2
push 40 stack (top first) [40, 50]
push 35 stack (top first) [35, 40, 50]
pop 35 stack (top first) [40, 50] k left 1
pop 40 stack (top first) [50] k left 0
answer: 40
+14
View File
@@ -0,0 +1,14 @@
Lowest common ancestor on the sample tree
=========================================
lca(20, 35):
at 50: both smaller, go left
at 30: paths split here, answer 30
lca(35, 65):
at 50: paths split here, answer 50
lca(60, 65):
at 50: both larger, go right
at 70: both smaller, go left
at 60: paths split here, answer 60
lca(20, 99), with 99 absent, returns 50 (a wrong-looking answer: check presence first)
+15
View File
@@ -0,0 +1,15 @@
floor and ceiling of 45 on the sample tree: the best candidate so far
=====================================================================
floor(45):
at 50: too big, best = none, go left
at 30: candidate, best = 30, go right
at 40: candidate, best = 40, go right
answer 40
ceiling(45):
at 50: candidate, best = 50, go left
at 30: too small, best = 50, go right
at 40: too small, best = 50, go right
answer 50
floor(10) = OptionalInt.empty, ceiling(90) = OptionalInt.empty
+11
View File
@@ -0,0 +1,11 @@
Same keys, different insertion order (n = 1023; shuffles seeded 1..5)
=====================================================================
order height avg nodes per search
sorted 1..1023 1023 512.00
shuffled, seed 1 24 11.58
shuffled, seed 2 19 11.47
shuffled, seed 3 20 11.81
shuffled, seed 4 23 12.15
shuffled, seed 5 25 12.98
middle-first (fromSorted) 10 9.01
+5
View File
@@ -0,0 +1,5 @@
java.util.TreeSet fed sorted keys, comparisons counted by its Comparator
========================================================================
n = 1023 contains(): worst 18 comparisons, average 9.51 (plain BST on the same input: worst 1023, average 512.0)
n = 100000 contains(): worst 31 comparisons, average 16.10 (plain BST on the same input: worst 100000, average 50000.5)
+13
View File
@@ -0,0 +1,13 @@
isBalanced: height recomputed at every node versus one postorder pass (node visits)
===================================================================================
sample tree: height 4, balanced true
tree balanced top-down one pass
perfect, n = 1023 true 8194 1023
perfect, n = 16383 true 196610 16383
chain, n = 1000 false 999 1000
chain, n = 2000 false 1999 2000
comb 100, n = 5050 false 171603 106
comb 200, n = 20100 false 1353203 206
comb 400, n = 80200 false 10746403 406
+6
View File
@@ -0,0 +1,6 @@
Recursion depth equals tree height
==================================
chain of 100000 nodes, 512 KB thread stack
Search.recursive: threw java.lang.StackOverflowError
Search.iterative: returned true
+6
View File
@@ -0,0 +1,6 @@
insert, delete, search, floor, ceiling against java.util.TreeSet (seed 42)
==========================================================================
20000 random steps (10028 inserts, 9972 deletes), checked after every step:
inorder equals the TreeSet, isBst is true, recursive and iterative trees have identical shape,
search, floor and ceiling give the TreeSet's answers
+13
View File
@@ -0,0 +1,13 @@
Ten problems, each against a brute force on random trees (seeded)
=================================================================
1. validate BST: 20000 random binary trees, 7382 were BSTs; bounds and inorder agreed on all; childrenOnly wrongly accepted 417
2. kth smallest: 27427 (tree, k) cases against sorting all keys
3. lowest common ancestor: 18990 pairs against comparing the two root paths
4. range sum: 2000 cases against filtering all keys; sample tree [35, 50] = 125
5. sorted array to balanced BST: every n from 0 to 500 is a balanced BST of minimum height with the same keys
6. inorder successor: 2000 cases against scanning all keys; successor(40) in the sample tree = 50
7. two keys summing to a target: 2000 cases (359 true) against all pairs
8. minimum difference: 1729 trees against all pairs; MIN_VALUE and MAX_VALUE give 4294967295 (needs long)
9. recover two swapped keys: 1834 damaged trees, all restored
10. trim to a range: 2000 cases against filtering all keys, result still a BST; sample trimmed to [35, 65] = [35, 40, 50, 60, 65]