Created
June 22, 2018 23:21
-
-
Save kennetpostigo/5c0b079f9b56a41a35ff4a4c5ba2c401 to your computer and use it in GitHub Desktop.
Reason BST Blog Post remove
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rec remove = (tree, compare, v) => | |
switch (tree) { | |
| Empty => Empty | |
| Node({value, left: Empty, right: Empty}) => | |
compare(v, value) == 0 ? Empty : tree | |
| Node({value, left: Node(_) as left, right: Empty as right}) => | |
compare(v, value) == 0 ? | |
left : Node({value, left: remove(left, compare, v), right}) | |
| Node({value, left: Empty as left, right: Node(_) as right}) => | |
compare(v, value) == 0 ? | |
right : Node({value, left, right: remove(right, compare, v)}) | |
| Node({value, left: Node(_) as left, right: Node(_) as right}) => | |
if (compare(v, value) == (-1)) { | |
Node({value, left: remove(left, compare, v), right}); | |
} else if (compare(v, value) == 1) { | |
Node({value, left, right: remove(right, compare, v)}); | |
} else { | |
switch (min(right)) { | |
| Empty => tree | |
| Node(a) => | |
Node({value: a.value, left, right: remove(right, compare, a.value)}) | |
}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment