Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update binary_tree_traversals.py
  • Loading branch information
rajansh87 authored Oct 14, 2020
commit 85738c3c66973f0a4c6a3fb785c264b954bd318c
30 changes: 20 additions & 10 deletions data_structures/binary_tree/binary_tree_traversals.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def make_tree() -> Node:

def preorder(root):
"""
PreOrder traversal: visit root node then its left subtree followed by right subtree.

PreOrder traversal:visit root then its
leftsubtree followed by right subtree.
>>> preorder(make_tree())
4 2 5 1 3
"""
Expand All @@ -33,7 +33,9 @@ def preorder(root):

def postorder(root):
"""
PostOrder traversal: visit left subtree followed by right subtree and then root node.
PostOrder traversal:visit leftsubtree
followed by right subtree and
then root node.
"""
if root:
postorder(root.left)
Expand All @@ -43,7 +45,9 @@ def postorder(root):

def inorder(root):
"""
InOrder traversal: visit its left subtree followed by root node and then right subtree.
InOrder traversal: visit leftsubtree
followed by root node and
then right subtree.
"""
if root:
inorder(root.left)
Expand All @@ -53,7 +57,8 @@ def inorder(root):

def height(root):
"""
Recursive function for calculating height of the binary tree.
Recursive function for calculating
height of the binary tree.
"""
if not root:
return 0
Expand All @@ -68,7 +73,8 @@ def height(root):
def levelorder1(root):
"""
Print whole binary tree in Level Order Traverse.
Level Order traverse: Visit nodes of the tree level-by-level.
Level Order traverse: Visit nodes of
the tree level-by-level.
"""
if not root:
return
Expand All @@ -86,7 +92,8 @@ def levelorder1(root):
def levelorder2(root, level):
"""
Level-wise traversal:
Print all nodes present at the given level of the binary tree.
Print all nodes present at the
given level of the binary tree.
"""
if not root:
return root
Expand All @@ -99,7 +106,8 @@ def levelorder2(root, level):

def print_left_to_right(root, level):
"""
Print elements on particular level from left to right direction of the binary tree.
Print elements on particular level from
left to right direction of the binary tree.
"""
if not root:
return
Expand All @@ -112,7 +120,8 @@ def print_left_to_right(root, level):

def print_right_to_left(root, level):
"""
Print elements on particular level from right to left direction of the binary tree.
Print elements on particular level from
right to left direction of the binary tree.
"""
if not root:
return
Expand All @@ -125,7 +134,8 @@ def print_right_to_left(root, level):

def zigzag(root):
"""
ZigZag traverse: Print node left to right and right to left, alternatively.
ZigZag traverse: Print node left to right
and right to left, alternatively.
"""
flag = 0
height_tree = height(root)
Expand Down