Skip to content

Commit bbd44bd

Browse files
committed
Fix formula for left child and right child in max heap
1 parent 3340d43 commit bbd44bd

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

03. Data Structures/Trees/Heap.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ def parent(self, pos):
2727

2828
#Method to return the position of the left child for the node currently at pos
2929
def left_child(self, pos):
30-
return 2 * pos
30+
return (2 * pos) + 1
3131

3232
#Method to return the position of the right child for the node currently at pos
3333
def right_child(self, pos):
34-
return (2 * pos) + 1
34+
return (2 * pos) + 2
3535

3636
#Method that returns true if the passed node is a leaf node.
3737
#All the nodes in the second half of the heap(when viewed as an array) are leaf nodes.
@@ -83,8 +83,8 @@ def insert(self, element):
8383
def print_heap(self):
8484
for i in range(1, (self.size // 2) + 1):
8585
print(" PARENT : " + str(self.Heap[i]) + " LEFT CHILD : " +
86-
str(self.Heap[2 * i]) + " RIGHT CHILD : " +
87-
str(self.Heap[2 * i + 1]))
86+
str(self.Heap[(2 * i) + 1]) + " RIGHT CHILD : " +
87+
str(self.Heap[(2 * i) + 2]))
8888

8989
#Method to remove and return the maximum element from the heap . The maximum element will be at the root.
9090
#So we will copy the element at the end of the heap into the root node and delete the last node, which will leave the heap property disturbed

0 commit comments

Comments
 (0)