data_structures.binary_tree.binary_tree_traversals

Classes

Node

Functions

get_nodes_from_left_to_right(...)

Returns a list of nodes value from a particular level:

get_nodes_from_right_to_left(...)

Returns a list of nodes value from a particular level:

height(→ int)

Recursive function for calculating the height of the binary tree.

inorder(→ collections.abc.Generator[int])

In-order traversal visits left subtree, root node, right subtree.

level_order(→ collections.abc.Generator[int])

Returns a list of nodes value from a whole binary tree in Level Order Traverse.

main(→ None)

make_tree(→ Node | None)

The below tree

postorder(→ collections.abc.Generator[int])

Post-order traversal visits left subtree, right subtree, root node.

preorder(→ collections.abc.Generator[int])

Pre-order traversal visits root node, left subtree, right subtree.

reverse_inorder(→ collections.abc.Generator[int])

Reverse in-order traversal visits right subtree, root node, left subtree.

zigzag(→ collections.abc.Generator[int])

ZigZag traverse:

Module Contents

class data_structures.binary_tree.binary_tree_traversals.Node
data: int
left: Node | None = None
right: Node | None = None
data_structures.binary_tree.binary_tree_traversals.get_nodes_from_left_to_right(root: Node | None, level: int) collections.abc.Generator[int]

Returns a list of nodes value from a particular level: Left to right direction of the binary tree. >>> list(get_nodes_from_left_to_right(make_tree(), 1)) [1] >>> list(get_nodes_from_left_to_right(make_tree(), 2)) [2, 3]

data_structures.binary_tree.binary_tree_traversals.get_nodes_from_right_to_left(root: Node | None, level: int) collections.abc.Generator[int]

Returns a list of nodes value from a particular level: Right to left direction of the binary tree. >>> list(get_nodes_from_right_to_left(make_tree(), 1)) [1] >>> list(get_nodes_from_right_to_left(make_tree(), 2)) [3, 2]

data_structures.binary_tree.binary_tree_traversals.height(root: Node | None) int

Recursive function for calculating the height of the binary tree. >>> height(None) 0 >>> height(make_tree()) 3

data_structures.binary_tree.binary_tree_traversals.inorder(root: Node | None) collections.abc.Generator[int]

In-order traversal visits left subtree, root node, right subtree. >>> list(inorder(make_tree())) [4, 2, 5, 1, 3]

data_structures.binary_tree.binary_tree_traversals.level_order(root: Node | None) collections.abc.Generator[int]

Returns a list of nodes value from a whole binary tree in Level Order Traverse. Level Order traverse: Visit nodes of the tree level-by-level. >>> list(level_order(make_tree())) [1, 2, 3, 4, 5]

data_structures.binary_tree.binary_tree_traversals.main() None
data_structures.binary_tree.binary_tree_traversals.make_tree() Node | None
The below tree

1

/

2 3

/

4 5

data_structures.binary_tree.binary_tree_traversals.postorder(root: Node | None) collections.abc.Generator[int]

Post-order traversal visits left subtree, right subtree, root node. >>> list(postorder(make_tree())) [4, 5, 2, 3, 1]

data_structures.binary_tree.binary_tree_traversals.preorder(root: Node | None) collections.abc.Generator[int]

Pre-order traversal visits root node, left subtree, right subtree. >>> list(preorder(make_tree())) [1, 2, 4, 5, 3]

data_structures.binary_tree.binary_tree_traversals.reverse_inorder(root: Node | None) collections.abc.Generator[int]

Reverse in-order traversal visits right subtree, root node, left subtree. >>> list(reverse_inorder(make_tree())) [3, 1, 5, 2, 4]

data_structures.binary_tree.binary_tree_traversals.zigzag(root: Node | None) collections.abc.Generator[int]

ZigZag traverse: Returns a list of nodes value from left to right and right to left, alternatively. >>> list(zigzag(make_tree())) [1, 3, 2, 4, 5]