From 1a3ba30abd4ed8ab64309fc9d0bee7482139edc2 Mon Sep 17 00:00:00 2001 From: dasdebanna Date: Tue, 26 Sep 2023 18:37:24 +0530 Subject: [PATCH 1/3] Updated basic_binary_tree.py The BinaryTree class has been created with the root node as its only argument. The utility functions have been modified to be methods of the BinaryTree class. The display, depth_of_tree, and is_full_binary_tree functions now take self as their first argument. The main function has been modified to create a BinaryTree object and call its methods instead of calling the utility functions directly. --- .../binary_tree/basic_binary_tree.py | 160 +++++++++--------- 1 file changed, 83 insertions(+), 77 deletions(-) diff --git a/data_structures/binary_tree/basic_binary_tree.py b/data_structures/binary_tree/basic_binary_tree.py index 65dccf247b51..63afa6cd35f2 100644 --- a/data_structures/binary_tree/basic_binary_tree.py +++ b/data_structures/binary_tree/basic_binary_tree.py @@ -12,90 +12,96 @@ 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: - """ - 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 - """ - 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 __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: # 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__": - main() + main() \ No newline at end of file From dfb5aeba0acb4ddbced0d8032df8be892d15fde5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 26 Sep 2023 13:13:07 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/binary_tree/basic_binary_tree.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/data_structures/binary_tree/basic_binary_tree.py b/data_structures/binary_tree/basic_binary_tree.py index 63afa6cd35f2..2f331d9bc637 100644 --- a/data_structures/binary_tree/basic_binary_tree.py +++ b/data_structures/binary_tree/basic_binary_tree.py @@ -56,7 +56,11 @@ def depth_of_tree(self, tree: Node | None) -> int: >>> 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 + 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: """ @@ -81,7 +85,9 @@ def is_full_binary_tree(self, tree: Node) -> bool: 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) + 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 @@ -104,4 +110,4 @@ def main() -> None: # Main function for testing. if __name__ == "__main__": - main() \ No newline at end of file + main() From 906e6fe36f00fbd019f63bd3e7abe598e81b81ce Mon Sep 17 00:00:00 2001 From: dasdebanna Date: Tue, 26 Sep 2023 19:26:26 +0530 Subject: [PATCH 3/3] Updared basic_binary_tree.py The BinaryTree class has been created with the root node as its only argument. The utility functions have been modified to be methods of the BinaryTree class. The display, depth_of_tree, and is_full_binary_tree functions now take self as their first argument. The main function has been modified to create a BinaryTree object and call its methods instead of calling the utility functions directly. --- data_structures/binary_tree/basic_binary_tree.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/data_structures/binary_tree/basic_binary_tree.py b/data_structures/binary_tree/basic_binary_tree.py index 63afa6cd35f2..b0bdc5acbac9 100644 --- a/data_structures/binary_tree/basic_binary_tree.py +++ b/data_structures/binary_tree/basic_binary_tree.py @@ -20,8 +20,9 @@ class BinaryTree: 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 + 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) @@ -40,7 +41,6 @@ def display(self, tree: Node | None) -> None: # In Order traversal of the tree 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 @@ -61,7 +61,6 @@ def depth_of_tree(self, tree: Node | None) -> int: 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 @@ -86,7 +85,10 @@ def is_full_binary_tree(self, tree: Node) -> bool: return not tree.left and not tree.right -def main() -> None: # Main function for testing. +def main() -> None: + """ + Main function for testing. + """ tree = BinaryTree(1) tree.root.left = Node(2) tree.root.right = Node(3)