Create a Complete Binary Tree using Linked Lists w/o comparing node values

后端 未结 3 2008
心在旅途
心在旅途 2021-01-07 07:39

I am trying to create a complete binary tree using a linked list, instead of arraylist, without comparing node values. What I mean is on inserting a new value, I do not wish

3条回答
  •  独厮守ぢ
    2021-01-07 08:18

    Keep a count of how many items you have in the tree.

    Then, to add the nth item 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: where 1 means right and 0 means left.

    For example, to add the 11th item:

    11/2 = 5 (1)
    5/2 = 2 (1)
    2/2 = 1 (0)
    

    That means from the root, you'd go left, right, right.

提交回复
热议问题