0

I am reading about Heaps, which describes that you can do the operations of accessing the left child, the RIGHT/LEFT child and the PARENT with bit shift operations. While Left and Parent seems trivial i am not sure with the right one. Do i just have to add one?

Here is an excerpt from the book: MIT Introduciton to algorithms:

"Similarly, the RIGHT procedure can quickly compute 2i + 1 by shifting the binary representation of i left by one bit position and then adding in a 1 as the low-order bit".

MIT Introduction to Algorithms - Heap

Access Operations:

LEFT: 2*i

i<<1

RIGHT: 2*i+1

(i<<1)+1

PARENT: i/2

i>>1
user1767754
  • 23,311
  • 18
  • 141
  • 164
  • What's wrong? The right child of the node #3 is the node #7, which is `(3 << 1) + 1` **(note the `<< 1`, since `<< 2` multiplies the N by 4)** – awesoon Aug 31 '16 at 06:19
  • adding in a 1 as the low-order bit in the book sounded for me something more fancy then just doing a **+1** – user1767754 Aug 31 '16 at 06:29
  • The question still a bit unclear for me, but I tried to explain the logic behind `N*2` and `N*2 + 1`. Tell me, if you need more details or explanations – awesoon Aug 31 '16 at 06:49
  • It's clear...i just didn't do much with bitshifting, i was wondering of my operations are correct. – user1767754 Aug 31 '16 at 06:53
  • Updated answer with notes about bit shifting operations – awesoon Aug 31 '16 at 07:16

2 Answers2

0

This is how heap works - for every node you could easily get:

  1. Parent - just divide the node index by two (N / 2)
  2. Left child - multiply index by two (N * 2)
  3. Right child - multiply index by two and increase new index by one (N * 2 + 1)

It is easy to prove, that two distinct nodes cannot have same child.

Suppose, N1, N2 and C are heap nodes. N1 != N2 and C.is_child_of(N1) and C.is_child_of(N2), where C.is_child_of(N) returns true when C is either right or left child of N. Then:

  1. If C is the left child of both N1 and N2, then N1 * 2 = N2 * 2 <=> N1 = N2
  2. Similarly, if C is the right child of both N1 and N2, then N1 * 2 + 1 = N2 * 2 + 1 <=> N1 = N2
  3. If C is the left child of N1 and C is the right child of N2, then N1 * 2 = N2 * 2 + 1 which is incorrect, because N1 * 2 is even and N2 * 2 + 1 is odd.

Note, that your bitwise access operations are incorrect - you should shift indices by one, not by two, because N << M is N * 2^M. I'd also suggest you to use plain division / multiplication instead of bit shifting - the compiler knows how to optimize your code.

awesoon
  • 32,469
  • 11
  • 74
  • 99
  • 2
    Note that this is only true if the root of your heap is at index 1 in the array. For a 0-based heap the parent is at `(n-1)/2`, the left child is at `(n*2)+1`, and the right child is at `(n*2)+2`. – Jim Mischel Aug 31 '16 at 11:34
0

A left shift by one bit position on an Int i is equivalent to 2*i

If we consider i as the index of an array:
- the index of the left child can be obtained using:

i << 1

-the index of the right child

(i<<1)+1

-The parent:

i>>1

(equivalent to i/2)

Don't forget we consider on this case that the initial index of the array is 1.

pacholik
  • 8,607
  • 9
  • 43
  • 55