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:
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user