forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_binary_tree.py
115 lines (101 loc) · 3.06 KB
/
basic_binary_tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from __future__ import annotations
class Node:
"""
A Node has data variable and pointers to Nodes to its left and right.
"""
def __init__(self, data: int) -> None:
self.data = data
self.left: Node | None = None
self.right: Node | None = None
class BinaryTree:
"""
A BinaryTree has a root node and methods for creating and manipulating binary trees.
"""
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 = 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: ")
tree.display(tree.root)
if __name__ == "__main__":
main()