0

Assuming I want to insert a node into a binary heap, how can i find the index of the node in the array that representing the heap after the insertion and the heapifying? I need to find this algorithm in O(log(log(n)).

Thank you all.

Nick Zuber
  • 5,467
  • 3
  • 24
  • 48
ChikChak
  • 936
  • 19
  • 44

1 Answers1

2

If you look at the insertion in a binary heap, the new node is put at the end, then moves upward his branch to find its rightful place.

So, as you know the size of the heap, you know in which branch the new node will be inserted. The branch size is log(n). From this position, you can also find all of the nodes belonging to the branch.

All you have to do is a binary search along this branch, and you'll find the place of the new node in log(log(n)).

T. Claverie
  • 11,380
  • 1
  • 17
  • 28
  • Ok,First of all,thank you very much. I have one more question-is there any way to know generally which index will it be? – ChikChak May 27 '16 at 20:18
  • No, it depends of the current heap and the value you want to insert, there is no certain way of finding the position. – T. Claverie May 27 '16 at 20:21