stepps 0.2.0__tar.gz → 0.4.0__tar.gz
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.
- {stepps-0.2.0 → stepps-0.4.0}/PKG-INFO +88 -4
- {stepps-0.2.0 → stepps-0.4.0}/README.md +87 -3
- {stepps-0.2.0 → stepps-0.4.0}/pyproject.toml +4 -1
- stepps-0.4.0/src/stepps/comparable.py +27 -0
- stepps-0.4.0/src/stepps/nodes/__init__.py +14 -0
- stepps-0.4.0/src/stepps/nodes/binary_node.py +65 -0
- stepps-0.4.0/src/stepps/nodes/impl/__init__.py +3 -0
- stepps-0.2.0/src/stepps/nodes/binary_node.py → stepps-0.4.0/src/stepps/nodes/impl/binary_node_impl.py +26 -7
- stepps-0.4.0/src/stepps/tree_builder/__init__.py +21 -0
- stepps-0.4.0/src/stepps/tree_builder/inorder_tree_builder.py +10 -0
- {stepps-0.2.0/src/stepps/TreeBuilder → stepps-0.4.0/src/stepps/tree_builder}/inorder_tree_builder_impl.py +3 -2
- stepps-0.4.0/src/stepps/tree_builder/level_order_tree_builder.py +10 -0
- {stepps-0.2.0/src/stepps/TreeBuilder → stepps-0.4.0/src/stepps/tree_builder}/level_order_tree_builder_impl.py +5 -5
- stepps-0.4.0/src/stepps/tree_builder/postorder_tree_builder.py +10 -0
- {stepps-0.2.0/src/stepps/TreeBuilder → stepps-0.4.0/src/stepps/tree_builder}/postorder_tree_builder_impl.py +3 -2
- stepps-0.4.0/src/stepps/tree_builder/preorder_tree_builder.py +10 -0
- {stepps-0.2.0/src/stepps/TreeBuilder → stepps-0.4.0/src/stepps/tree_builder}/preorder_tree_builder_impl.py +3 -3
- {stepps-0.2.0/src/stepps/TreeBuilder → stepps-0.4.0/src/stepps/tree_builder}/tree_builder.py +3 -2
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps/trees/__init__.py +2 -1
- stepps-0.4.0/src/stepps/trees/balanced/avl_tree/__init__.py +4 -0
- stepps-0.4.0/src/stepps/trees/balanced/avl_tree/avl.py +19 -0
- stepps-0.4.0/src/stepps/trees/balanced/avl_tree/avl_impl.py +245 -0
- stepps-0.4.0/src/stepps/trees/balanced/rb_tree/__init__.py +3 -0
- stepps-0.4.0/src/stepps/trees/balanced/rb_tree/rb_node.py +60 -0
- stepps-0.4.0/src/stepps/trees/balanced/rb_tree/rb_node_impl.py +98 -0
- stepps-0.4.0/src/stepps/trees/balanced/rb_tree/red_black.py +21 -0
- stepps-0.4.0/src/stepps/trees/balanced/rb_tree/red_black_impl.py +828 -0
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps/trees/binary_tree.py +26 -5
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps/trees/binary_tree_impl.py +24 -0
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps/trees/bst.py +2 -11
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps/trees/bst_impl.py +4 -2
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps/trees/tree.py +3 -2
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps/visualizer/cli_visualizer.py +2 -1
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps/visualizer/tree_visualizer.py +3 -2
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps.egg-info/PKG-INFO +88 -4
- stepps-0.4.0/src/stepps.egg-info/SOURCES.txt +57 -0
- stepps-0.4.0/tests/test_avl.py +368 -0
- {stepps-0.2.0 → stepps-0.4.0}/tests/test_binary_tree.py +100 -25
- {stepps-0.2.0 → stepps-0.4.0}/tests/test_inorder_tree_builder.py +1 -1
- {stepps-0.2.0 → stepps-0.4.0}/tests/test_level_order_tree_builder.py +1 -1
- {stepps-0.2.0 → stepps-0.4.0}/tests/test_postorder_tree_builder.py +1 -1
- {stepps-0.2.0 → stepps-0.4.0}/tests/test_preorder_tree_builder.py +1 -1
- stepps-0.4.0/tests/test_rb_tree.py +970 -0
- stepps-0.2.0/src/stepps/TreeBuilder/__init__.py +0 -13
- stepps-0.2.0/src/stepps/TreeBuilder/inorder_tree_builder.py +0 -8
- stepps-0.2.0/src/stepps/TreeBuilder/level_order_tree_builder.py +0 -8
- stepps-0.2.0/src/stepps/TreeBuilder/postorder_tree_builder.py +0 -8
- stepps-0.2.0/src/stepps/TreeBuilder/preorder_tree_builder.py +0 -8
- stepps-0.2.0/src/stepps/nodes/__init__.py +0 -3
- stepps-0.2.0/src/stepps.egg-info/SOURCES.txt +0 -44
- {stepps-0.2.0 → stepps-0.4.0}/LICENSE +0 -0
- {stepps-0.2.0 → stepps-0.4.0}/setup.cfg +0 -0
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps/__init__.py +0 -0
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps/iterators/__init__.py +0 -0
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps/iterators/base_iterator.py +0 -0
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps/iterators/inorder.py +0 -0
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps/iterators/levelorder.py +0 -0
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps/iterators/postorder.py +0 -0
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps/iterators/preorder.py +0 -0
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps/py.typed +0 -0
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps/visualizer/__init__.py +0 -0
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps/visualizer/cli_visualizer_impl.py +0 -0
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps.egg-info/dependency_links.txt +0 -0
- {stepps-0.2.0 → stepps-0.4.0}/src/stepps.egg-info/top_level.txt +0 -0
- {stepps-0.2.0 → stepps-0.4.0}/tests/test_bst.py +0 -0
- {stepps-0.2.0 → stepps-0.4.0}/tests/test_visualizer.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: stepps
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: Useful algorithms in Python
|
|
5
5
|
Author-email: Suhas Krishna Srivastava <suhas.srivastava@vaastav.tech>, Trinay Mitra <trinay.mitra@vaastav.tech>
|
|
6
6
|
Maintainer-email: Suhas Krishna Srivastava <suhas.srivastava@vaastav.tech>
|
|
@@ -42,8 +42,8 @@ Dynamic: license-file
|
|
|
42
42
|
**Fully typed, modular data structures and algorithms for binary trees in Python.**
|
|
43
43
|
|
|
44
44
|
`stepps` provides generic binary tree and binary search tree implementations,
|
|
45
|
-
tree traversal iterators, traversal-based tree
|
|
46
|
-
utilities.
|
|
45
|
+
AVL balancing, red-black trees, tree traversal iterators, traversal-based tree
|
|
46
|
+
builders, and tree visualization utilities.
|
|
47
47
|
|
|
48
48
|
The project follows an interface-and-implementation architecture and uses
|
|
49
49
|
Python generics and static typing throughout its APIs.
|
|
@@ -54,6 +54,10 @@ Python generics and static typing throughout its APIs.
|
|
|
54
54
|
|
|
55
55
|
- Generic binary tree data structures.
|
|
56
56
|
- Binary search tree implementation.
|
|
57
|
+
- AVL tree balancing with configurable balance factor.
|
|
58
|
+
- Red-black tree implementation.
|
|
59
|
+
- Automatic balancing after AVL and red-black tree insert and delete operations.
|
|
60
|
+
- Binary node creation through `get_binary_node(value)`.
|
|
57
61
|
- Preorder, inorder, postorder, and level-order traversal iterators.
|
|
58
62
|
- Tree builders for preorder, inorder, postorder, and level-order sequences.
|
|
59
63
|
- Binary tree statistics and utility operations.
|
|
@@ -103,6 +107,11 @@ the default implementation.
|
|
|
103
107
|
| `count_leaves()` | Returns the number of leaf nodes. |
|
|
104
108
|
| `count_internal_nodes()` | Returns the number of internal nodes. |
|
|
105
109
|
| `invert_tree()` | Inverts the tree in-place. |
|
|
110
|
+
| `get_root()` | Returns the root node, or `None` if the tree is empty. |
|
|
111
|
+
| `get_left_subtree()` | Returns the left subtree. |
|
|
112
|
+
| `get_right_subtree()` | Returns the right subtree. |
|
|
113
|
+
| `left_rotate()` | Performs a left rotation on the tree. |
|
|
114
|
+
| `right_rotate()` | Performs a right rotation on the tree. |
|
|
106
115
|
|
|
107
116
|
---
|
|
108
117
|
|
|
@@ -138,6 +147,75 @@ are greater.
|
|
|
138
147
|
|
|
139
148
|
---
|
|
140
149
|
|
|
150
|
+
## AVL Tree
|
|
151
|
+
|
|
152
|
+
`AVL` defines the AVL balancing interface and `AVLImpl` provides the
|
|
153
|
+
implementation.
|
|
154
|
+
|
|
155
|
+
`AVLImpl` accepts a binary tree through its constructor and provides automatic
|
|
156
|
+
balancing after insert and delete operations. The implementation supports LL,
|
|
157
|
+
RR, LR, and RL balancing cases and a configurable balance factor.
|
|
158
|
+
|
|
159
|
+
`AVL` and `AVLImpl` are available from `stepps.trees.balanced.avl_tree`.
|
|
160
|
+
|
|
161
|
+
### AVL Tree Methods
|
|
162
|
+
|
|
163
|
+
| Method | Description |
|
|
164
|
+
|---|---|
|
|
165
|
+
| `balance()` | Balances and returns the supplied binary tree. |
|
|
166
|
+
| `insert(value)` | Inserts a value into the supplied tree and balances it. |
|
|
167
|
+
| `delete(value)` | Deletes a value from the supplied tree and balances it. |
|
|
168
|
+
|
|
169
|
+
The remaining binary tree operations are delegated to the supplied tree.
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## Red-Black Tree
|
|
174
|
+
|
|
175
|
+
`RedBlack` defines the red-black tree interface and `RedBlackImpl` provides the
|
|
176
|
+
implementation.
|
|
177
|
+
|
|
178
|
+
The red-black tree maintains BST ordering and uses node colors, rotations, and
|
|
179
|
+
recoloring to maintain balance after insert and delete operations. Duplicate
|
|
180
|
+
values are not inserted.
|
|
181
|
+
|
|
182
|
+
`RedBlackImpl` accepts a binary tree through its constructor and supports
|
|
183
|
+
creating a red-black tree from an existing binary tree.
|
|
184
|
+
|
|
185
|
+
`RedBlack` and `RedBlackImpl` are available from
|
|
186
|
+
`stepps.trees.balanced.rb_tree.red_black` and
|
|
187
|
+
`stepps.trees.balanced.rb_tree.red_black_impl`, respectively.
|
|
188
|
+
|
|
189
|
+
### Red-Black Tree Methods
|
|
190
|
+
|
|
191
|
+
| Method | Description |
|
|
192
|
+
|---|---|
|
|
193
|
+
| `balance()` | Applies red-black balancing and returns the supplied tree. |
|
|
194
|
+
| `insert(value)` | Inserts a value while maintaining BST ordering and red-black balance. |
|
|
195
|
+
| `delete(value)` | Deletes a value while maintaining BST ordering and red-black balance. |
|
|
196
|
+
| `find(value)` | Searches for and returns the node containing the value. |
|
|
197
|
+
| `contains(value)` | Checks whether a value exists in the tree. |
|
|
198
|
+
| `__contains__(value)` | Supports the `value in tree` operation. |
|
|
199
|
+
| `minimum()` | Returns the node containing the minimum value. |
|
|
200
|
+
| `maximum()` | Returns the node containing the maximum value. |
|
|
201
|
+
| `min()` | Returns the node containing the minimum value. |
|
|
202
|
+
| `max()` | Returns the node containing the maximum value. |
|
|
203
|
+
|
|
204
|
+
The implementation also provides root access, tree statistics, and clear
|
|
205
|
+
operations.
|
|
206
|
+
|
|
207
|
+
### Red-Black Node
|
|
208
|
+
|
|
209
|
+
`RBNode` defines the red-black node interface and `RBNodeImpl` provides the
|
|
210
|
+
implementation. Each node extends the binary node attributes with:
|
|
211
|
+
|
|
212
|
+
| Attribute | Description |
|
|
213
|
+
|---|---|
|
|
214
|
+
| `parent` | Reference to the parent node or `None`. |
|
|
215
|
+
| `color` | The node color: `RED` (`0`) or `BLACK` (`1`). |
|
|
216
|
+
|
|
217
|
+
---
|
|
218
|
+
|
|
141
219
|
## Tree Traversal Iterators
|
|
142
220
|
|
|
143
221
|
`stepps` provides iterators implementing the standard binary tree traversal
|
|
@@ -224,7 +302,10 @@ displaying binary trees in the CLI.
|
|
|
224
302
|
|
|
225
303
|
## Binary Node
|
|
226
304
|
|
|
227
|
-
`BinaryNode[T]`
|
|
305
|
+
`BinaryNode[T]` defines the generic node interface used by the binary tree
|
|
306
|
+
structures and `BinaryNodeImpl[T]` provides the implementation.
|
|
307
|
+
|
|
308
|
+
`get_binary_node(value)` in `stepps.nodes` creates and returns a binary node.
|
|
228
309
|
|
|
229
310
|
Each node provides:
|
|
230
311
|
|
|
@@ -311,6 +392,9 @@ The test suite covers:
|
|
|
311
392
|
|
|
312
393
|
- Binary tree operations.
|
|
313
394
|
- Binary search tree operations.
|
|
395
|
+
- AVL balancing, including LL, RR, LR, and RL cases.
|
|
396
|
+
- Red-black tree operations, balancing, node colors, and parent references.
|
|
397
|
+
- Red-black tree invariants after randomized insert and delete operations.
|
|
314
398
|
- Tree traversal iterators.
|
|
315
399
|
- Tree builders.
|
|
316
400
|
- Tree visualization.
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
**Fully typed, modular data structures and algorithms for binary trees in Python.**
|
|
13
13
|
|
|
14
14
|
`stepps` provides generic binary tree and binary search tree implementations,
|
|
15
|
-
tree traversal iterators, traversal-based tree
|
|
16
|
-
utilities.
|
|
15
|
+
AVL balancing, red-black trees, tree traversal iterators, traversal-based tree
|
|
16
|
+
builders, and tree visualization utilities.
|
|
17
17
|
|
|
18
18
|
The project follows an interface-and-implementation architecture and uses
|
|
19
19
|
Python generics and static typing throughout its APIs.
|
|
@@ -24,6 +24,10 @@ Python generics and static typing throughout its APIs.
|
|
|
24
24
|
|
|
25
25
|
- Generic binary tree data structures.
|
|
26
26
|
- Binary search tree implementation.
|
|
27
|
+
- AVL tree balancing with configurable balance factor.
|
|
28
|
+
- Red-black tree implementation.
|
|
29
|
+
- Automatic balancing after AVL and red-black tree insert and delete operations.
|
|
30
|
+
- Binary node creation through `get_binary_node(value)`.
|
|
27
31
|
- Preorder, inorder, postorder, and level-order traversal iterators.
|
|
28
32
|
- Tree builders for preorder, inorder, postorder, and level-order sequences.
|
|
29
33
|
- Binary tree statistics and utility operations.
|
|
@@ -73,6 +77,11 @@ the default implementation.
|
|
|
73
77
|
| `count_leaves()` | Returns the number of leaf nodes. |
|
|
74
78
|
| `count_internal_nodes()` | Returns the number of internal nodes. |
|
|
75
79
|
| `invert_tree()` | Inverts the tree in-place. |
|
|
80
|
+
| `get_root()` | Returns the root node, or `None` if the tree is empty. |
|
|
81
|
+
| `get_left_subtree()` | Returns the left subtree. |
|
|
82
|
+
| `get_right_subtree()` | Returns the right subtree. |
|
|
83
|
+
| `left_rotate()` | Performs a left rotation on the tree. |
|
|
84
|
+
| `right_rotate()` | Performs a right rotation on the tree. |
|
|
76
85
|
|
|
77
86
|
---
|
|
78
87
|
|
|
@@ -108,6 +117,75 @@ are greater.
|
|
|
108
117
|
|
|
109
118
|
---
|
|
110
119
|
|
|
120
|
+
## AVL Tree
|
|
121
|
+
|
|
122
|
+
`AVL` defines the AVL balancing interface and `AVLImpl` provides the
|
|
123
|
+
implementation.
|
|
124
|
+
|
|
125
|
+
`AVLImpl` accepts a binary tree through its constructor and provides automatic
|
|
126
|
+
balancing after insert and delete operations. The implementation supports LL,
|
|
127
|
+
RR, LR, and RL balancing cases and a configurable balance factor.
|
|
128
|
+
|
|
129
|
+
`AVL` and `AVLImpl` are available from `stepps.trees.balanced.avl_tree`.
|
|
130
|
+
|
|
131
|
+
### AVL Tree Methods
|
|
132
|
+
|
|
133
|
+
| Method | Description |
|
|
134
|
+
|---|---|
|
|
135
|
+
| `balance()` | Balances and returns the supplied binary tree. |
|
|
136
|
+
| `insert(value)` | Inserts a value into the supplied tree and balances it. |
|
|
137
|
+
| `delete(value)` | Deletes a value from the supplied tree and balances it. |
|
|
138
|
+
|
|
139
|
+
The remaining binary tree operations are delegated to the supplied tree.
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
## Red-Black Tree
|
|
144
|
+
|
|
145
|
+
`RedBlack` defines the red-black tree interface and `RedBlackImpl` provides the
|
|
146
|
+
implementation.
|
|
147
|
+
|
|
148
|
+
The red-black tree maintains BST ordering and uses node colors, rotations, and
|
|
149
|
+
recoloring to maintain balance after insert and delete operations. Duplicate
|
|
150
|
+
values are not inserted.
|
|
151
|
+
|
|
152
|
+
`RedBlackImpl` accepts a binary tree through its constructor and supports
|
|
153
|
+
creating a red-black tree from an existing binary tree.
|
|
154
|
+
|
|
155
|
+
`RedBlack` and `RedBlackImpl` are available from
|
|
156
|
+
`stepps.trees.balanced.rb_tree.red_black` and
|
|
157
|
+
`stepps.trees.balanced.rb_tree.red_black_impl`, respectively.
|
|
158
|
+
|
|
159
|
+
### Red-Black Tree Methods
|
|
160
|
+
|
|
161
|
+
| Method | Description |
|
|
162
|
+
|---|---|
|
|
163
|
+
| `balance()` | Applies red-black balancing and returns the supplied tree. |
|
|
164
|
+
| `insert(value)` | Inserts a value while maintaining BST ordering and red-black balance. |
|
|
165
|
+
| `delete(value)` | Deletes a value while maintaining BST ordering and red-black balance. |
|
|
166
|
+
| `find(value)` | Searches for and returns the node containing the value. |
|
|
167
|
+
| `contains(value)` | Checks whether a value exists in the tree. |
|
|
168
|
+
| `__contains__(value)` | Supports the `value in tree` operation. |
|
|
169
|
+
| `minimum()` | Returns the node containing the minimum value. |
|
|
170
|
+
| `maximum()` | Returns the node containing the maximum value. |
|
|
171
|
+
| `min()` | Returns the node containing the minimum value. |
|
|
172
|
+
| `max()` | Returns the node containing the maximum value. |
|
|
173
|
+
|
|
174
|
+
The implementation also provides root access, tree statistics, and clear
|
|
175
|
+
operations.
|
|
176
|
+
|
|
177
|
+
### Red-Black Node
|
|
178
|
+
|
|
179
|
+
`RBNode` defines the red-black node interface and `RBNodeImpl` provides the
|
|
180
|
+
implementation. Each node extends the binary node attributes with:
|
|
181
|
+
|
|
182
|
+
| Attribute | Description |
|
|
183
|
+
|---|---|
|
|
184
|
+
| `parent` | Reference to the parent node or `None`. |
|
|
185
|
+
| `color` | The node color: `RED` (`0`) or `BLACK` (`1`). |
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
|
|
111
189
|
## Tree Traversal Iterators
|
|
112
190
|
|
|
113
191
|
`stepps` provides iterators implementing the standard binary tree traversal
|
|
@@ -194,7 +272,10 @@ displaying binary trees in the CLI.
|
|
|
194
272
|
|
|
195
273
|
## Binary Node
|
|
196
274
|
|
|
197
|
-
`BinaryNode[T]`
|
|
275
|
+
`BinaryNode[T]` defines the generic node interface used by the binary tree
|
|
276
|
+
structures and `BinaryNodeImpl[T]` provides the implementation.
|
|
277
|
+
|
|
278
|
+
`get_binary_node(value)` in `stepps.nodes` creates and returns a binary node.
|
|
198
279
|
|
|
199
280
|
Each node provides:
|
|
200
281
|
|
|
@@ -281,6 +362,9 @@ The test suite covers:
|
|
|
281
362
|
|
|
282
363
|
- Binary tree operations.
|
|
283
364
|
- Binary search tree operations.
|
|
365
|
+
- AVL balancing, including LL, RR, LR, and RL cases.
|
|
366
|
+
- Red-black tree operations, balancing, node colors, and parent references.
|
|
367
|
+
- Red-black tree invariants after randomized insert and delete operations.
|
|
284
368
|
- Tree traversal iterators.
|
|
285
369
|
- Tree builders.
|
|
286
370
|
- Tree visualization.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "stepps"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.4.0"
|
|
4
4
|
description = "Useful algorithms in Python"
|
|
5
5
|
requires-python = ">=3.12"
|
|
6
6
|
readme = "README.md"
|
|
@@ -52,3 +52,6 @@ issues = "https://github.com/Vaastav-Technologies/py-algor/issues"
|
|
|
52
52
|
|
|
53
53
|
[tool.setuptools.packages.find]
|
|
54
54
|
where = ['src']
|
|
55
|
+
|
|
56
|
+
[tool.pytest.ini_options]
|
|
57
|
+
pythonpath = ["src"]
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from typing import Protocol
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Comparable(Protocol):
|
|
5
|
+
"""
|
|
6
|
+
Define the comparison operations required by ordered tree implementations.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
def __lt__(self, other: object, /) -> bool:
|
|
10
|
+
"""
|
|
11
|
+
Return whether this value is less than another value.
|
|
12
|
+
|
|
13
|
+
:param other: The value to compare with.
|
|
14
|
+
:return: ``True`` if this value is less than ``other``, otherwise
|
|
15
|
+
``False``.
|
|
16
|
+
"""
|
|
17
|
+
...
|
|
18
|
+
|
|
19
|
+
def __gt__(self, other: object, /) -> bool:
|
|
20
|
+
"""
|
|
21
|
+
Return whether this value is greater than another value.
|
|
22
|
+
|
|
23
|
+
:param other: The value to compare with.
|
|
24
|
+
:return: ``True`` if this value is greater than ``other``, otherwise
|
|
25
|
+
``False``.
|
|
26
|
+
"""
|
|
27
|
+
...
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from stepps.nodes.binary_node import BinaryNode
|
|
2
|
+
from stepps.nodes.impl.binary_node_impl import BinaryNodeImpl as _BinaryNodeImpl
|
|
3
|
+
|
|
4
|
+
__all__ = ["BinaryNode", "get_binary_node"]
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def get_binary_node[T](value: T) -> BinaryNode[T]:
|
|
8
|
+
"""
|
|
9
|
+
Create a binary node implementation.
|
|
10
|
+
|
|
11
|
+
:param value: The value to store in the node.
|
|
12
|
+
:return: A binary node implementation.
|
|
13
|
+
"""
|
|
14
|
+
return _BinaryNodeImpl(value)
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from abc import abstractmethod
|
|
4
|
+
from typing import Protocol
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class BinaryNode[T](Protocol):
|
|
8
|
+
@property
|
|
9
|
+
@abstractmethod
|
|
10
|
+
def value(self) -> T | None: ...
|
|
11
|
+
|
|
12
|
+
@property
|
|
13
|
+
@abstractmethod
|
|
14
|
+
def right(self) -> BinaryNode[T] | None: ...
|
|
15
|
+
|
|
16
|
+
@right.setter
|
|
17
|
+
@abstractmethod
|
|
18
|
+
def right(self, node: BinaryNode[T] | None) -> None: ...
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
@abstractmethod
|
|
22
|
+
def left(self) -> BinaryNode[T] | None: ...
|
|
23
|
+
|
|
24
|
+
@left.setter
|
|
25
|
+
@abstractmethod
|
|
26
|
+
def left(self, node: BinaryNode[T] | None) -> None: ...
|
|
27
|
+
@abstractmethod
|
|
28
|
+
def is_leaf(self):
|
|
29
|
+
"""
|
|
30
|
+
Return whether the node has no children.
|
|
31
|
+
|
|
32
|
+
:return: ``True`` if the node has no children, otherwise ``False``.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
@abstractmethod
|
|
36
|
+
def has_left(self) -> bool:
|
|
37
|
+
"""
|
|
38
|
+
Return whether the node has a left child.
|
|
39
|
+
|
|
40
|
+
:return: ``True`` if a left child exists, otherwise ``False``.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
@abstractmethod
|
|
44
|
+
def has_right(self) -> bool:
|
|
45
|
+
"""
|
|
46
|
+
Return whether the node has a right child.
|
|
47
|
+
|
|
48
|
+
:return: ``True`` if a right child exists, otherwise ``False``.
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
@abstractmethod
|
|
52
|
+
def has_children(self) -> bool:
|
|
53
|
+
"""
|
|
54
|
+
Return whether the node has at least one child.
|
|
55
|
+
|
|
56
|
+
:return: ``True`` if the node has one or more children, otherwise ``False``.
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
@abstractmethod
|
|
60
|
+
def child_count(self) -> int:
|
|
61
|
+
"""
|
|
62
|
+
Return the number of children of the node.
|
|
63
|
+
|
|
64
|
+
:return: The number of children, either ``0``, ``1``, or ``2``.
|
|
65
|
+
"""
|
|
@@ -1,24 +1,43 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
from stepps.nodes import BinaryNode
|
|
3
4
|
|
|
4
|
-
|
|
5
|
+
|
|
6
|
+
class BinaryNodeImpl[T](BinaryNode[T]):
|
|
5
7
|
"""
|
|
6
8
|
Represent a node in a binary tree.
|
|
7
9
|
|
|
8
|
-
:ivar value: The value stored in the node.
|
|
9
|
-
:ivar left: The left child of the node, if any.
|
|
10
|
-
:ivar right: The right child of the node, if any.
|
|
11
10
|
"""
|
|
12
11
|
|
|
12
|
+
@property
|
|
13
|
+
def value(self) -> T | None:
|
|
14
|
+
return self._value
|
|
15
|
+
|
|
16
|
+
@property
|
|
17
|
+
def right(self) -> BinaryNode[T] | None:
|
|
18
|
+
return self._right
|
|
19
|
+
|
|
20
|
+
@right.setter
|
|
21
|
+
def right(self, node: BinaryNode[T] | None) -> None:
|
|
22
|
+
self._right = node
|
|
23
|
+
|
|
24
|
+
@property
|
|
25
|
+
def left(self) -> BinaryNode[T] | None:
|
|
26
|
+
return self._left
|
|
27
|
+
|
|
28
|
+
@left.setter
|
|
29
|
+
def left(self, node: BinaryNode[T] | None) -> None:
|
|
30
|
+
self._left = node
|
|
31
|
+
|
|
13
32
|
def __init__(self, value: T) -> None:
|
|
14
33
|
"""
|
|
15
34
|
Initialize a binary tree node.
|
|
16
35
|
|
|
17
36
|
:param value: The value to store in the node.
|
|
18
37
|
"""
|
|
19
|
-
self.
|
|
20
|
-
self.
|
|
21
|
-
self.
|
|
38
|
+
self._value: T = value
|
|
39
|
+
self._right = None
|
|
40
|
+
self._left = None
|
|
22
41
|
|
|
23
42
|
def is_leaf(self) -> bool:
|
|
24
43
|
"""
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from stepps.tree_builder.inorder_tree_builder import InOrderTreeBuilder
|
|
2
|
+
from stepps.tree_builder.inorder_tree_builder_impl import InOrderTreeBuilderImpl
|
|
3
|
+
from stepps.tree_builder.level_order_tree_builder import LevelOrderTreeBuilder
|
|
4
|
+
from stepps.tree_builder.level_order_tree_builder_impl import LevelOrderTreeBuilderImpl
|
|
5
|
+
from stepps.tree_builder.postorder_tree_builder import PostOrderTreeBuilder
|
|
6
|
+
from stepps.tree_builder.postorder_tree_builder_impl import PostOrderTreeBuilderImpl
|
|
7
|
+
from stepps.tree_builder.preorder_tree_builder import PreOrderTreeBuilder
|
|
8
|
+
from stepps.tree_builder.preorder_tree_builder_impl import PreOrderTreeBuilderImpl
|
|
9
|
+
from stepps.tree_builder.tree_builder import TreeBuilder
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"InOrderTreeBuilder",
|
|
13
|
+
"InOrderTreeBuilderImpl",
|
|
14
|
+
"LevelOrderTreeBuilder",
|
|
15
|
+
"LevelOrderTreeBuilderImpl",
|
|
16
|
+
"PostOrderTreeBuilder",
|
|
17
|
+
"PostOrderTreeBuilderImpl",
|
|
18
|
+
"PreOrderTreeBuilder",
|
|
19
|
+
"PreOrderTreeBuilderImpl",
|
|
20
|
+
"TreeBuilder",
|
|
21
|
+
]
|
|
@@ -2,7 +2,8 @@ from collections.abc import Sequence
|
|
|
2
2
|
from typing import override
|
|
3
3
|
|
|
4
4
|
from stepps.nodes import BinaryNode
|
|
5
|
-
from stepps.
|
|
5
|
+
from stepps.nodes.impl.binary_node_impl import BinaryNodeImpl
|
|
6
|
+
from stepps.tree_builder.inorder_tree_builder import InOrderTreeBuilder
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
class InOrderTreeBuilderImpl[T](InOrderTreeBuilder[T]):
|
|
@@ -44,7 +45,7 @@ class InOrderTreeBuilderImpl[T](InOrderTreeBuilder[T]):
|
|
|
44
45
|
else:
|
|
45
46
|
mid = (left + right) // 2
|
|
46
47
|
|
|
47
|
-
node =
|
|
48
|
+
node = BinaryNodeImpl(sequence[mid])
|
|
48
49
|
|
|
49
50
|
node.left = self._build(sequence, left, mid - 1)
|
|
50
51
|
node.right = self._build(sequence, mid + 1, right)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from typing import Protocol
|
|
2
|
+
|
|
3
|
+
from stepps.tree_builder.tree_builder import TreeBuilder
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class LevelOrderTreeBuilder[T](TreeBuilder[T], Protocol):
|
|
7
|
+
"""
|
|
8
|
+
Define the interface for building a binary tree from a sequence
|
|
9
|
+
using level-order traversal.
|
|
10
|
+
"""
|
|
@@ -2,8 +2,8 @@ from collections import deque
|
|
|
2
2
|
from collections.abc import Sequence
|
|
3
3
|
from typing import override
|
|
4
4
|
|
|
5
|
-
from stepps.nodes import BinaryNode
|
|
6
|
-
from stepps.
|
|
5
|
+
from stepps.nodes import BinaryNode, get_binary_node
|
|
6
|
+
from stepps.tree_builder.level_order_tree_builder import LevelOrderTreeBuilder
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
class LevelOrderTreeBuilderImpl[T](LevelOrderTreeBuilder[T]):
|
|
@@ -26,7 +26,7 @@ class LevelOrderTreeBuilderImpl[T](LevelOrderTreeBuilder[T]):
|
|
|
26
26
|
if not sequence:
|
|
27
27
|
return None
|
|
28
28
|
|
|
29
|
-
root =
|
|
29
|
+
root = get_binary_node(sequence[0])
|
|
30
30
|
queue: deque[BinaryNode[T]] = deque([root])
|
|
31
31
|
|
|
32
32
|
index = 1
|
|
@@ -34,12 +34,12 @@ class LevelOrderTreeBuilderImpl[T](LevelOrderTreeBuilder[T]):
|
|
|
34
34
|
while index < len(sequence):
|
|
35
35
|
parent = queue.popleft()
|
|
36
36
|
|
|
37
|
-
parent.left =
|
|
37
|
+
parent.left = get_binary_node(sequence[index])
|
|
38
38
|
queue.append(parent.left)
|
|
39
39
|
index += 1
|
|
40
40
|
|
|
41
41
|
if index < len(sequence):
|
|
42
|
-
parent.right =
|
|
42
|
+
parent.right = get_binary_node(sequence[index])
|
|
43
43
|
queue.append(parent.right)
|
|
44
44
|
index += 1
|
|
45
45
|
|
|
@@ -2,7 +2,8 @@ from collections.abc import Sequence
|
|
|
2
2
|
from typing import override
|
|
3
3
|
|
|
4
4
|
from stepps.nodes import BinaryNode
|
|
5
|
-
from stepps.
|
|
5
|
+
from stepps.nodes.impl.binary_node_impl import BinaryNodeImpl
|
|
6
|
+
from stepps.tree_builder.postorder_tree_builder import PostOrderTreeBuilder
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
class PostOrderTreeBuilderImpl[T](PostOrderTreeBuilder[T]):
|
|
@@ -43,7 +44,7 @@ class PostOrderTreeBuilderImpl[T](PostOrderTreeBuilder[T]):
|
|
|
43
44
|
return None
|
|
44
45
|
|
|
45
46
|
root_index = start + size - 1
|
|
46
|
-
root =
|
|
47
|
+
root = BinaryNodeImpl(sequence[root_index])
|
|
47
48
|
|
|
48
49
|
remaining = size - 1
|
|
49
50
|
left_size = remaining // 2
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
from collections.abc import Sequence
|
|
2
2
|
from typing import override
|
|
3
3
|
|
|
4
|
-
from stepps.nodes import BinaryNode
|
|
5
|
-
from stepps.
|
|
4
|
+
from stepps.nodes import BinaryNode, get_binary_node
|
|
5
|
+
from stepps.tree_builder.preorder_tree_builder import PreOrderTreeBuilder
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
class PreOrderTreeBuilderImpl[T](PreOrderTreeBuilder[T]):
|
|
@@ -41,7 +41,7 @@ class PreOrderTreeBuilderImpl[T](PreOrderTreeBuilder[T]):
|
|
|
41
41
|
if size <= 0:
|
|
42
42
|
return None
|
|
43
43
|
|
|
44
|
-
root =
|
|
44
|
+
root = get_binary_node(sequence[index])
|
|
45
45
|
|
|
46
46
|
left_size = (size - 1) // 2
|
|
47
47
|
right_size = size - 1 - left_size
|
{stepps-0.2.0/src/stepps/TreeBuilder → stepps-0.4.0/src/stepps/tree_builder}/tree_builder.py
RENAMED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
from abc import
|
|
1
|
+
from abc import abstractmethod
|
|
2
2
|
from collections.abc import Sequence
|
|
3
|
+
from typing import Protocol
|
|
3
4
|
|
|
4
5
|
from stepps.nodes import BinaryNode
|
|
5
6
|
|
|
6
7
|
|
|
7
|
-
class TreeBuilder[T](
|
|
8
|
+
class TreeBuilder[T](Protocol):
|
|
8
9
|
"""
|
|
9
10
|
Define the interface for building a binary tree from a sequence.
|
|
10
11
|
"""
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
from collections.abc import Callable
|
|
2
2
|
from typing import Literal, overload
|
|
3
3
|
|
|
4
|
+
from stepps.comparable import Comparable
|
|
4
5
|
from stepps.iterators.base_iterator import BinaryTreeIterator
|
|
5
6
|
from stepps.iterators.levelorder import LevelOrderIterator as _LOI
|
|
6
7
|
from stepps.nodes import BinaryNode
|
|
7
8
|
from stepps.trees.binary_tree import BinaryTree
|
|
8
9
|
from stepps.trees.binary_tree_impl import BinaryTreeImpl
|
|
9
|
-
from stepps.trees.bst import BST
|
|
10
|
+
from stepps.trees.bst import BST
|
|
10
11
|
from stepps.trees.bst_impl import BSTImpl
|
|
11
12
|
from stepps.trees.tree import Tree
|
|
12
13
|
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from abc import abstractmethod
|
|
2
|
+
from typing import Protocol
|
|
3
|
+
|
|
4
|
+
from stepps.trees.binary_tree import BinaryTree
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class AVL[T](BinaryTree[T], Protocol):
|
|
8
|
+
"""
|
|
9
|
+
Define the interface for balancing a binary tree.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
@abstractmethod
|
|
13
|
+
def balance(self) -> BinaryTree[T]:
|
|
14
|
+
"""
|
|
15
|
+
Balance the binary tree.
|
|
16
|
+
|
|
17
|
+
:return: The balanced binary tree.
|
|
18
|
+
"""
|
|
19
|
+
...
|