Compare commits

...

10 Commits

Author SHA1 Message Date
Diwas Dahal
02fe6955e5
Update basic_binary_tree.py 2023-07-24 16:51:35 +05:45
pre-commit-ci[bot]
64939195c1 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2023-07-24 11:00:46 +00:00
Diwas Dahal
dc86773d6d
Update basic_binary_tree.py 2023-07-24 16:44:31 +05:45
pre-commit-ci[bot]
58637718ff [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2023-07-24 10:43:44 +00:00
Diwas Dahal
e33d860c0a
Update basic_binary_tree.py 2023-07-24 16:28:10 +05:45
pre-commit-ci[bot]
14682d100d [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2023-07-24 10:34:11 +00:00
Diwas Dahal
04e91d58d9
Update basic_binary_tree.py 2023-07-24 16:18:21 +05:45
Diwas Dahal
8ce8fcfc12
Update basic_binary_tree.py 2023-07-24 16:12:03 +05:45
Diwas Dahal
8341098d5b
Update data_structures/binary_tree/basic_binary_tree.py
Co-authored-by: Christian Clauss <cclauss@me.com>
2023-07-24 16:04:58 +05:45
Diwas Dahal
0f29543d3c
Update data_structures/binary_tree/basic_binary_tree.py
Co-authored-by: Christian Clauss <cclauss@me.com>
2023-07-24 16:04:36 +05:45

View File

@ -12,20 +12,38 @@ class Node:
self.right: Node | None = None self.right: Node | None = None
def display_tree(root: Node | None, level: int = 0) -> None: def display(tree: Node | None) -> None: # In Order traversal of the tree
""" """
>>> root = Node(1) >>> root = Node(1)
>>> root.left = Node(2) >>> root.left = Node(0)
>>> root.right = Node(3) >>> root.right = Node(2)
>>> display_tree(root) >>> display(root)
3 0
1 1
2 2
>>> display(root.right)
2
"""
if tree:
display(tree.left)
print(tree.data)
display(tree.right)
def display_using_in_order_traversal(root: Node | None, level: int = 0) -> None:
"""
>>> root = Node(1)
>>> root.left = Node(0)
>>> root.right = Node(2)
>>> display_tree(root)
2
1
0
""" """
if root: if root:
display_tree(root.right, level + 1) display_using_in_order_traversal(root.right, level + 1)
print(" " * level + str(root.data)) print(f"{' ' * level}{root.data}")
display_tree(root.left, level + 1) display_using_in_order_traversal(root.left, level + 1)
def depth_of_tree(tree: Node | None) -> int: def depth_of_tree(tree: Node | None) -> int:
@ -92,7 +110,8 @@ def main() -> None: # Main function for testing.
print(is_full_binary_tree(tree)) print(is_full_binary_tree(tree))
print(depth_of_tree(tree)) print(depth_of_tree(tree))
print("Tree is: ") print("Tree is: ")
display_tree(tree) display_using_in_order_traversal(tree)
display(tree)
if __name__ == "__main__": if __name__ == "__main__":