Skip to content

Updated basic_binary_tree.py #9092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 91 additions & 77 deletions data_structures/binary_tree/basic_binary_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,89 +12,103 @@ def __init__(self, data: int) -> None:
self.right: Node | None = None


def display(tree: Node | None) -> None: # In Order traversal of the tree
class BinaryTree:
"""
>>> root = Node(1)
>>> root.left = Node(0)
>>> root.right = Node(2)
>>> display(root)
0
1
2
>>> display(root.right)
2
A BinaryTree has a root node and methods for creating and manipulating binary trees.
"""
if tree:
display(tree.left)
print(tree.data)
display(tree.right)


def depth_of_tree(tree: Node | None) -> int:
"""
Recursive function that returns the depth of a binary tree.

>>> root = Node(0)
>>> depth_of_tree(root)
1
>>> root.left = Node(0)
>>> depth_of_tree(root)
2
>>> root.right = Node(0)
>>> depth_of_tree(root)
2
>>> root.left.right = Node(0)
>>> depth_of_tree(root)
3
>>> depth_of_tree(root.left)
2
"""
return 1 + max(depth_of_tree(tree.left), depth_of_tree(tree.right)) if tree else 0


def is_full_binary_tree(tree: Node) -> bool:
def __init__(self, root_data: int) -> None:
self.root = Node(root_data)

def display(self, tree: Node | None) -> None:
"""
In Order traversal of the tree
>>> tree = BinaryTree(1)
>>> tree.root.left = Node(0)
>>> tree.root.right = Node(2)
>>> tree.display(tree.root)
0
1
2
>>> tree.display(tree.root.right)
2
"""
if tree:
self.display(tree.left)
print(tree.data)
self.display(tree.right)

def depth_of_tree(self, tree: Node | None) -> int:
"""
Recursive function that returns the depth of a binary tree.
>>> tree = BinaryTree(0)
>>> tree.depth_of_tree(tree.root)
1
>>> tree.root.left = Node(0)
>>> tree.depth_of_tree(tree.root)
2
>>> tree.root.right = Node(0)
>>> tree.depth_of_tree(tree.root)
2
>>> tree.root.left.right = Node(0)
>>> tree.depth_of_tree(tree.root)
3
>>> tree.depth_of_tree(tree.root.left)
2
"""
return (
1 + max(self.depth_of_tree(tree.left), self.depth_of_tree(tree.right))
if tree
else 0
)

def is_full_binary_tree(self, tree: Node) -> bool:
"""
Returns True if this is a full binary tree
>>> tree = BinaryTree(0)
>>> tree.is_full_binary_tree(tree.root)
True
>>> tree.root.left = Node(0)
>>> tree.is_full_binary_tree(tree.root)
False
>>> tree.root.right = Node(0)
>>> tree.is_full_binary_tree(tree.root)
True
>>> tree.root.left.left = Node(0)
>>> tree.is_full_binary_tree(tree.root)
False
>>> tree.root.right.right = Node(0)
>>> tree.is_full_binary_tree(tree.root)
False
"""
if not tree:
return True
if tree.left and tree.right:
return self.is_full_binary_tree(tree.left) and self.is_full_binary_tree(
tree.right
)
else:
return not tree.left and not tree.right


def main() -> None:
"""
Returns True if this is a full binary tree

>>> root = Node(0)
>>> is_full_binary_tree(root)
True
>>> root.left = Node(0)
>>> is_full_binary_tree(root)
False
>>> root.right = Node(0)
>>> is_full_binary_tree(root)
True
>>> root.left.left = Node(0)
>>> is_full_binary_tree(root)
False
>>> root.right.right = Node(0)
>>> is_full_binary_tree(root)
False
Main function for testing.
"""
if not tree:
return True
if tree.left and tree.right:
return is_full_binary_tree(tree.left) and is_full_binary_tree(tree.right)
else:
return not tree.left and not tree.right


def main() -> None: # Main function for testing.
tree = Node(1)
tree.left = Node(2)
tree.right = Node(3)
tree.left.left = Node(4)
tree.left.right = Node(5)
tree.left.right.left = Node(6)
tree.right.left = Node(7)
tree.right.left.left = Node(8)
tree.right.left.left.right = Node(9)

print(is_full_binary_tree(tree))
print(depth_of_tree(tree))
tree = BinaryTree(1)
tree.root.left = Node(2)
tree.root.right = Node(3)
tree.root.left.left = Node(4)
tree.root.left.right = Node(5)
tree.root.left.right.left = Node(6)
tree.root.right.left = Node(7)
tree.root.right.left.left = Node(8)
tree.root.right.left.left.right = Node(9)

print(tree.is_full_binary_tree(tree.root))
print(tree.depth_of_tree(tree.root))
print("Tree is: ")
display(tree)
tree.display(tree.root)


if __name__ == "__main__":
Expand Down