From 99aba7901314136a662c10399fac1a27ad8f8719 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Tue, 11 Aug 2020 22:33:06 +0200 Subject: [PATCH] type hints, docstring, depth_of_tree more straightforward --- .../binary_tree/basic_binary_tree.py | 39 +++++++++---------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/data_structures/binary_tree/basic_binary_tree.py b/data_structures/binary_tree/basic_binary_tree.py index 9b6e25d5ec56..e6b2e757cbef 100644 --- a/data_structures/binary_tree/basic_binary_tree.py +++ b/data_structures/binary_tree/basic_binary_tree.py @@ -3,15 +3,16 @@ class Node: This is the Class Node with a constructor that contains data variable to type data and left, right pointers. """ - - def __init__(self, data): + def __init__(self, data: int): self.data = data self.left = None self.right = None -def display(tree): # In Order traversal of the tree - +def display(tree: Node): + """ + Display in order traversal of the tree. + """ if tree is None: return @@ -26,34 +27,32 @@ def display(tree): # In Order traversal of the tree return -def depth_of_tree( - tree, -): # This is the recursive function to find the depth of binary tree. +def depth_of_tree(tree: Node) -> int: + """ + This is the recursive function to find the depth of binary tree. + """ if tree is None: return 0 - else: - depth_l_tree = depth_of_tree(tree.left) - depth_r_tree = depth_of_tree(tree.right) - if depth_l_tree > depth_r_tree: - return 1 + depth_l_tree - else: - return 1 + depth_r_tree + return max(depth_of_tree(tree.left), depth_of_tree(tree.right)) + 1 -def is_full_binary_tree( - tree, -): # This function returns that is it full binary tree or not? +def is_full_binary_tree(tree: Node) -> bool: + """ + This function returns that is it full binary tree or not. + """ if tree is None: return True if (tree.left is None) and (tree.right is None): return True if (tree.left is not None) and (tree.right is not None): return is_full_binary_tree(tree.left) and is_full_binary_tree(tree.right) - else: - return False + return False -def main(): # Main function for testing. +def main(): + """ + Main function for testing. + """ tree = Node(1) tree.left = Node(2) tree.right = Node(3)