-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree.py
130 lines (106 loc) · 4.04 KB
/
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from __future__ import annotations
from typing import Any, Optional, Union
class Node:
"""
Node Class for the nodes of a Binary Tree
Functions:
insert_helper: Helper function to add node in a Binary Search Tree
height_helper: Helper function to calculate the height of a Binary Tree
num_nodes_helper: Helper function to calculate the number of Nodes in a Binary Tree
to_str: Helper function for __repr__
"""
def __init__(
self, val: int, left: Optional[Node] = None, right: Optional[Node] = None
) -> None:
self.val = val
self.left = left
self.right = right
def __eq__(self, other: Any) -> bool:
if type(other) == Node and self.val == other.val:
return self.left == other.left and self.right == other.right
return False
def __repr__(self) -> str:
return self.to_str()
def height_helper(self) -> int:
# Helper function to calculate the height of a Binary Tree
# Uses: height = max(left_height, right_height)
left_height, right_height = 0, 0
if self.left is not None:
left_height = self.left.height_helper()
if self.right is not None:
right_height = self.right.height_helper()
return max(left_height, right_height) + 1
def insert_helper(self, val: int) -> None:
# Helper function to add node in a Binary Search Tree
# Uses: BST property
# NOTE: Duplicate nodes are not added
if self.val > val:
if self.left is None:
self.left = Node(val)
else:
self.left.insert_helper(val)
elif self.val < val:
if self.right is None:
self.right = Node(val)
else:
self.right.insert_helper(val)
def num_nodes_helper(self) -> int:
# Helper function to calculate the number of Nodes in a Binary Tree
left, right = 0, 0
if self.left:
left = self.left.num_nodes_helper()
if self.right:
right = self.right.num_nodes_helper()
return left + right + 1
def to_str(self) -> str:
# Helper function for __repr__
# Returns all the childen in case 1 of them is not None, else returns only the
# value
if self.right is None and self.left is None:
return f"('{self.val}')"
elif self.left is not None and self.right is None:
return f"({self.left.to_str()}, '{self.val}', null)"
elif self.left is None and self.right is not None:
return f"(null, '{self.val}', {self.right.to_str()})"
elif self.left is not None and self.right is not None:
return f"({self.left.to_str()}, '{self.val}', {self.right.to_str()})"
class BinaryTree:
"""
Binary Tree Class
Functions:
find_height: Calculate the height of a Binary Tree (uses height_helper in the Node
Class)
NOTE: This class does not have the add node function and nodes have to be added
manually
"""
def __init__(self) -> None:
self.root = None
def __eq__(self, other: Any) -> bool:
if type(other) == BinaryTree:
return self.root == other.root
return False
def __len__(self) -> int:
if self.root:
return self.root.num_nodes_helper()
return 0
def __repr__(self) -> str:
return str(self.root)
def find_height(self) -> int:
# Calculate the height of a Binary Tree
if self.root:
return self.root.height_helper()
return 0
class BinarySearchTree(BinaryTree):
"""
Binary Tree Class (INHERITS FROM THE BinaryTree CLASS)
Functions:
add: Add nodes to a Binary Search Tree (uses insert_helper in the Node Class)
"""
def __init__(self) -> None:
BinaryTree.__init__(self)
def add(self, val: Union[int, str]) -> None:
# Add nodes to a Binary Search Tree
if self.root is None:
self.root = Node(val)
else:
self.root.insert_helper(val)