Created
August 21, 2017 06:15
-
-
Save kunigami/d3749e634308c39f8dae42f7ab36ca2c to your computer and use it in GitHub Desktop.
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 updateTree index newElement tree size = match tree with | |
| Leaf element -> Leaf newElement | |
| Node {element; left; right} -> | |
let newSize = ((size + 1) / 2) - 1 in | |
if index == 0 then Node {element = newElement; left; right} | |
else if index <= newSize then | |
Node { | |
element; | |
left = updateTree (index - 1) newElement left newSize; | |
right; | |
} | |
else | |
Node { | |
element; | |
left; | |
right = updateTree (index - newSize - 1) newElement right newSize; | |
} | |
;; | |
let rec update index element ls = match ls with | |
| [] -> raise IndexOutOfBoundsException | |
| {size; tree} :: rest -> if index < size | |
then {size; tree = updateTree index element tree size} :: rest | |
else {size; tree} :: (update (index - size) element rest) | |
;; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment