Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix depth-preserving left subtree insertion #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions avl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ module RawAVLTree = struct
| Same -> Deeper (Tree (t', tv, r, More))
| Less -> PSameDepth (Tree (t', tv, r, Same))
end
| PSameDepth t' -> PSameDepth (Tree (t', v, r, diff))
| PSameDepth t' -> PSameDepth (Tree (t', tv, r, diff))
end
| Equal -> PSameDepth t
| GreaterThan -> begin
Expand All @@ -119,6 +119,7 @@ module RawAVLTree = struct

let rec remove_min_elt : type d. ('a, d s) atree -> ('a, d s) neg_result = function
| Tree (Empty, _, r, Less) -> Shallower r
| Tree (Empty, _, r, Same) -> Shallower r
| Tree (l, tv, r, Less) -> begin
match l with
| Empty -> raise Empty_tree
Expand All @@ -145,14 +146,11 @@ module RawAVLTree = struct
| NSameDepth t -> NSameDepth (Tree (t, tv, r, More))
| Shallower t -> Shallower (Tree (t, tv, r, Same))
end
| Tree (l, tv, r, Same) -> begin
match l with
| Empty -> raise Empty_tree
| Tree _ as l ->
let result = remove_min_elt l in
match result with
| NSameDepth t -> NSameDepth (Tree (t, tv, r, Same))
| Shallower t -> NSameDepth (Tree (t, tv, r, Less))
| Tree (Tree(_) as l, tv, r, Same) -> begin
let result = remove_min_elt l in
match result with
| NSameDepth t -> NSameDepth (Tree (t, tv, r, Same))
| Shallower t -> NSameDepth (Tree (t, tv, r, Less))
end

let merge : type m n o. ('a, m) atree -> ('a, n) atree -> (m, n, o) diff -> ('a, o) pos_result =
Expand Down