@@ -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
5958from 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.
6261my_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.
6564my_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.
6867my_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.
7170print (my_tree)
7271#
7372# _______1_____
@@ -154,7 +153,7 @@ assert root.min_leaf_depth == 1
154153assert root.min_node_value == 1
155154assert root.size == 5
156155
157- # See all properties at once:
156+ # See all properties at once.
158157assert root.properties == {
159158 ' height' : 2 ,
160159 ' is_balanced' : True ,
@@ -178,6 +177,21 @@ print(root.leaves)
178177print (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+
181195Use [ level-order (breadth-first)] ( https://en.wikipedia.org/wiki/Tree_traversal#Breadth-first_search )
182196indexes to manipulate nodes:
183197
@@ -213,7 +227,7 @@ root.pprint(index=True)
213227print (root[9 ])
214228# Node(5)
215229
216- # Replace the node/subtree at index 4
230+ # Replace the node/subtree at index 4.
217231root[4 ] = Node(6 , left = Node(7 ), right = Node(8 ))
218232root.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.
230244del root[1 ]
231245root.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+
297348Check out the [ documentation] ( http://binarytree.readthedocs.io ) for more details.
0 commit comments