-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathnode.py
More file actions
24 lines (19 loc) · 903 Bytes
/
node.py
File metadata and controls
24 lines (19 loc) · 903 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Node:
id: int # Adding the type hint for the 'id' attribute
parent: int # Adding the type hint for the 'parent' attribute
# Initializes the Node with id and parent, ensuring valid values.
def __init__(self, id: int, parent: int):
if not isinstance(id, int) or not isinstance(parent, int):
raise ValueError("Both id and parent must be integers.")
if parent >= id:
raise ValueError("The parent cannot be greater than or equal to the id.")
self.id: int = id
self.parent: int = parent
# Returns a string representation of the Node.
def __repr__(self):
return f"Node({self.id}, {self.parent})"
# Compares two Nodes based on id and parent.
def __eq__(self, other):
if isinstance(other, Node):
return self.id == other.id and self.parent == other.parent
return False