amend merge function from persistent-vector.sml. If we reach the same depth and find that there is no space, then climb (using the stack) up by one depth, raise the depth of the smaller node by 1, and merge there. This helps ensure balance. Before, we broke a LEAF into two LEAF objects if we wanted to merge and found that there was no room, but that led to balancing problems, which are addressed by this change.

This commit is contained in:
2026-02-05 23:54:31 +00:00
parent 0f907769e3
commit c856f49a6c
2 changed files with 134 additions and 119 deletions

View File

@@ -488,6 +488,24 @@ struct
in
Expect.isTrue (outputList = expectedOutput)
end)
, test
"maintains balance with all leaves at same depth \
\when deleting a large portion of nodes in the middle"
(fn _ =>
let
(* arrange *)
val inputList = List.tabulate (228, fn i =>
{start = i, finish = i})
val pv = PersistentVector.fromList inputList
(* act *)
val pv = PersistentVector.delete (19, 15, pv)
(* assert *)
val isBalanced = PersistentVector.allLeavesAtSameDepth pv
in
Expect.isTrue isBalanced
end)
]
val tests = [appendTests, toListTests, splitLeftTests, deleteTests]