Skip to content

Commit d53a4b2

Browse files
committed
Add support for another list representation, Node.equals and Node.clone methods
1 parent f181d3d commit d53a4b2

File tree

5 files changed

+499
-62
lines changed

5 files changed

+499
-62
lines changed

README.md

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ also supported.
1616

1717
![IPython Demo](gifs/demo.gif)
1818

19-
**New in version 6.0.0**: You can now use binarytree with
20-
[Graphviz](https://graphviz.org) and [Jupyter Notebooks](https://jupyter.org)
21-
([documentation](https://binarytree.readthedocs.io/en/main/graphviz.html)):
19+
Binarytree can be used with [Graphviz](https://graphviz.org) and
20+
[Jupyter Notebooks](https://jupyter.org) as well:
2221

2322
![Jupyter Demo](gifs/jupyter.gif)
2423

@@ -58,16 +57,16 @@ Generate and pretty-print various types of binary trees:
5857
```python
5958
from binarytree import tree, bst, heap
6059

61-
# Generate a random binary tree and return its root node
60+
# Generate a random binary tree and return its root node.
6261
my_tree = tree(height=3, is_perfect=False)
6362

64-
# Generate a random BST and return its root node
63+
# Generate a random BST and return its root node.
6564
my_bst = bst(height=3, is_perfect=True)
6665

67-
# Generate a random max heap and return its root node
66+
# Generate a random max heap and return its root node.
6867
my_heap = heap(height=3, is_max=True, is_perfect=False)
6968

70-
# Pretty-print the trees in stdout
69+
# Pretty-print the trees in stdout.
7170
print(my_tree)
7271
#
7372
# _______1_____
@@ -154,7 +153,7 @@ assert root.min_leaf_depth == 1
154153
assert root.min_node_value == 1
155154
assert root.size == 5
156155

157-
# See all properties at once:
156+
# See all properties at once.
158157
assert root.properties == {
159158
'height': 2,
160159
'is_balanced': True,
@@ -178,6 +177,21 @@ print(root.leaves)
178177
print(root.levels)
179178
# [[Node(1)], [Node(2), Node(3)], [Node(4), Node(5)]]
180179
```
180+
181+
Compare and clone trees:
182+
```python
183+
from binarytree import tree
184+
185+
original = tree()
186+
187+
# Clone the binary tree.
188+
clone = original.clone()
189+
190+
# Check if the trees are equal.
191+
original.equals(clone)
192+
```
193+
194+
181195
Use [level-order (breadth-first)](https://en.wikipedia.org/wiki/Tree_traversal#Breadth-first_search)
182196
indexes to manipulate nodes:
183197

@@ -213,7 +227,7 @@ root.pprint(index=True)
213227
print(root[9])
214228
# Node(5)
215229

216-
# Replace the node/subtree at index 4
230+
# Replace the node/subtree at index 4.
217231
root[4] = Node(6, left=Node(7), right=Node(8))
218232
root.pprint(index=True)
219233
#
@@ -226,7 +240,7 @@ root.pprint(index=True)
226240
# 9-7 10-8
227241
#
228242

229-
# Delete the node/subtree at index 1
243+
# Delete the node/subtree at index 1.
230244
del root[1]
231245
root.pprint(index=True)
232246
#
@@ -294,4 +308,41 @@ print(root.values)
294308
# [7, 3, 2, 6, 9, None, 1, 5, 8]
295309
```
296310

311+
Binarytree supports another representation which is more compact but without
312+
the [indexing properties](https://en.wikipedia.org/wiki/Binary_tree#Arrays):
313+
314+
```python
315+
from binarytree import build, build2, Node
316+
317+
# First let's create an example tree.
318+
root = Node(1)
319+
root.left = Node(2)
320+
root.left.left = Node(3)
321+
root.left.left.left = Node(4)
322+
root.left.left.right = Node(5)
323+
print(root)
324+
#
325+
# 1
326+
# /
327+
# __2
328+
# /
329+
# 3
330+
# / \
331+
# 4 5
332+
333+
# First representation is was already shown above.
334+
# All "null" nodes in each level are present.
335+
print(root.values)
336+
# [1, 2, None, 3, None, None, None, 4, 5]
337+
338+
# Second representation is more compact but without the indexing properties.
339+
print(root.values2)
340+
# [1, 2, None, 3, None, 4, 5]
341+
342+
# Build trees from the list representations
343+
tree1 = build(root.values)
344+
tree2 = build2(root.values2)
345+
assert tree1.equals(tree2) is True
346+
```
347+
297348
Check out the [documentation](http://binarytree.readthedocs.io) for more details.

0 commit comments

Comments
 (0)