Trees#

class data_structures.trees.binary_tree.BinaryTree(value)[source]#

Bases: object

In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child…

__init__(value) None[source]#
static create_example_tree()[source]#

Build and return a complete binary tree with 15 nodes for testing.

pre_order() List[source]#
>>> root = BinaryTree.create_example_tree()
>>> root.pre_order()
[1, 2, 4, 8, 9, 5, 10, 11, 3, 6, 12, 13, 7, 14, 15]
pre_order_iterative() List[source]#
>>> root = BinaryTree.create_example_tree()
>>> root.pre_order_iterative()
[1, 2, 4, 8, 9, 5, 10, 11, 3, 6, 12, 13, 7, 14, 15]
in_order() List[source]#
>>> root = BinaryTree.create_example_tree()
>>> root.in_order()
[8, 4, 9, 2, 10, 5, 11, 1, 12, 6, 13, 3, 14, 7, 15]
in_order_iterative() List[source]#
>>> root = BinaryTree.create_example_tree()
>>> root.in_order_iterative()
[8, 4, 9, 2, 10, 5, 11, 1, 12, 6, 13, 3, 14, 7, 15]
post_order() List[source]#
>>> root = BinaryTree.create_example_tree()
>>> root.post_order()
[8, 9, 4, 10, 11, 5, 2, 12, 13, 6, 14, 15, 7, 3, 1]
post_order_iterative() List[source]#
>>> root = BinaryTree.create_example_tree()
>>> root.post_order_iterative()
[8, 9, 4, 10, 11, 5, 2, 12, 13, 6, 14, 15, 7, 3, 1]
level_order() List[source]#
>>> root = BinaryTree.create_example_tree()
>>> root.level_order()
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
depth()[source]#

The maximum depth of a binary tree is the number of nodes from the root down to the furthest leaf node. In other words, it is the height of a binary tree.

>>> root = BinaryTree.create_example_tree()
>>> root.depth()
4
class data_structures.trees.simple_tree.Tree[source]#

Bases: Dict

Basic implementation based on Dictionary