mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
adding static type checking to basic_binary_tree.py (#2293)
* adding static type checking to basic_binary_tree.py * Add static type checking to functions with None return type * Applying code review comments * Added missing import statement * fix spaciing * "cleaned up depth_of_tree" * Add doctests and then streamline display() and is_full_binary_tree() Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
e49ece95a4
commit
d25a926c02
@ -1,59 +1,85 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
class Node:
|
class Node:
|
||||||
"""
|
"""
|
||||||
This is the Class Node with a constructor that contains data variable to type data
|
A Node has data variable and pointers to Nodes to its left and right.
|
||||||
and left, right pointers.
|
|
||||||
"""
|
"""
|
||||||
|
def __init__(self, data: int) -> None:
|
||||||
def __init__(self, data):
|
|
||||||
self.data = data
|
self.data = data
|
||||||
self.left = None
|
self.left: Optional[Node] = None
|
||||||
self.right = None
|
self.right: Optional[Node] = None
|
||||||
|
|
||||||
|
|
||||||
def display(tree): # In Order traversal of the tree
|
def display(tree: Optional[Node]) -> None: # In Order traversal of the tree
|
||||||
|
"""
|
||||||
if tree is None:
|
>>> root = Node(1)
|
||||||
return
|
>>> root.left = Node(0)
|
||||||
|
>>> root.right = Node(2)
|
||||||
if tree.left is not None:
|
>>> display(root)
|
||||||
|
0
|
||||||
|
1
|
||||||
|
2
|
||||||
|
>>> display(root.right)
|
||||||
|
2
|
||||||
|
"""
|
||||||
|
if tree:
|
||||||
display(tree.left)
|
display(tree.left)
|
||||||
|
print(tree.data)
|
||||||
print(tree.data)
|
|
||||||
|
|
||||||
if tree.right is not None:
|
|
||||||
display(tree.right)
|
display(tree.right)
|
||||||
|
|
||||||
return
|
|
||||||
|
def depth_of_tree(tree: Optional[Node]) -> 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 depth_of_tree(
|
def is_full_binary_tree(tree: Node) -> bool:
|
||||||
tree,
|
"""
|
||||||
): # This is the recursive function to find the depth of binary tree.
|
Returns True if this is a full 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
|
|
||||||
|
|
||||||
|
>>> root = Node(0)
|
||||||
def is_full_binary_tree(
|
>>> is_full_binary_tree(root)
|
||||||
tree,
|
True
|
||||||
): # This function returns that is it full binary tree or not?
|
>>> root.left = Node(0)
|
||||||
if tree is None:
|
>>> 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
|
return True
|
||||||
if (tree.left is None) and (tree.right is None):
|
if tree.left and tree.right:
|
||||||
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)
|
return is_full_binary_tree(tree.left) and is_full_binary_tree(tree.right)
|
||||||
else:
|
else:
|
||||||
return False
|
return not tree.left and not tree.right
|
||||||
|
|
||||||
|
|
||||||
def main(): # Main function for testing.
|
def main() -> None: # Main function for testing.
|
||||||
tree = Node(1)
|
tree = Node(1)
|
||||||
tree.left = Node(2)
|
tree.left = Node(2)
|
||||||
tree.right = Node(3)
|
tree.right = Node(3)
|
||||||
|
Loading…
Reference in New Issue
Block a user