Skip to content

Commit d25a926

Browse files
kanthuccclauss
andauthored
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 <[email protected]>
1 parent e49ece9 commit d25a926

File tree

1 file changed

+65
-39
lines changed

1 file changed

+65
-39
lines changed

data_structures/binary_tree/basic_binary_tree.py

+65-39
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,85 @@
1+
from typing import Optional
2+
3+
14
class Node:
25
"""
3-
This is the Class Node with a constructor that contains data variable to type data
4-
and left, right pointers.
6+
A Node has data variable and pointers to Nodes to its left and right.
57
"""
6-
7-
def __init__(self, data):
8+
def __init__(self, data: int) -> None:
89
self.data = data
9-
self.left = None
10-
self.right = None
11-
10+
self.left: Optional[Node] = None
11+
self.right: Optional[Node] = None
1212

13-
def display(tree): # In Order traversal of the tree
1413

15-
if tree is None:
16-
return
17-
18-
if tree.left is not None:
14+
def display(tree: Optional[Node]) -> None: # In Order traversal of the tree
15+
"""
16+
>>> root = Node(1)
17+
>>> root.left = Node(0)
18+
>>> root.right = Node(2)
19+
>>> display(root)
20+
0
21+
1
22+
2
23+
>>> display(root.right)
24+
2
25+
"""
26+
if tree:
1927
display(tree.left)
20-
21-
print(tree.data)
22-
23-
if tree.right is not None:
28+
print(tree.data)
2429
display(tree.right)
2530

26-
return
27-
2831

29-
def depth_of_tree(
30-
tree,
31-
): # This is the recursive function to find the depth of binary tree.
32-
if tree is None:
33-
return 0
34-
else:
35-
depth_l_tree = depth_of_tree(tree.left)
36-
depth_r_tree = depth_of_tree(tree.right)
37-
if depth_l_tree > depth_r_tree:
38-
return 1 + depth_l_tree
39-
else:
40-
return 1 + depth_r_tree
32+
def depth_of_tree(tree: Optional[Node]) -> int:
33+
"""
34+
Recursive function that returns the depth of a binary tree.
35+
36+
>>> root = Node(0)
37+
>>> depth_of_tree(root)
38+
1
39+
>>> root.left = Node(0)
40+
>>> depth_of_tree(root)
41+
2
42+
>>> root.right = Node(0)
43+
>>> depth_of_tree(root)
44+
2
45+
>>> root.left.right = Node(0)
46+
>>> depth_of_tree(root)
47+
3
48+
>>> depth_of_tree(root.left)
49+
2
50+
"""
51+
return 1 + max(depth_of_tree(tree.left), depth_of_tree(tree.right)) if tree else 0
4152

4253

43-
def is_full_binary_tree(
44-
tree,
45-
): # This function returns that is it full binary tree or not?
46-
if tree is None:
47-
return True
48-
if (tree.left is None) and (tree.right is None):
54+
def is_full_binary_tree(tree: Node) -> bool:
55+
"""
56+
Returns True if this is a full binary tree
57+
58+
>>> root = Node(0)
59+
>>> is_full_binary_tree(root)
60+
True
61+
>>> root.left = Node(0)
62+
>>> is_full_binary_tree(root)
63+
False
64+
>>> root.right = Node(0)
65+
>>> is_full_binary_tree(root)
66+
True
67+
>>> root.left.left = Node(0)
68+
>>> is_full_binary_tree(root)
69+
False
70+
>>> root.right.right = Node(0)
71+
>>> is_full_binary_tree(root)
72+
False
73+
"""
74+
if not tree:
4975
return True
50-
if (tree.left is not None) and (tree.right is not None):
76+
if tree.left and tree.right:
5177
return is_full_binary_tree(tree.left) and is_full_binary_tree(tree.right)
5278
else:
53-
return False
79+
return not tree.left and not tree.right
5480

5581

56-
def main(): # Main function for testing.
82+
def main() -> None: # Main function for testing.
5783
tree = Node(1)
5884
tree.left = Node(2)
5985
tree.right = Node(3)

0 commit comments

Comments
 (0)