add lecture 08

This commit is contained in:
SowinskiBraeden committed 2026-03-10 13:58:16 -07:00
1 parent 7c3198fee4
commit 9b62c01175
4 files changed
+133

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
type color = R | B
type 'a rbtree = L | N of color * 'a rbtree * 'a * 'a rbtree
let balance = function
| B, N (R, N (R, a, x, b), y, c), z, d
| B, N (R, a, x, N (R, b, y, c)), z, d
| B, a, x, N (R, N (R, b, y, c), z, d)
| B, a, x, N (R, b, y, N (R, c, z, d)) ->
N (R, N (B, a, x, b), y, N (B, c, z, d))
| c, l, x, r ->
N (c, l, x, r)
let insert x t =
let rec ins = function
| L -> N (R, L, x, L)
| N (c, l, y, r) when x < y ->
balance (c, ins l, x, r)
| N (c, l, y, r) when x > y ->
balance (c, l, x, ins r)
| _ -> t
in
ins t