In: Computer Science
Write a function parent_index_3_heap(child_index) that returns the index of the parent of the node at the given child_index. A None value should be returned if the child has no parent.
Notes:
This is for Python
Logic : A heap is always stored in an array as the level order traversal of complete binary tree. For every kth element of the array (except 0th index) :
Have a look athe below complete binary tree:
for index 8 the parent will be 8/2 = 4.
for index 23 the parent will be 23/2 = 11 and so on...
Have a look at the below code. I have put comments wherever required for better understanding.
Happy Learning!