-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrb.fugue
38 lines (33 loc) · 1.07 KB
/
rb.fugue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
data Color = Red | Black;
data RBTree a = Leaf | Node Color (RBTree a) a (RBTree a);
insert' : RBTree a -> a -> (RBTree a, Bool);
insert' tree value =
case tree of
Leaf -> (Node Red Leaf value Leaf, True)
Node color left v right ->
if value < v
then let (left', added) = insert' left value
in (Node color left' v right, added)
else if value > v
then let (right', added) = insert' right value
in (Node color left v right', added)
else (tree, False);
fixColors : RBTree a -> RBTree a;
fixColors tree =
case tree of
Leaf -> Leaf
Node color left v right ->
let color' = if hasRedChild tree then Black else color
left' = fixColors left
right' = fixColors right
in Node color' left' v right';
insert : RBTree a -> a -> RBTree a;
insert tree value =
let i = insert' tree value
in fixColors (fst i);
hasRedChild : RBTree a -> Bool;
hasRedChild tree =
case tree of
Leaf -> False
Node color left v right ->
color == Red || hasRedChild left || hasRedChild right;