Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
90b8c32
add binary_tree_traversals.py to data_structures
rajansh87 Oct 14, 2020
6193d6e
Fixed error
rajansh87 Oct 14, 2020
5b6f8d8
Update data_structures/binary_tree/binary_tree_traversals.py
rajansh87 Oct 14, 2020
f15601c
Update binary_tree_traversals.py
rajansh87 Oct 14, 2020
b5285c1
Update binary_tree_traversals.py
rajansh87 Oct 14, 2020
2c9050c
Update binary_tree_traversals.py
rajansh87 Oct 14, 2020
4140522
Update data_structures/binary_tree/binary_tree_traversals.py
rajansh87 Oct 14, 2020
85738c3
Update binary_tree_traversals.py
rajansh87 Oct 14, 2020
27d854f
Update binary_tree_traversals.py
rajansh87 Oct 14, 2020
5f4e7ce
Update binary_tree_traversals.py
rajansh87 Oct 14, 2020
854fbd5
Update binary_tree_traversals.py
rajansh87 Oct 15, 2020
c0dc50d
Update binary_tree_traversals.py
rajansh87 Oct 15, 2020
48e97aa
Update binary_tree_traversals.py
rajansh87 Oct 15, 2020
cf0f9e4
Update binary_tree_traversals.py
rajansh87 Oct 15, 2020
6c43555
Update binary_tree_traversals.py
rajansh87 Oct 15, 2020
449f686
Update binary_tree_traversals.py
rajansh87 Oct 15, 2020
5dc5534
Update binary_tree_traversals.py
rajansh87 Oct 15, 2020
655657d
Update data_structures/binary_tree/binary_tree_traversals.py
rajansh87 Oct 15, 2020
d5a0dba
Update binary_tree_traversals.py
rajansh87 Oct 15, 2020
cd5c08c
Update binary_tree_traversals.py
rajansh87 Oct 15, 2020
b266c71
Update data_structures/binary_tree/binary_tree_traversals.py
rajansh87 Oct 15, 2020
fe91a5a
Update data_structures/binary_tree/binary_tree_traversals.py
rajansh87 Oct 15, 2020
ef6871e
Update binary_tree_traversals.py
rajansh87 Oct 15, 2020
f1c3bfe
Update data_structures/binary_tree/binary_tree_traversals.py
rajansh87 Oct 15, 2020
6ae25e6
Update binary_tree_traversals.py
rajansh87 Oct 15, 2020
41a7dc1
Doctests and type hints
cclauss Oct 15, 2020
baa9eab
Add spaces
cclauss Oct 15, 2020
ce35469
Update binary_tree_traversals.py
rajansh87 Oct 15, 2020
15c812d
black exclude data_structures/binary_tree/binary_tree_traversals.py
cclauss Oct 15, 2020
80409c5
Add spaces again
cclauss Oct 15, 2020
b7ea116
Update binary_tree_traversals.py
cclauss Oct 15, 2020
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ repos:
rev: stable
hooks:
- id: black
exclude: |
(?x)^(
data_structures/binary_tree/binary_tree_traversals.py
)$
- repo: https://github.com/PyCQA/isort
rev: 5.5.3
hooks:
Expand Down
161 changes: 161 additions & 0 deletions data_structures/binary_tree/binary_tree_traversals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# https://en.wikipedia.org/wiki/Tree_traversal


class Node:
"""
A Node has data variable and pointers to its left and right nodes.
"""

def __init__(self, data):
self.left = None
self.right = None
self.data = data


def make_tree() -> Node:
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
return root


def preorder(root: Node):
"""
Pre-order traversal visits root node, left subtree, right subtree.
>>> preorder(make_tree())
[1, 2, 4, 5, 3]
"""
return [root.data] + preorder(root.left) + preorder(root.right) if root else []


def postorder(root: Node):
"""
Post-order traversal visits left subtree, right subtree, root node.
>>> postorder(make_tree())
[4, 5, 2, 3, 1]
"""
return postorder(root.left) + postorder(root.right) + [root.data] if root else []


def inorder(root: Node):
"""
In-order traversal visits left subtree, root node, right subtree.
>>> inorder(make_tree())
[4, 2, 5, 1, 3]
"""
return inorder(root.left) + [root.data] + inorder(root.right) if root else []


def height(root: Node):
"""
Recursive function for calculating the height of the binary tree.
>>> height(None)
0
>>> height(make_tree())
3
"""
return (max(height(root.left), height(root.right)) + 1) if root else 0


def level_order_1(root: Node):
"""
Print whole binary tree in Level Order Traverse.
Level Order traverse: Visit nodes of the tree level-by-level.
"""
if not root:
return
temp = root
que = [temp]
while len(que) > 0:
print(que[0].data, end=" ")
temp = que.pop(0)
if temp.left:
que.append(temp.left)
if temp.right:
que.append(temp.right)
return que


def level_order_2(root: Node, level: int):
"""
Level-wise traversal: Print all nodes present at the given level of the binary tree
"""
if not root:
return root
if level == 1:
print(root.data, end=" ")
elif level > 1:
level_order_2(root.left, level - 1)
level_order_2(root.right, level - 1)


def print_left_to_right(root: Node, level: int):
"""
Print elements on particular level from left to right direction of the binary tree.
"""
if not root:
return
if level == 1:
print(root.data, end=" ")
elif level > 1:
print_left_to_right(root.left, level - 1)
print_left_to_right(root.right, level - 1)


def print_right_to_left(root: Node, level: int):
"""
Print elements on particular level from right to left direction of the binary tree.
"""
if not root:
return
if level == 1:
print(root.data, end=" ")
elif level > 1:
print_right_to_left(root.right, level - 1)
print_right_to_left(root.left, level - 1)


def zigzag(root: Node):
"""
ZigZag traverse: Print node left to right and right to left, alternatively.
"""
flag = 0
height_tree = height(root)
for h in range(1, height_tree + 1):
if flag == 0:
print_left_to_right(root, h)
flag = 1
else:
print_right_to_left(root, h)
flag = 0


def main(): # Main function for testing.
"""
Create binary tree.
"""
root = make_tree()
"""
All Traversals of the binary are as follows:
"""
print(f" In-order Traversal is {inorder(root)}")
print(f" Pre-order Traversal is {preorder(root)}")
print(f"Post-order Traversal is {postorder(root)}")
print(f"Height of Tree is {height(root)}")
print("Complete Level Order Traversal is : ")
level_order_1(root)
print("\nLevel-wise order Traversal is : ")
for h in range(1, height(root) + 1):
level_order_2(root, h)
print("\nZigZag order Traversal is : ")
zigzag(root)
print()


if __name__ == "__main__":
import doctest

doctest.testmod()
main()