-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbtree.py
159 lines (123 loc) · 3.73 KB
/
rbtree.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""
Rewrite from
https://github.com/koka-lang/koka/blob/master/test/bench/koka/rbtree.kk
"""
from __future__ import annotations
import sys
from dataclasses import dataclass
from typing import Callable, Literal, TypeVar
sys.setrecursionlimit(10000000)
Color = Literal["Red", "Black"]
@dataclass
class Node:
color: Color
left: tree
key: int
value: bool
right: tree
@dataclass
class Leaf:
...
tree = Node | Leaf
def is_red(t: tree) -> bool:
match t:
case Node("Red"):
return True
case _:
return False
def balance_left(l: tree, k: int, v: bool, r: tree) -> tree:
match l:
case Node(_, Node("Red", lx, kx, vx, rx), ky, vy, ry):
return Node(
"Red",
Node("Black", lx, kx, vx, rx),
ky,
vy,
Node("Black", ry, k, v, r),
)
case Node(_, ly, ky, vy, Node("Red", lx, kx, vx, rx)):
return Node(
"Red",
Node("Black", ly, ky, vy, lx),
kx,
vx,
Node("Black", rx, k, v, r),
)
case Node(_, lx, kx, vx, rx):
return Node("Black", Node("Red", lx, kx, vx, rx), k, v, r)
case Leaf():
return Leaf()
def balance_right(l: tree, k: int, v: bool, r: tree) -> tree:
match r:
case Node(_, Node("Red", lx, kx, vx, rx), ky, vy, ry):
return Node(
"Red",
Node("Black", l, k, v, lx),
kx,
vx,
Node("Black", rx, ky, vy, ry),
)
case Node(_, lx, kx, vx, Node("Red", ly, ky, vy, ry)):
return Node(
"Red",
Node("Black", l, k, v, lx),
kx,
vx,
Node("Black", ly, ky, vy, ry),
)
case Node(_, lx, kx, vx, rx):
return Node("Black", l, k, v, Node("Red", lx, kx, vx, rx))
case Leaf():
return Leaf()
def ins(t: tree, k: int, v: bool) -> tree:
match t:
case Node("Red", l, kx, vx, r):
if k < kx:
return Node("Red", ins(l, k, v), kx, vx, r)
elif k > kx:
return Node("Red", l, kx, vx, ins(r, k, v))
else:
return Node("Red", l, k, v, r)
case Node("Black", l, kx, vx, r):
if k < kx:
if is_red(l):
return balance_left(ins(l, k, v), kx, vx, r)
else:
return Node("Black", ins(l, k, v), kx, vx, r)
elif k > kx:
if is_red(r):
return balance_right(l, kx, vx, ins(r, k, v))
else:
return Node("Black", l, kx, vx, ins(r, k, v))
else:
return Node("Black", l, k, v, r)
case Leaf():
return Node("Red", Leaf(), k, v, Leaf())
def set_black(t: tree) -> tree:
match t:
case Node(_, l, k, v, r):
return Node("Black", l, k, v, r)
case _:
return t
def insert(t: tree, k: int, v: bool) -> tree:
return set_black(ins(t, k, v))
a = TypeVar("a")
def fold(t: tree, b: a, f: Callable[[int, bool, a], a]) -> a:
match t:
case Node(_, l, k, v, r):
return fold(r, f(k, v, fold(l, b, f)), f)
case Leaf():
return b
def make_tree_aux(n: int, t: tree) -> tree:
if n <= 0:
return t
n1 = n - 1
return make_tree_aux(n - 1, insert(t, n1, n1 % 10 == 0))
def make_tree(n: int) -> tree:
return make_tree_aux(n, Leaf())
def main():
t = make_tree(13)
print(t)
v = fold(t, 0, lambda k, v, a: a + 1 if v else a)
print(v)
main()