@@ -11,42 +11,42 @@ class MaxArrayBasedHeap(ArrayBasedHeap):
1111 def __init__ (self ):
1212 super ().__init__ ()
1313
14- def insert_data (self , value : Any ):
14+ def insert (self , value : Any ):
1515 """
1616 Inserts a value into the heap
1717 """
1818 # add the value into the last node
19- self .data .append (value )
19+ self .heap .append (value )
2020
2121 # keep track of the index of the newly inserted node
22- new_node_index = len (self .data ) - 1
22+ new_node_index = len (self .heap ) - 1
2323
2424 # the following executes the "trickle up" algorithm. If the new node is not in the root position and it's greater
2525 # than its parent node
2626 while (
2727 new_node_index > 0
28- and self .data [new_node_index ]
29- > self .data [self .get_parent_index (new_node_index )]
28+ and self .heap [new_node_index ]
29+ > self .heap [self .get_parent_index (new_node_index )]
3030 ):
3131 # swap the new node with the parent node
3232 (
33- self .data [self .get_parent_index (new_node_index )],
34- self .data [new_node_index ],
33+ self .heap [self .get_parent_index (new_node_index )],
34+ self .heap [new_node_index ],
3535 ) = (
36- self .data [new_node_index ],
37- self .data [self .get_parent_index (new_node_index )],
36+ self .heap [new_node_index ],
37+ self .heap [self .get_parent_index (new_node_index )],
3838 )
3939
4040 # update the index of the new node
4141 new_node_index = self .get_parent_index (new_node_index )
4242
4343 def delete (self ) -> Any :
44- if len (self .data ) == 0 :
44+ if len (self .heap ) == 0 :
4545 raise Exception ("Heap is empty" )
4646
4747 # we only ever delete the root node from a heap, so we pop the last node from the array and make it the root node
48- root_node = self .data [0 ]
49- self .data [0 ] = self .data .pop ()
48+ root_node = self .heap [0 ]
49+ self .heap [0 ] = self .heap .pop ()
5050
5151 # track the current index of the "trickle node". This is the node that will be moved into the correct position
5252 trickle_node_index = 0
@@ -57,9 +57,9 @@ def delete(self) -> Any:
5757 larger_child_index = self .__calculate_larger_child_index (trickle_node_index )
5858
5959 # swap the trickle node with its larger child
60- self .data [trickle_node_index ], self .data [larger_child_index ] = (
61- self .data [larger_child_index ],
62- self .data [trickle_node_index ],
60+ self .heap [trickle_node_index ], self .heap [larger_child_index ] = (
61+ self .heap [larger_child_index ],
62+ self .heap [trickle_node_index ],
6363 )
6464
6565 trickle_node_index = larger_child_index
@@ -76,20 +76,20 @@ def __has_greater_child(self, index: int) -> bool:
7676 left_child_index = self .get_left_child_index (index )
7777 right_child_index = self .get_right_child_index (index )
7878
79- left_child_exists = left_child_index < len (self .data )
80- right_child_exists = right_child_index < len (self .data )
79+ left_child_exists = left_child_index < len (self .heap )
80+ right_child_exists = right_child_index < len (self .heap )
8181
8282 if left_child_exists and right_child_exists :
83- left_child = self .data [left_child_index ]
84- right_child = self .data [right_child_index ]
83+ left_child = self .heap [left_child_index ]
84+ right_child = self .heap [right_child_index ]
8585
86- return left_child > self .data [index ] or right_child > self .data [index ]
86+ return left_child > self .heap [index ] or right_child > self .heap [index ]
8787 elif left_child_exists and not right_child_exists :
88- left_child = self .data [left_child_index ]
89- return left_child > self .data [index ]
88+ left_child = self .heap [left_child_index ]
89+ return left_child > self .heap [index ]
9090 elif right_child_exists and not left_child_exists :
91- right_child = self .data [right_child_index ]
92- return right_child > self .data [index ]
91+ right_child = self .heap [right_child_index ]
92+ return right_child > self .heap [index ]
9393 else :
9494 return False
9595
@@ -100,14 +100,14 @@ def __calculate_larger_child_index(self, index: int) -> int:
100100 :return: The position of the larger child
101101 """
102102 # if there is no right child
103- if not self .data [self .get_right_child_index (index )]:
103+ if not self .heap [self .get_right_child_index (index )]:
104104 # return the left child index
105105 return self .get_left_child_index (index )
106106
107107 # if right child value is greater than left child value
108108 if (
109- self .data [self .get_right_child_index (index )]
110- > self .data [self .get_left_child_index (index )]
109+ self .heap [self .get_right_child_index (index )]
110+ > self .heap [self .get_left_child_index (index )]
111111 ):
112112 # return the right child index
113113 return self .get_right_child_index (index )
0 commit comments