add a function to persistent-vector.sml to check if all leaves are at the same depth (which is how we want to balance)
This commit is contained in:
@@ -711,10 +711,18 @@ struct
|
|||||||
if leftDepth = rightDepth then
|
if leftDepth = rightDepth then
|
||||||
mergeSameDepth (left, right)
|
mergeSameDepth (left, right)
|
||||||
else if leftDepth < rightDepth then
|
else if leftDepth < rightDepth then
|
||||||
|
let
|
||||||
|
val targetDepth = rightDepth - leftDepth
|
||||||
|
in
|
||||||
mergeWhenRightDepthIsGreater (left, right, leftDepth, 0)
|
mergeWhenRightDepthIsGreater (left, right, leftDepth, 0)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
|
let
|
||||||
|
val targetDepth = leftDepth - rightDepth
|
||||||
|
in
|
||||||
mergeWhenLeftDepthIsGreater (left, right, rightDepth, 0)
|
mergeWhenLeftDepthIsGreater (left, right, rightDepth, 0)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
fun delete (start, length, tree) =
|
fun delete (start, length, tree) =
|
||||||
if isEmpty tree then
|
if isEmpty tree then
|
||||||
@@ -827,6 +835,30 @@ struct
|
|||||||
end
|
end
|
||||||
|
|
||||||
(* functions only for testing *)
|
(* functions only for testing *)
|
||||||
|
fun allLeavesAtSameDepth tree =
|
||||||
|
case tree of
|
||||||
|
BRANCH (nodes, _) =>
|
||||||
|
let
|
||||||
|
fun loop (pos, expectedDepth) =
|
||||||
|
if pos = Vector.length nodes then
|
||||||
|
true
|
||||||
|
else
|
||||||
|
let
|
||||||
|
val node = Vector.sub (nodes, pos)
|
||||||
|
val nodeDepth = countDepth node
|
||||||
|
in
|
||||||
|
if nodeDepth = expectedDepth then
|
||||||
|
loop (pos + 1, expectedDepth)
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
val expectedDepth = countDepth (Vector.sub (nodes, 0))
|
||||||
|
in
|
||||||
|
loop (0, expectedDepth)
|
||||||
|
end
|
||||||
|
| LEAF _ => true
|
||||||
|
|
||||||
fun fromListLoop (lst, acc) =
|
fun fromListLoop (lst, acc) =
|
||||||
case lst of
|
case lst of
|
||||||
{start, finish} :: tl =>
|
{start, finish} :: tl =>
|
||||||
|
|||||||
Reference in New Issue
Block a user