Skip to content

Commit f02e2cf

Browse files
committed
feat(datastructures, trees): add depth, height and degree
1 parent 9c8c113 commit f02e2cf

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

datastructures/trees/binary/node.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ def __init__(
1818
key: Optional[Any] = None,
1919
parent: Optional["BinaryTreeNode"] = None,
2020
nxt: Optional["BinaryTreeNode"] = None,
21+
depth: Optional[int] = None,
22+
height: Optional[int] = None,
23+
degree: Optional[int] = None,
2124
) -> None:
2225
"""
2326
Constructor for BinaryTreeNode class. This will create a new node with the provided data and optional
@@ -35,7 +38,7 @@ def __init__(
3538
of the tree, then this is the next node on the next level starting from the left. If this is the last node
3639
in the tree, then this is None.
3740
"""
38-
super().__init__(data, key, parent)
41+
super().__init__(data, key, parent, depth, height, degree)
3942
self.left: Optional[BinaryTreeNode] = left
4043
self.right: Optional[BinaryTreeNode] = right
4144
# Next is a pointer that connects this node to it's right sibling in the tree. If this node is the right most

datastructures/trees/node.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ class TreeNode(Generic[T]):
1313
"""
1414

1515
def __init__(
16-
self, value: T, key: Optional[Any] = None, parent: Optional["TreeNode"] = None
16+
self,
17+
value: T,
18+
key: Optional[Any] = None,
19+
parent: Optional["TreeNode"] = None,
20+
depth: Optional[int] = None,
21+
height: Optional[int] = None,
22+
degree: Optional[int] = None,
1723
):
1824
"""
1925
Initialises a tree node with a value, key and a parent node.
@@ -23,16 +29,22 @@ def __init__(
2329
value (T): The value of the node
2430
key (Optional[Any]): The key of the node. If not provided, a hash of the data is used.
2531
parent (Optional[TreeNode]): The parent node of the current node.
32+
depth (int): The depth of the current node.
33+
height (int): The height of the current node.
34+
degree (int): The degree of the current node.
2635
2736
Notes:
2837
The key is used to identify the node in the tree. If not key is provided, a hash of the data is used.
2938
"""
3039
self.data = value
3140
self.key = key or hash(value)
3241
self.parent = parent
42+
self.depth = depth
43+
self.height = height
44+
self.degree = degree
3345

3446
def __repr__(self):
35-
return f"TreeNode(data={self.data}, key={self.key})"
47+
return f"TreeNode(data={self.data}, key={self.key}, depth={self.depth}, height={self.height}, degree={self.degree})"
3648

3749
def __eq__(self, other: "TreeNode[T]") -> bool:
3850
"""Checks if this node is equal to another node based on the data they contain

0 commit comments

Comments
 (0)