arborikit 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- arborikit/__init__.py +26 -0
- arborikit/bst.py +275 -0
- arborikit/general_tree.py +184 -0
- arborikit/rbtree.py +466 -0
- arborikit-0.1.0.dist-info/METADATA +78 -0
- arborikit-0.1.0.dist-info/RECORD +9 -0
- arborikit-0.1.0.dist-info/WHEEL +5 -0
- arborikit-0.1.0.dist-info/licenses/LICENSE +21 -0
- arborikit-0.1.0.dist-info/top_level.txt +1 -0
arborikit/__init__.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""
|
|
2
|
+
arborikit — arbres équilibrés et non équilibrés, sans aucune dépendance.
|
|
3
|
+
|
|
4
|
+
Trois structures, une API cohérente :
|
|
5
|
+
- GeneralTree : arbre généraliste (n-aire), nœuds dynamiques
|
|
6
|
+
- BST : arbre binaire de recherche NON équilibré
|
|
7
|
+
- RBTree : arbre rouge-noir (ARN), équilibré, avec invariants vérifiables
|
|
8
|
+
|
|
9
|
+
Zéro dépendance d'exécution. Python 3.8+.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from .general_tree import GeneralTree, GeneralNode
|
|
15
|
+
from .bst import BST
|
|
16
|
+
from .rbtree import RBTree, Color
|
|
17
|
+
|
|
18
|
+
__version__ = "0.1.0"
|
|
19
|
+
__all__ = [
|
|
20
|
+
"GeneralTree",
|
|
21
|
+
"GeneralNode",
|
|
22
|
+
"BST",
|
|
23
|
+
"RBTree",
|
|
24
|
+
"Color",
|
|
25
|
+
"__version__",
|
|
26
|
+
]
|
arborikit/bst.py
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
"""Arbre binaire de recherche NON équilibré (ABR / BST).
|
|
2
|
+
|
|
3
|
+
Propriété : pour tout nœud, sous-arbre gauche < nœud < sous-arbre droit.
|
|
4
|
+
Complexités : O(log n) en moyenne, O(n) au pire (insertion triée).
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from typing import Any, Iterator, List, Optional
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class _Node:
|
|
13
|
+
__slots__ = ("key", "left", "right")
|
|
14
|
+
|
|
15
|
+
def __init__(self, key: Any) -> None:
|
|
16
|
+
self.key = key
|
|
17
|
+
self.left: Optional[_Node] = None
|
|
18
|
+
self.right: Optional[_Node] = None
|
|
19
|
+
|
|
20
|
+
def __repr__(self) -> str:
|
|
21
|
+
return f"_Node({self.key!r})"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class BST:
|
|
25
|
+
"""ABR non équilibré, doublons ignorés."""
|
|
26
|
+
|
|
27
|
+
__slots__ = ("_root", "_size")
|
|
28
|
+
|
|
29
|
+
def __init__(self, items: Optional[Any] = None) -> None:
|
|
30
|
+
self._root: Optional[_Node] = None
|
|
31
|
+
self._size = 0
|
|
32
|
+
if items is not None:
|
|
33
|
+
for x in items:
|
|
34
|
+
self.insert(x)
|
|
35
|
+
|
|
36
|
+
# -- état ----------------------------------------------------
|
|
37
|
+
def __len__(self) -> int:
|
|
38
|
+
return self._size
|
|
39
|
+
|
|
40
|
+
def __bool__(self) -> bool:
|
|
41
|
+
return self._root is not None
|
|
42
|
+
|
|
43
|
+
def __contains__(self, key: Any) -> bool:
|
|
44
|
+
return self.find(key) is not None
|
|
45
|
+
|
|
46
|
+
def __iter__(self) -> Iterator[Any]:
|
|
47
|
+
return iter(self.inorder())
|
|
48
|
+
|
|
49
|
+
@property
|
|
50
|
+
def size(self) -> int:
|
|
51
|
+
return self._size
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def is_empty(self) -> bool:
|
|
55
|
+
return self._root is None
|
|
56
|
+
|
|
57
|
+
# -- insertion -----------------------------------------------
|
|
58
|
+
def insert(self, key: Any) -> bool:
|
|
59
|
+
"""Insère key. Retourne False si déjà présent."""
|
|
60
|
+
if self._root is None:
|
|
61
|
+
self._root = _Node(key)
|
|
62
|
+
self._size = 1
|
|
63
|
+
return True
|
|
64
|
+
cur = self._root
|
|
65
|
+
while True:
|
|
66
|
+
if key == cur.key:
|
|
67
|
+
return False
|
|
68
|
+
if key < cur.key:
|
|
69
|
+
if cur.left is None:
|
|
70
|
+
cur.left = _Node(key)
|
|
71
|
+
self._size += 1
|
|
72
|
+
return True
|
|
73
|
+
cur = cur.left
|
|
74
|
+
else:
|
|
75
|
+
if cur.right is None:
|
|
76
|
+
cur.right = _Node(key)
|
|
77
|
+
self._size += 1
|
|
78
|
+
return True
|
|
79
|
+
cur = cur.right
|
|
80
|
+
|
|
81
|
+
# -- recherche -----------------------------------------------
|
|
82
|
+
def find(self, key: Any) -> Optional[_Node]:
|
|
83
|
+
cur = self._root
|
|
84
|
+
while cur is not None:
|
|
85
|
+
if key == cur.key:
|
|
86
|
+
return cur
|
|
87
|
+
cur = cur.left if key < cur.key else cur.right
|
|
88
|
+
return None
|
|
89
|
+
|
|
90
|
+
def min(self) -> Any:
|
|
91
|
+
if self._root is None:
|
|
92
|
+
raise ValueError("arbre vide")
|
|
93
|
+
cur = self._root
|
|
94
|
+
while cur.left is not None:
|
|
95
|
+
cur = cur.left
|
|
96
|
+
return cur.key
|
|
97
|
+
|
|
98
|
+
def max(self) -> Any:
|
|
99
|
+
if self._root is None:
|
|
100
|
+
raise ValueError("arbre vide")
|
|
101
|
+
cur = self._root
|
|
102
|
+
while cur.right is not None:
|
|
103
|
+
cur = cur.right
|
|
104
|
+
return cur.key
|
|
105
|
+
|
|
106
|
+
def successor(self, key: Any) -> Optional[Any]:
|
|
107
|
+
"""Plus petit élément strictement supérieur à key."""
|
|
108
|
+
cur, succ = self._root, None
|
|
109
|
+
while cur is not None:
|
|
110
|
+
if key < cur.key:
|
|
111
|
+
succ, cur = cur.key, cur.left
|
|
112
|
+
elif key > cur.key:
|
|
113
|
+
cur = cur.right
|
|
114
|
+
else:
|
|
115
|
+
if cur.right is not None:
|
|
116
|
+
m = cur.right
|
|
117
|
+
while m.left is not None:
|
|
118
|
+
m = m.left
|
|
119
|
+
return m.key
|
|
120
|
+
return succ
|
|
121
|
+
return succ
|
|
122
|
+
|
|
123
|
+
def predecessor(self, key: Any) -> Optional[Any]:
|
|
124
|
+
cur, pred = self._root, None
|
|
125
|
+
while cur is not None:
|
|
126
|
+
if key > cur.key:
|
|
127
|
+
pred, cur = cur.key, cur.right
|
|
128
|
+
elif key < cur.key:
|
|
129
|
+
cur = cur.left
|
|
130
|
+
else:
|
|
131
|
+
if cur.left is not None:
|
|
132
|
+
m = cur.left
|
|
133
|
+
while m.right is not None:
|
|
134
|
+
m = m.right
|
|
135
|
+
return m.key
|
|
136
|
+
return pred
|
|
137
|
+
return pred
|
|
138
|
+
|
|
139
|
+
# -- suppression ---------------------------------------------
|
|
140
|
+
def delete(self, key: Any) -> bool:
|
|
141
|
+
before = self._size
|
|
142
|
+
self._root = self._delete(self._root, key)
|
|
143
|
+
return self._size < before
|
|
144
|
+
|
|
145
|
+
def _delete(self, node: Optional[_Node], key: Any) -> Optional[_Node]:
|
|
146
|
+
if node is None:
|
|
147
|
+
return None
|
|
148
|
+
if key < node.key:
|
|
149
|
+
node.left = self._delete(node.left, key)
|
|
150
|
+
elif key > node.key:
|
|
151
|
+
node.right = self._delete(node.right, key)
|
|
152
|
+
else:
|
|
153
|
+
self._size -= 1
|
|
154
|
+
if node.left is None:
|
|
155
|
+
return node.right
|
|
156
|
+
if node.right is None:
|
|
157
|
+
return node.left
|
|
158
|
+
nxt = node.right
|
|
159
|
+
while nxt.left is not None:
|
|
160
|
+
nxt = nxt.left
|
|
161
|
+
node.key = nxt.key
|
|
162
|
+
node.right = self._delete(node.right, nxt.key)
|
|
163
|
+
self._size += 1 # annule le décrément du sous-appel
|
|
164
|
+
return node
|
|
165
|
+
|
|
166
|
+
def clear(self) -> None:
|
|
167
|
+
self._root = None
|
|
168
|
+
self._size = 0
|
|
169
|
+
|
|
170
|
+
# -- parcours ------------------------------------------------
|
|
171
|
+
def inorder(self) -> List[Any]:
|
|
172
|
+
"""Ordre croissant."""
|
|
173
|
+
out: List[Any] = []
|
|
174
|
+
stack, cur = [], self._root
|
|
175
|
+
while stack or cur is not None:
|
|
176
|
+
while cur is not None:
|
|
177
|
+
stack.append(cur)
|
|
178
|
+
cur = cur.left
|
|
179
|
+
cur = stack.pop()
|
|
180
|
+
out.append(cur.key)
|
|
181
|
+
cur = cur.right
|
|
182
|
+
return out
|
|
183
|
+
|
|
184
|
+
def preorder(self) -> List[Any]:
|
|
185
|
+
out: List[Any] = []
|
|
186
|
+
stack = [self._root] if self._root else []
|
|
187
|
+
while stack:
|
|
188
|
+
n = stack.pop()
|
|
189
|
+
out.append(n.key)
|
|
190
|
+
if n.right is not None:
|
|
191
|
+
stack.append(n.right)
|
|
192
|
+
if n.left is not None:
|
|
193
|
+
stack.append(n.left)
|
|
194
|
+
return out
|
|
195
|
+
|
|
196
|
+
def postorder(self) -> List[Any]:
|
|
197
|
+
out: List[Any] = []
|
|
198
|
+
stack = [self._root] if self._root else []
|
|
199
|
+
while stack:
|
|
200
|
+
n = stack.pop()
|
|
201
|
+
out.append(n.key)
|
|
202
|
+
if n.left is not None:
|
|
203
|
+
stack.append(n.left)
|
|
204
|
+
if n.right is not None:
|
|
205
|
+
stack.append(n.right)
|
|
206
|
+
return out[::-1]
|
|
207
|
+
|
|
208
|
+
def levelorder(self) -> List[Any]:
|
|
209
|
+
out: List[Any] = []
|
|
210
|
+
queue = [self._root] if self._root else []
|
|
211
|
+
i = 0
|
|
212
|
+
while i < len(queue):
|
|
213
|
+
n = queue[i]
|
|
214
|
+
i += 1
|
|
215
|
+
out.append(n.key)
|
|
216
|
+
if n.left is not None:
|
|
217
|
+
queue.append(n.left)
|
|
218
|
+
if n.right is not None:
|
|
219
|
+
queue.append(n.right)
|
|
220
|
+
return out
|
|
221
|
+
|
|
222
|
+
# -- mesures -------------------------------------------------
|
|
223
|
+
@property
|
|
224
|
+
def height(self) -> int:
|
|
225
|
+
"""Hauteur : arbre vide = -1, un nœud = 0."""
|
|
226
|
+
if self._root is None:
|
|
227
|
+
return -1
|
|
228
|
+
best, stack = 0, [(self._root, 0)]
|
|
229
|
+
while stack:
|
|
230
|
+
n, d = stack.pop()
|
|
231
|
+
best = max(best, d)
|
|
232
|
+
if n.left is not None:
|
|
233
|
+
stack.append((n.left, d + 1))
|
|
234
|
+
if n.right is not None:
|
|
235
|
+
stack.append((n.right, d + 1))
|
|
236
|
+
return best
|
|
237
|
+
|
|
238
|
+
@property
|
|
239
|
+
def is_degenerate(self) -> bool:
|
|
240
|
+
"""Vrai si l'arbre est une liste chaînée (hauteur == size-1)."""
|
|
241
|
+
return self._size > 0 and self.height == self._size - 1
|
|
242
|
+
|
|
243
|
+
# -- vérification --------------------------------------------
|
|
244
|
+
def validate(self) -> None:
|
|
245
|
+
"""Lève ValueError si la propriété ABR est violée."""
|
|
246
|
+
stack = [(self._root, None, None)]
|
|
247
|
+
while stack:
|
|
248
|
+
n, lo, hi = stack.pop()
|
|
249
|
+
if n is None:
|
|
250
|
+
continue
|
|
251
|
+
if lo is not None and not (lo < n.key):
|
|
252
|
+
raise ValueError(f"ABR violé : {n.key!r} <= {lo!r}")
|
|
253
|
+
if hi is not None and not (n.key < hi):
|
|
254
|
+
raise ValueError(f"ABR violé : {n.key!r} >= {hi!r}")
|
|
255
|
+
stack.append((n.left, lo, n.key))
|
|
256
|
+
stack.append((n.right, n.key, hi))
|
|
257
|
+
|
|
258
|
+
# -- affichage ------------------------------------------------
|
|
259
|
+
def __str__(self) -> str:
|
|
260
|
+
lines: List[str] = []
|
|
261
|
+
|
|
262
|
+
def rec(n: Optional[_Node], prefix: str, is_last: bool) -> None:
|
|
263
|
+
if n is None:
|
|
264
|
+
return
|
|
265
|
+
lines.append(prefix + ("└── " if is_last else "├── ") + str(n.key))
|
|
266
|
+
child_prefix = prefix + (" " if is_last else "│ ")
|
|
267
|
+
kids = [c for c in (n.left, n.right) if c is not None]
|
|
268
|
+
for i, c in enumerate(kids):
|
|
269
|
+
rec(c, child_prefix, i == len(kids) - 1)
|
|
270
|
+
|
|
271
|
+
rec(self._root, "", True)
|
|
272
|
+
return "\n".join(lines)
|
|
273
|
+
|
|
274
|
+
def __repr__(self) -> str:
|
|
275
|
+
return f"BST(size={self._size}, height={self.height})"
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"""Arbre généraliste (n-aire) : chaque nœud a 0..n enfants et un parent."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, Iterator, List, Optional
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class GeneralNode:
|
|
9
|
+
"""Nœud d'arbre généraliste. `children` est ordonné (insertion)."""
|
|
10
|
+
|
|
11
|
+
__slots__ = ("data", "parent", "children")
|
|
12
|
+
|
|
13
|
+
def __init__(self, data: Any, parent: Optional["GeneralNode"] = None) -> None:
|
|
14
|
+
self.data = data
|
|
15
|
+
self.parent = parent
|
|
16
|
+
self.children: List[GeneralNode] = []
|
|
17
|
+
|
|
18
|
+
def add_child(self, data: Any) -> "GeneralNode":
|
|
19
|
+
child = GeneralNode(data, self)
|
|
20
|
+
self.children.append(child)
|
|
21
|
+
return child
|
|
22
|
+
|
|
23
|
+
def add_node(self, node: "GeneralNode") -> "GeneralNode":
|
|
24
|
+
"""Rattache un nœud existant (détaché d'abord de son parent)."""
|
|
25
|
+
if node.parent is not None:
|
|
26
|
+
node.parent.children.remove(node)
|
|
27
|
+
node.parent = self
|
|
28
|
+
self.children.append(node)
|
|
29
|
+
return node
|
|
30
|
+
|
|
31
|
+
def remove_child(self, node: "GeneralNode") -> bool:
|
|
32
|
+
try:
|
|
33
|
+
self.children.remove(node)
|
|
34
|
+
except ValueError:
|
|
35
|
+
return False
|
|
36
|
+
node.parent = None
|
|
37
|
+
return True
|
|
38
|
+
|
|
39
|
+
@property
|
|
40
|
+
def is_leaf(self) -> bool:
|
|
41
|
+
return not self.children
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def is_root(self) -> bool:
|
|
45
|
+
return self.parent is None
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def depth(self) -> int:
|
|
49
|
+
"""Profondeur : racine = 0."""
|
|
50
|
+
d, cur = 0, self
|
|
51
|
+
while cur.parent is not None:
|
|
52
|
+
d += 1
|
|
53
|
+
cur = cur.parent
|
|
54
|
+
return d
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def height(self) -> int:
|
|
58
|
+
"""Hauteur du sous-arbre : feuille = 0."""
|
|
59
|
+
if not self.children:
|
|
60
|
+
return 0
|
|
61
|
+
return 1 + max(c.height for c in self.children)
|
|
62
|
+
|
|
63
|
+
@property
|
|
64
|
+
def subtree_size(self) -> int:
|
|
65
|
+
return 1 + sum(c.subtree_size for c in self.children)
|
|
66
|
+
|
|
67
|
+
def path_from_root(self) -> List[Any]:
|
|
68
|
+
path, cur = [], self
|
|
69
|
+
while cur is not None:
|
|
70
|
+
path.append(cur.data)
|
|
71
|
+
cur = cur.parent
|
|
72
|
+
return path[::-1]
|
|
73
|
+
|
|
74
|
+
def __iter__(self) -> Iterator["GeneralNode"]:
|
|
75
|
+
return iter(self.children)
|
|
76
|
+
|
|
77
|
+
def __repr__(self) -> str:
|
|
78
|
+
return f"GeneralNode({self.data!r})"
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class GeneralTree:
|
|
82
|
+
"""Arbre généraliste enveloppant un nœud racine."""
|
|
83
|
+
|
|
84
|
+
def __init__(self, root_data: Any) -> None:
|
|
85
|
+
self.root = GeneralNode(root_data)
|
|
86
|
+
|
|
87
|
+
# -- mesures -------------------------------------------------
|
|
88
|
+
def __len__(self) -> int:
|
|
89
|
+
return self.root.subtree_size
|
|
90
|
+
|
|
91
|
+
def __bool__(self) -> bool:
|
|
92
|
+
return True
|
|
93
|
+
|
|
94
|
+
@property
|
|
95
|
+
def size(self) -> int:
|
|
96
|
+
return len(self)
|
|
97
|
+
|
|
98
|
+
@property
|
|
99
|
+
def height(self) -> int:
|
|
100
|
+
return self.root.height
|
|
101
|
+
|
|
102
|
+
# -- parcours ------------------------------------------------
|
|
103
|
+
def preorder(self) -> List[Any]:
|
|
104
|
+
"""Nœud puis enfants."""
|
|
105
|
+
out: List[Any] = []
|
|
106
|
+
stack = [self.root]
|
|
107
|
+
while stack:
|
|
108
|
+
n = stack.pop()
|
|
109
|
+
out.append(n.data)
|
|
110
|
+
stack.extend(reversed(n.children))
|
|
111
|
+
return out
|
|
112
|
+
|
|
113
|
+
def postorder(self) -> List[Any]:
|
|
114
|
+
"""Enfants puis nœud."""
|
|
115
|
+
out: List[Any] = []
|
|
116
|
+
root = self.root
|
|
117
|
+
|
|
118
|
+
def rec(n: GeneralNode) -> None:
|
|
119
|
+
for c in n.children:
|
|
120
|
+
rec(c)
|
|
121
|
+
out.append(n.data)
|
|
122
|
+
|
|
123
|
+
rec(root)
|
|
124
|
+
return out
|
|
125
|
+
|
|
126
|
+
def levelorder(self) -> List[Any]:
|
|
127
|
+
"""BFS, niveau par niveau."""
|
|
128
|
+
out: List[Any] = []
|
|
129
|
+
queue = [self.root]
|
|
130
|
+
i = 0
|
|
131
|
+
while i < len(queue):
|
|
132
|
+
n = queue[i]
|
|
133
|
+
i += 1
|
|
134
|
+
out.append(n.data)
|
|
135
|
+
queue.extend(n.children)
|
|
136
|
+
return out
|
|
137
|
+
|
|
138
|
+
def leaves(self) -> List[Any]:
|
|
139
|
+
return [n.data for n in self._walk() if n.is_leaf]
|
|
140
|
+
|
|
141
|
+
def _walk(self) -> Iterator[GeneralNode]:
|
|
142
|
+
stack = [self.root]
|
|
143
|
+
while stack:
|
|
144
|
+
n = stack.pop()
|
|
145
|
+
yield n
|
|
146
|
+
stack.extend(reversed(n.children))
|
|
147
|
+
|
|
148
|
+
# -- recherche ----------------------------------------------
|
|
149
|
+
def find(self, data: Any) -> Optional[GeneralNode]:
|
|
150
|
+
for n in self._walk():
|
|
151
|
+
if n.data == data:
|
|
152
|
+
return n
|
|
153
|
+
return None
|
|
154
|
+
|
|
155
|
+
# -- édition -------------------------------------------------
|
|
156
|
+
def add(self, parent_data: Any, data: Any) -> GeneralNode:
|
|
157
|
+
"""Raccourci : ajoute `data` sous le nœud contenant `parent_data`."""
|
|
158
|
+
p = self.find(parent_data)
|
|
159
|
+
if p is None:
|
|
160
|
+
raise KeyError(f"nœud parent introuvable : {parent_data!r}")
|
|
161
|
+
return p.add_child(data)
|
|
162
|
+
|
|
163
|
+
def prune(self, data: Any) -> bool:
|
|
164
|
+
"""Supprime le nœud et tout son sous-arbre."""
|
|
165
|
+
n = self.find(data)
|
|
166
|
+
if n is None or n.parent is None:
|
|
167
|
+
return False
|
|
168
|
+
return n.parent.remove_child(n)
|
|
169
|
+
|
|
170
|
+
# -- affichage ------------------------------------------------
|
|
171
|
+
def __str__(self) -> str:
|
|
172
|
+
lines: List[str] = []
|
|
173
|
+
|
|
174
|
+
def rec(n: GeneralNode, prefix: str, is_last: bool) -> None:
|
|
175
|
+
lines.append(prefix + ("└── " if is_last else "├── ") + str(n.data))
|
|
176
|
+
child_prefix = prefix + (" " if is_last else "│ ")
|
|
177
|
+
for i, c in enumerate(n.children):
|
|
178
|
+
rec(c, child_prefix, i == len(n.children) - 1)
|
|
179
|
+
|
|
180
|
+
rec(self.root, "", True)
|
|
181
|
+
return "\n".join(lines)
|
|
182
|
+
|
|
183
|
+
def __repr__(self) -> str:
|
|
184
|
+
return f"GeneralTree({self.root.data!r}, size={len(self)})"
|
arborikit/rbtree.py
ADDED
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
"""Arbre rouge-noir (ARN / Red-Black Tree) — implémentation CLRS complète.
|
|
2
|
+
|
|
3
|
+
Contrairement à un dictionnaire trié opaque, cette implémentation expose
|
|
4
|
+
les nœuds, les couleurs, les rotations et permet de vérifier les 5 invariants.
|
|
5
|
+
|
|
6
|
+
Invariants :
|
|
7
|
+
1. chaque nœud est rouge ou noir
|
|
8
|
+
2. la racine est noire
|
|
9
|
+
3. toute feuille (NIL) est noire
|
|
10
|
+
4. un nœud rouge a deux enfants noirs (pas de rouge-rouge)
|
|
11
|
+
5. tout chemin racine→feuille a le même nombre de nœuds noirs
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
from enum import Enum
|
|
17
|
+
from typing import Any, Iterator, List, Optional
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class Color(Enum):
|
|
21
|
+
RED = "R"
|
|
22
|
+
BLACK = "N"
|
|
23
|
+
|
|
24
|
+
def __str__(self) -> str: # pragma: no cover - cosmétique
|
|
25
|
+
return self.value
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class RBNode:
|
|
29
|
+
"""Nœud rouge-noir exposé (couleur lisible, parent accessible)."""
|
|
30
|
+
|
|
31
|
+
__slots__ = ("key", "left", "right", "parent", "color")
|
|
32
|
+
|
|
33
|
+
def __init__(self, key: Any = None, color: Color = Color.BLACK) -> None:
|
|
34
|
+
self.key = key
|
|
35
|
+
self.left: RBNode = self
|
|
36
|
+
self.right: RBNode = self
|
|
37
|
+
self.parent: Optional[RBNode] = None
|
|
38
|
+
self.color = color
|
|
39
|
+
|
|
40
|
+
def __repr__(self) -> str:
|
|
41
|
+
return f"{self.key!r}({self.color.value})"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class RBTree:
|
|
45
|
+
"""ARN avec sentinelle NIL, doublons ignorés."""
|
|
46
|
+
|
|
47
|
+
__slots__ = ("_nil", "_root", "_size")
|
|
48
|
+
|
|
49
|
+
def __init__(self, items: Optional[Any] = None) -> None:
|
|
50
|
+
self._nil: RBNode = RBNode(None, Color.BLACK)
|
|
51
|
+
self._nil.left = self._nil
|
|
52
|
+
self._nil.right = self._nil
|
|
53
|
+
self._nil.parent = None
|
|
54
|
+
self._root = self._nil
|
|
55
|
+
self._size = 0
|
|
56
|
+
if items is not None:
|
|
57
|
+
for x in items:
|
|
58
|
+
self.insert(x)
|
|
59
|
+
|
|
60
|
+
# -- état ----------------------------------------------------
|
|
61
|
+
def __len__(self) -> int:
|
|
62
|
+
return self._size
|
|
63
|
+
|
|
64
|
+
def __bool__(self) -> bool:
|
|
65
|
+
return self._root is not self._nil
|
|
66
|
+
|
|
67
|
+
def __contains__(self, key: Any) -> bool:
|
|
68
|
+
return self.find(key) is not None
|
|
69
|
+
|
|
70
|
+
def __iter__(self) -> Iterator[Any]:
|
|
71
|
+
return iter(self.inorder())
|
|
72
|
+
|
|
73
|
+
@property
|
|
74
|
+
def size(self) -> int:
|
|
75
|
+
return self._size
|
|
76
|
+
|
|
77
|
+
@property
|
|
78
|
+
def is_empty(self) -> bool:
|
|
79
|
+
return self._root is self._nil
|
|
80
|
+
|
|
81
|
+
@property
|
|
82
|
+
def root(self) -> Optional[RBNode]:
|
|
83
|
+
"""Racine exposée (None si vide)."""
|
|
84
|
+
return None if self._root is self._nil else self._root
|
|
85
|
+
|
|
86
|
+
# -- rotations -----------------------------------------------
|
|
87
|
+
def _left_rotate(self, x: RBNode) -> None:
|
|
88
|
+
y = x.right
|
|
89
|
+
x.right = y.left
|
|
90
|
+
if y.left is not self._nil:
|
|
91
|
+
y.left.parent = x
|
|
92
|
+
y.parent = x.parent
|
|
93
|
+
if x.parent is None:
|
|
94
|
+
self._root = y
|
|
95
|
+
elif x is x.parent.left:
|
|
96
|
+
x.parent.left = y
|
|
97
|
+
else:
|
|
98
|
+
x.parent.right = y
|
|
99
|
+
y.left = x
|
|
100
|
+
x.parent = y
|
|
101
|
+
|
|
102
|
+
def _right_rotate(self, x: RBNode) -> None:
|
|
103
|
+
y = x.left
|
|
104
|
+
x.left = y.right
|
|
105
|
+
if y.right is not self._nil:
|
|
106
|
+
y.right.parent = x
|
|
107
|
+
y.parent = x.parent
|
|
108
|
+
if x.parent is None:
|
|
109
|
+
self._root = y
|
|
110
|
+
elif x is x.parent.right:
|
|
111
|
+
x.parent.right = y
|
|
112
|
+
else:
|
|
113
|
+
x.parent.left = y
|
|
114
|
+
y.right = x
|
|
115
|
+
x.parent = y
|
|
116
|
+
|
|
117
|
+
# -- insertion -----------------------------------------------
|
|
118
|
+
def insert(self, key: Any) -> bool:
|
|
119
|
+
"""Insère key (nœud rouge + fixup). False si doublon."""
|
|
120
|
+
nil = self._nil
|
|
121
|
+
y: Optional[RBNode] = None
|
|
122
|
+
x = self._root
|
|
123
|
+
while x is not nil:
|
|
124
|
+
y = x
|
|
125
|
+
if key == x.key:
|
|
126
|
+
return False
|
|
127
|
+
x = x.left if key < x.key else x.right
|
|
128
|
+
z = RBNode(key, Color.RED)
|
|
129
|
+
z.left = z.right = nil
|
|
130
|
+
z.parent = y
|
|
131
|
+
if y is None:
|
|
132
|
+
self._root = z
|
|
133
|
+
elif key < y.key:
|
|
134
|
+
y.left = z
|
|
135
|
+
else:
|
|
136
|
+
y.right = z
|
|
137
|
+
self._size += 1
|
|
138
|
+
self._insert_fixup(z)
|
|
139
|
+
return True
|
|
140
|
+
|
|
141
|
+
def _insert_fixup(self, z: RBNode) -> None:
|
|
142
|
+
red, black = Color.RED, Color.BLACK
|
|
143
|
+
while z.parent is not None and z.parent.color is red:
|
|
144
|
+
gp = z.parent.parent
|
|
145
|
+
if z.parent is gp.left:
|
|
146
|
+
y = gp.right # oncle
|
|
147
|
+
if y.color is red: # cas 1
|
|
148
|
+
z.parent.color = black
|
|
149
|
+
y.color = black
|
|
150
|
+
gp.color = red
|
|
151
|
+
z = gp
|
|
152
|
+
else:
|
|
153
|
+
if z is z.parent.right: # cas 2 : zigzag
|
|
154
|
+
z = z.parent
|
|
155
|
+
self._left_rotate(z)
|
|
156
|
+
z.parent.color = black # cas 3 : ligne
|
|
157
|
+
z.parent.parent.color = red
|
|
158
|
+
self._right_rotate(z.parent.parent)
|
|
159
|
+
else:
|
|
160
|
+
y = gp.left
|
|
161
|
+
if y.color is red:
|
|
162
|
+
z.parent.color = black
|
|
163
|
+
y.color = black
|
|
164
|
+
gp.color = red
|
|
165
|
+
z = gp
|
|
166
|
+
else:
|
|
167
|
+
if z is z.parent.left:
|
|
168
|
+
z = z.parent
|
|
169
|
+
self._right_rotate(z)
|
|
170
|
+
z.parent.color = black
|
|
171
|
+
z.parent.parent.color = red
|
|
172
|
+
self._left_rotate(z.parent.parent)
|
|
173
|
+
self._root.color = black
|
|
174
|
+
|
|
175
|
+
# -- recherche -----------------------------------------------
|
|
176
|
+
def find(self, key: Any) -> Optional[RBNode]:
|
|
177
|
+
nil = self._nil
|
|
178
|
+
x = self._root
|
|
179
|
+
while x is not nil:
|
|
180
|
+
if key == x.key:
|
|
181
|
+
return x
|
|
182
|
+
x = x.left if key < x.key else x.right
|
|
183
|
+
return None
|
|
184
|
+
|
|
185
|
+
def min(self) -> Any:
|
|
186
|
+
if self.is_empty:
|
|
187
|
+
raise ValueError("arbre vide")
|
|
188
|
+
return self._minimum(self._root).key
|
|
189
|
+
|
|
190
|
+
def max(self) -> Any:
|
|
191
|
+
if self.is_empty:
|
|
192
|
+
raise ValueError("arbre vide")
|
|
193
|
+
return self._maximum(self._root).key
|
|
194
|
+
|
|
195
|
+
def _minimum(self, x: RBNode) -> RBNode:
|
|
196
|
+
while x.left is not self._nil:
|
|
197
|
+
x = x.left
|
|
198
|
+
return x
|
|
199
|
+
|
|
200
|
+
def _maximum(self, x: RBNode) -> RBNode:
|
|
201
|
+
while x.right is not self._nil:
|
|
202
|
+
x = x.right
|
|
203
|
+
return x
|
|
204
|
+
|
|
205
|
+
def successor(self, key: Any) -> Optional[Any]:
|
|
206
|
+
nil = self._nil
|
|
207
|
+
x = self.find(key)
|
|
208
|
+
if x is None:
|
|
209
|
+
return None
|
|
210
|
+
if x.right is not nil:
|
|
211
|
+
return self._minimum(x.right).key
|
|
212
|
+
y = x.parent
|
|
213
|
+
while y is not None and x is y.right:
|
|
214
|
+
x, y = y, y.parent
|
|
215
|
+
return None if y is None else y.key
|
|
216
|
+
|
|
217
|
+
def predecessor(self, key: Any) -> Optional[Any]:
|
|
218
|
+
nil = self._nil
|
|
219
|
+
x = self.find(key)
|
|
220
|
+
if x is None:
|
|
221
|
+
return None
|
|
222
|
+
if x.left is not nil:
|
|
223
|
+
return self._maximum(x.left).key
|
|
224
|
+
y = x.parent
|
|
225
|
+
while y is not None and x is y.left:
|
|
226
|
+
x, y = y, y.parent
|
|
227
|
+
return None if y is None else y.key
|
|
228
|
+
|
|
229
|
+
# -- suppression ---------------------------------------------
|
|
230
|
+
def delete(self, key: Any) -> bool:
|
|
231
|
+
z = self.find(key)
|
|
232
|
+
if z is None:
|
|
233
|
+
return False
|
|
234
|
+
self._delete_node(z)
|
|
235
|
+
self._size -= 1
|
|
236
|
+
return True
|
|
237
|
+
|
|
238
|
+
def _transplant(self, u: RBNode, v: RBNode) -> None:
|
|
239
|
+
if u.parent is None:
|
|
240
|
+
self._root = v
|
|
241
|
+
elif u is u.parent.left:
|
|
242
|
+
u.parent.left = v
|
|
243
|
+
else:
|
|
244
|
+
u.parent.right = v
|
|
245
|
+
v.parent = u.parent
|
|
246
|
+
|
|
247
|
+
def _delete_node(self, z: RBNode) -> None:
|
|
248
|
+
nil = self._nil
|
|
249
|
+
y = z
|
|
250
|
+
y_original_color = y.color
|
|
251
|
+
if z.left is nil:
|
|
252
|
+
x = z.right
|
|
253
|
+
self._transplant(z, z.right)
|
|
254
|
+
elif z.right is nil:
|
|
255
|
+
x = z.left
|
|
256
|
+
self._transplant(z, z.left)
|
|
257
|
+
else:
|
|
258
|
+
y = self._minimum(z.right)
|
|
259
|
+
y_original_color = y.color
|
|
260
|
+
x = y.right
|
|
261
|
+
if y.parent is z:
|
|
262
|
+
x.parent = y
|
|
263
|
+
else:
|
|
264
|
+
self._transplant(y, y.right)
|
|
265
|
+
y.right = z.right
|
|
266
|
+
y.right.parent = y
|
|
267
|
+
self._transplant(z, y)
|
|
268
|
+
y.left = z.left
|
|
269
|
+
y.left.parent = y
|
|
270
|
+
y.color = z.color
|
|
271
|
+
if y_original_color is Color.BLACK:
|
|
272
|
+
self._delete_fixup(x)
|
|
273
|
+
|
|
274
|
+
def _delete_fixup(self, x: RBNode) -> None:
|
|
275
|
+
red, black = Color.RED, Color.BLACK
|
|
276
|
+
nil = self._nil
|
|
277
|
+
while x is not self._root and x.color is black:
|
|
278
|
+
if x is x.parent.left:
|
|
279
|
+
w = x.parent.right
|
|
280
|
+
if w.color is red: # cas 1
|
|
281
|
+
w.color = black
|
|
282
|
+
x.parent.color = red
|
|
283
|
+
self._left_rotate(x.parent)
|
|
284
|
+
w = x.parent.right
|
|
285
|
+
if w.left.color is black and w.right.color is black: # cas 2
|
|
286
|
+
w.color = red
|
|
287
|
+
x = x.parent
|
|
288
|
+
else:
|
|
289
|
+
if w.right.color is black: # cas 3
|
|
290
|
+
w.left.color = black
|
|
291
|
+
w.color = red
|
|
292
|
+
self._right_rotate(w)
|
|
293
|
+
w = x.parent.right
|
|
294
|
+
w.color = x.parent.color # cas 4
|
|
295
|
+
x.parent.color = black
|
|
296
|
+
w.right.color = black
|
|
297
|
+
self._left_rotate(x.parent)
|
|
298
|
+
x = self._root
|
|
299
|
+
else:
|
|
300
|
+
w = x.parent.left
|
|
301
|
+
if w.color is red:
|
|
302
|
+
w.color = black
|
|
303
|
+
x.parent.color = red
|
|
304
|
+
self._right_rotate(x.parent)
|
|
305
|
+
w = x.parent.left
|
|
306
|
+
if w.right.color is black and w.left.color is black:
|
|
307
|
+
w.color = red
|
|
308
|
+
x = x.parent
|
|
309
|
+
else:
|
|
310
|
+
if w.left.color is black:
|
|
311
|
+
w.right.color = black
|
|
312
|
+
w.color = red
|
|
313
|
+
self._left_rotate(w)
|
|
314
|
+
w = x.parent.left
|
|
315
|
+
w.color = x.parent.color
|
|
316
|
+
x.parent.color = black
|
|
317
|
+
w.left.color = black
|
|
318
|
+
self._right_rotate(x.parent)
|
|
319
|
+
x = self._root
|
|
320
|
+
x.color = black
|
|
321
|
+
|
|
322
|
+
def clear(self) -> None:
|
|
323
|
+
self._root = self._nil
|
|
324
|
+
self._size = 0
|
|
325
|
+
|
|
326
|
+
# -- parcours ------------------------------------------------
|
|
327
|
+
def inorder(self) -> List[Any]:
|
|
328
|
+
"""Ordre croissant."""
|
|
329
|
+
out: List[Any] = []
|
|
330
|
+
nil = self._nil
|
|
331
|
+
stack, cur = [], self._root
|
|
332
|
+
while stack or cur is not nil:
|
|
333
|
+
while cur is not nil:
|
|
334
|
+
stack.append(cur)
|
|
335
|
+
cur = cur.left
|
|
336
|
+
cur = stack.pop()
|
|
337
|
+
out.append(cur.key)
|
|
338
|
+
cur = cur.right
|
|
339
|
+
return out
|
|
340
|
+
|
|
341
|
+
def preorder(self) -> List[Any]:
|
|
342
|
+
out: List[Any] = []
|
|
343
|
+
nil = self._nil
|
|
344
|
+
stack = [self._root] if self._root is not nil else []
|
|
345
|
+
while stack:
|
|
346
|
+
n = stack.pop()
|
|
347
|
+
out.append(n.key)
|
|
348
|
+
if n.right is not nil:
|
|
349
|
+
stack.append(n.right)
|
|
350
|
+
if n.left is not nil:
|
|
351
|
+
stack.append(n.left)
|
|
352
|
+
return out
|
|
353
|
+
|
|
354
|
+
def levelorder(self) -> List[Any]:
|
|
355
|
+
out: List[Any] = []
|
|
356
|
+
nil = self._nil
|
|
357
|
+
queue = [self._root] if self._root is not nil else []
|
|
358
|
+
i = 0
|
|
359
|
+
while i < len(queue):
|
|
360
|
+
n = queue[i]
|
|
361
|
+
i += 1
|
|
362
|
+
out.append(n.key)
|
|
363
|
+
if n.left is not nil:
|
|
364
|
+
queue.append(n.left)
|
|
365
|
+
if n.right is not nil:
|
|
366
|
+
queue.append(n.right)
|
|
367
|
+
return out
|
|
368
|
+
|
|
369
|
+
def nodes(self) -> List[RBNode]:
|
|
370
|
+
"""Tous les nœuds réels, en préordre (sentinelle exclue)."""
|
|
371
|
+
out: List[RBNode] = []
|
|
372
|
+
nil = self._nil
|
|
373
|
+
stack = [self._root] if self._root is not nil else []
|
|
374
|
+
while stack:
|
|
375
|
+
n = stack.pop()
|
|
376
|
+
out.append(n)
|
|
377
|
+
if n.right is not nil:
|
|
378
|
+
stack.append(n.right)
|
|
379
|
+
if n.left is not nil:
|
|
380
|
+
stack.append(n.left)
|
|
381
|
+
return out
|
|
382
|
+
|
|
383
|
+
# -- mesures -------------------------------------------------
|
|
384
|
+
@property
|
|
385
|
+
def height(self) -> int:
|
|
386
|
+
"""Hauteur : arbre vide = -1 ; nœuds noirs comptés."""
|
|
387
|
+
nil = self._nil
|
|
388
|
+
if self._root is nil:
|
|
389
|
+
return -1
|
|
390
|
+
best, stack = 0, [(self._root, 0)]
|
|
391
|
+
while stack:
|
|
392
|
+
n, d = stack.pop()
|
|
393
|
+
best = max(best, d)
|
|
394
|
+
if n.left is not nil:
|
|
395
|
+
stack.append((n.left, d + 1))
|
|
396
|
+
if n.right is not nil:
|
|
397
|
+
stack.append((n.right, d + 1))
|
|
398
|
+
return best
|
|
399
|
+
|
|
400
|
+
@property
|
|
401
|
+
def black_height(self) -> int:
|
|
402
|
+
"""Nombre de nœuds noirs sur un chemin racine→feuille (NIL inclus)."""
|
|
403
|
+
return self._black_height(self._root)
|
|
404
|
+
|
|
405
|
+
def _black_height(self, n: RBNode) -> int:
|
|
406
|
+
if n is self._nil:
|
|
407
|
+
return 1
|
|
408
|
+
return self._black_height(n.left) + (1 if n.color is Color.BLACK else 0)
|
|
409
|
+
|
|
410
|
+
# -- vérification des invariants ------------------------------
|
|
411
|
+
def validate(self) -> None:
|
|
412
|
+
"""Lève ValueError si l'un des 5 invariants est violé."""
|
|
413
|
+
nil = self._nil
|
|
414
|
+
if self._root is nil:
|
|
415
|
+
return
|
|
416
|
+
if self._root.color is Color.RED:
|
|
417
|
+
raise ValueError("invariant 2 violé : racine rouge")
|
|
418
|
+
if self._root.parent is not None:
|
|
419
|
+
raise ValueError("la racine a un parent")
|
|
420
|
+
self._check(self._root, None, None)
|
|
421
|
+
if self._check_black_height(nil, self._root) < 0:
|
|
422
|
+
raise ValueError("invariant 5 violé : hauteurs noires différentes")
|
|
423
|
+
|
|
424
|
+
def _check(self, n: RBNode, lo: Any, hi: Any) -> None:
|
|
425
|
+
if n is self._nil:
|
|
426
|
+
return
|
|
427
|
+
if lo is not None and not (lo < n.key):
|
|
428
|
+
raise ValueError(f"propriété ABR violée : {n.key!r} <= {lo!r}")
|
|
429
|
+
if hi is not None and not (n.key < hi):
|
|
430
|
+
raise ValueError(f"propriété ABR violée : {n.key!r} >= {hi!r}")
|
|
431
|
+
if n.color is Color.RED:
|
|
432
|
+
if n.left.color is Color.RED:
|
|
433
|
+
raise ValueError(f"invariant 4 violé (rouge-rouge) : {n.key!r}/{n.left.key!r}")
|
|
434
|
+
if n.right.color is Color.RED:
|
|
435
|
+
raise ValueError(f"invariant 4 violé (rouge-rouge) : {n.key!r}/{n.right.key!r}")
|
|
436
|
+
self._check(n.left, lo, n.key)
|
|
437
|
+
self._check(n.right, n.key, hi)
|
|
438
|
+
|
|
439
|
+
def _check_black_height(self, nil: RBNode, n: RBNode) -> int:
|
|
440
|
+
if n is nil:
|
|
441
|
+
return 1
|
|
442
|
+
l = self._check_black_height(nil, n.left)
|
|
443
|
+
r = self._check_black_height(nil, n.right)
|
|
444
|
+
if l < 0 or r < 0 or l != r:
|
|
445
|
+
return -1
|
|
446
|
+
return l + (1 if n.color is Color.BLACK else 0)
|
|
447
|
+
|
|
448
|
+
# -- affichage ------------------------------------------------
|
|
449
|
+
def __str__(self) -> str:
|
|
450
|
+
lines: List[str] = []
|
|
451
|
+
nil = self._nil
|
|
452
|
+
|
|
453
|
+
def rec(n: RBNode, prefix: str, is_last: bool) -> None:
|
|
454
|
+
if n is nil:
|
|
455
|
+
return
|
|
456
|
+
lines.append(prefix + ("└── " if is_last else "├── ") + repr(n))
|
|
457
|
+
child_prefix = prefix + (" " if is_last else "│ ")
|
|
458
|
+
kids = [c for c in (n.left, n.right) if c is not nil]
|
|
459
|
+
for i, c in enumerate(kids):
|
|
460
|
+
rec(c, child_prefix, i == len(kids) - 1)
|
|
461
|
+
|
|
462
|
+
rec(self._root, "", True)
|
|
463
|
+
return "\n".join(lines)
|
|
464
|
+
|
|
465
|
+
def __repr__(self) -> str:
|
|
466
|
+
return f"RBTree(size={self._size}, height={self.height}, black_height={self.black_height})"
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: arborikit
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: General trees, unbalanced BSTs and red-black trees. Zero dependencies.
|
|
5
|
+
Author: Arborikit contributors
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/jojo8356/arborikit
|
|
8
|
+
Project-URL: Repository, https://github.com/jojo8356/arborikit
|
|
9
|
+
Project-URL: Issues, https://github.com/jojo8356/arborikit/issues
|
|
10
|
+
Project-URL: Changelog, https://github.com/jojo8356/arborikit/blob/main/CHANGELOG.md
|
|
11
|
+
Keywords: tree,bst,red-black,arn,abt,arbre,data-structure,algorithm
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Intended Audience :: Education
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Topic :: Education
|
|
25
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
26
|
+
Classifier: Typing :: Typed
|
|
27
|
+
Requires-Python: >=3.8
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Provides-Extra: test
|
|
31
|
+
Requires-Dist: pytest>=7; extra == "test"
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# arborikit (Python)
|
|
35
|
+
|
|
36
|
+
> Arbres généralistes, ABR non équilibrés et ARN — **zéro dépendance**.
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from arborikit import GeneralTree, BST, RBTree
|
|
40
|
+
|
|
41
|
+
# Arbre généraliste
|
|
42
|
+
t = GeneralTree("CEO")
|
|
43
|
+
t.add("CEO", "VP1")
|
|
44
|
+
t.add("CEO", "VP2")
|
|
45
|
+
t.add("VP1", "GM1")
|
|
46
|
+
print(t) # affichage arborescent
|
|
47
|
+
print(t.preorder()) # ['CEO', 'VP1', 'GM1', 'VP2']
|
|
48
|
+
|
|
49
|
+
# ABR non équilibré
|
|
50
|
+
b = BST([50, 30, 70, 20, 40])
|
|
51
|
+
b.insert(60)
|
|
52
|
+
print(b.inorder()) # [20, 30, 40, 50, 60, 70]
|
|
53
|
+
print(b.height, b.is_degenerate)
|
|
54
|
+
|
|
55
|
+
# ARN (rouge-noir), observable
|
|
56
|
+
r = RBTree([50, 30, 70, 20, 40, 60, 80])
|
|
57
|
+
r.validate() # vérifie les 5 invariants
|
|
58
|
+
print(r) # 50(N), 30(R), ...
|
|
59
|
+
print(r.inorder(), r.black_height)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## API
|
|
63
|
+
|
|
64
|
+
| Structure | Méthodes |
|
|
65
|
+
|---|---|
|
|
66
|
+
| `GeneralTree` | `add`, `prune`, `find`, `preorder`, `postorder`, `levelorder`, `leaves`, `size`, `height` |
|
|
67
|
+
| `BST` | `insert`, `delete`, `find`, `min`, `max`, `successor`, `predecessor`, `inorder`, `preorder`, `postorder`, `levelorder`, `height`, `is_degenerate`, `validate` |
|
|
68
|
+
| `RBTree` | idem `BST` + `root`, `nodes`, `black_height`, `validate`, couleurs `Color.RED/BLACK` |
|
|
69
|
+
|
|
70
|
+
## Installation
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
pip install arborikit
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Licence
|
|
77
|
+
|
|
78
|
+
MIT.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
arborikit/__init__.py,sha256=OhhpshQblmULJxOo1BgnViQ4GlodRxWII-10vJHqKK4,654
|
|
2
|
+
arborikit/bst.py,sha256=_3N-3vQ7UKfhddXusFsBUMxTY16x7ysO4Bf6NhCqMb8,8780
|
|
3
|
+
arborikit/general_tree.py,sha256=aSU4UwXIVTUlDB3cO0P_vDj5Vxa4jg49cuTnkd_4up0,5406
|
|
4
|
+
arborikit/rbtree.py,sha256=Fp0wDwOX96EY7lu_AaK8S_oCAkjsmWQG4uMrzsobbM4,15458
|
|
5
|
+
arborikit-0.1.0.dist-info/licenses/LICENSE,sha256=9lycKuuIn6kRPy7kwBnRgY40--qeYLk5dbDoXsRHnyI,1079
|
|
6
|
+
arborikit-0.1.0.dist-info/METADATA,sha256=2-wtlVSqSp2Bp6lqBMsXGA8E4Qz6r1hr9AZ4HmWLoJ8,2628
|
|
7
|
+
arborikit-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
8
|
+
arborikit-0.1.0.dist-info/top_level.txt,sha256=q5VM9fj240XIlU3zF9tM2_2-_TZrhB2NeNjbOC733zE,10
|
|
9
|
+
arborikit-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Arborikit contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
arborikit
|