-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap_properties.py
80 lines (60 loc) · 2.25 KB
/
heap_properties.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
# Implementation of basic properties of a heap data structure such as the parent-child relationship
# Assuming the heap is stored in an array and it follows the 0-based indexing
class Heap:
"""
A class to illustrate the basic properties of a binary heap, such as the parent-child relationship.
This class is not a full implementation of a heap data structure. It only demonstrates how to calculate
the indices of parent and child nodes in a heap stored in an array.
Attributes:
heap (list): The list representation of the heap, with a placeholder at index 0 for 1-based indexing.
"""
def __init__(self):
"""Initialize an empty heap for illustration purposes."""
self.heap = [0] # Placeholder for 1-based indexing
def left(self, i: int) -> int:
"""Get index of left child node given
the index of the parent node.
Args:
i (int): index of the parent node
Returns:
int: index of the left child node
"""
return 2 * i
def right(self, i: int) -> int:
"""Get the index of the right child node given
the index of the parent node.
Args:
i (int): index of the parent node
Returns:
int: index of the right child node
"""
return 2 * i + 1
def parent(self, i: int) -> int:
"""Get the index of the parent node given
the index of the child node.
Args:
i (int): index of the child node
Returns:
int: index of the parent node
"""
return i // 2 # floor division
def peek(self) -> int:
"""Access the root node of the heap.
Raises:
IndexError: If the heap is empty or
only index 0 is present in the heap.
Returns:
int: The root node of the heap
"""
if len(self.heap) <= 1:
raise IndexError("Heap is empty")
return self.heap[1]
# Example usage
if __name__ == "__main__":
heap = (
Heap()
) # Initialize an empty heap, we can still access the parent-child relationship
print(heap.left(1)) # 2
print(heap.right(1)) # 3
print(heap.parent(3)) # 1
print(heap.peek()) # IndexError: Heap is empty