|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | + |
1 | 4 | class Node: |
2 | 5 | """ |
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. |
5 | 7 | """ |
6 | | - |
7 | | - def __init__(self, data): |
| 8 | + def __init__(self, data: int) -> None: |
8 | 9 | self.data = data |
9 | | - self.left = None |
10 | | - self.right = None |
11 | | - |
| 10 | + self.left: Optional[Node] = None |
| 11 | + self.right: Optional[Node] = None |
12 | 12 |
|
13 | | -def display(tree): # In Order traversal of the tree |
14 | 13 |
|
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: |
19 | 27 | display(tree.left) |
20 | | - |
21 | | - print(tree.data) |
22 | | - |
23 | | - if tree.right is not None: |
| 28 | + print(tree.data) |
24 | 29 | display(tree.right) |
25 | 30 |
|
26 | | - return |
27 | | - |
28 | 31 |
|
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 |
41 | 52 |
|
42 | 53 |
|
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: |
49 | 75 | return True |
50 | | - if (tree.left is not None) and (tree.right is not None): |
| 76 | + if tree.left and tree.right: |
51 | 77 | return is_full_binary_tree(tree.left) and is_full_binary_tree(tree.right) |
52 | 78 | else: |
53 | | - return False |
| 79 | + return not tree.left and not tree.right |
54 | 80 |
|
55 | 81 |
|
56 | | -def main(): # Main function for testing. |
| 82 | +def main() -> None: # Main function for testing. |
57 | 83 | tree = Node(1) |
58 | 84 | tree.left = Node(2) |
59 | 85 | tree.right = Node(3) |
|
0 commit comments