2

The problem: I need to be able to recursively retrieve a node from a perfect binary tree of unknown height via an index.

Because of the unknown height property it seems that the only form of indexing that makes sense is breadth-first indexing (as per title):

          0
    1           2
3       4   5       6

The problem is that at each node it seems difficult to know which direction to take, and how to transform the index in my recursive request to that child node... or maybe I'm just not thinking clearly.

Node Navigate(Index):
Index 0: return this;
Index 1: return Left.Navigate(0);
Index 2: return Right.Navigate(0);
Index 3: return Left.Navigate(1);
Index 4: return Left.Navigate(2);
Index 5: return Right.Navigate(1);
Index 6: return Right.Navigate(2);
...
Index 7: return Left.Navigate(3);
Index 8: return Left.Navigate(4);
Index 9: return Left.Navigate(5);
Index 10: return Left.Navigate(6);
Index 11: return Right.Navigate(3);
Index 12: return Right.Navigate(4);
Index 13: return Right.Navigate(5);
Index 14: return Right.Navigate(6);

The pattern is clear - but how can one programatically - without consuming too many clock cycles (this is an embedded device) - select a node from Index and transform Index to a parameter for Navigate for that node? Am I missing an easy transformation?


Here's the implementation I ended up using, building on yurib's answer:

public class Node
{
  public Node Left, Right;

  public Node(int levels)
  {
      if (levels == 0) return;
      Left = new Node(levels - 1);
      Right = new Node(levels - 1);
  }

  public Node Navigate(int index)
  {
      if (index == 0) return this;

      // we want 1 based indexing.
      int oneBased = index + 1;
      // level is how many levels deep we are looking, stored as 1 << depth.
      int level = 1;  
      // find level - it's equal to the highest bit in "oneBased". Find it.
      for (int i = oneBased; (i >>= 1) != 0; )
      {
          level *= 2;
      }

      // level adjusted for our children.
      int subLevel = level >> 1;
      // clear our level bit, set our children's level bit.
      int childIndex = ((oneBased & ~level) | subLevel) - 1;

      // is the node we're looking for over half way through level? go right.
      if ((oneBased & subLevel) != 0)
          return Right.Navigate(childIndex);
      else
          return Left.Navigate(childIndex);  // otherwise it's in our left tree.
  }
}

It's C# for testing, although in reality each call to Navigate is processed on a different embedded device, hence the need for recursion instead of simply following the pseudocode, building a List etc. Thanks yurib :).

1 Answer 1

5

to find the n'th node follow the path created by dividing n repeatedly by two and keeping track of the remainder. follow the "route" created by the remainder in reverse when 1 means right and 0 means left.

for example, to add the 6'th item (index = 5):
6/2 = 3 (0)
3/2 = 1 (1)

that means from the root go right, left.

Sign up to request clarification or add additional context in comments.

1 Comment

The key part is "in reverse".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.