Skip to content

Instantly share code, notes, and snippets.

@kennetpostigo
Created June 22, 2018 23:21
Show Gist options
  • Save kennetpostigo/5c0b079f9b56a41a35ff4a4c5ba2c401 to your computer and use it in GitHub Desktop.
Save kennetpostigo/5c0b079f9b56a41a35ff4a4c5ba2c401 to your computer and use it in GitHub Desktop.
Reason BST Blog Post remove
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