#I dont remember how was this mathmetacically done

1 messages · Page 1 of 1 (latest)

solar flower
#

im programming and i have a data structure that is a binary tree : like it starts with 1 branch it divides into 2 and each of the sons divide into 2 also till the end. the question is i have for example a size of 64 and 32 (0-63 index)
and from 0-31 are in the left of the first branch and the other 32 are in the right, and if i have for example the index 40 i have to go right first and then know how to recursively know if i have to go right again or left till it reaches the last branch assuring me that effectively its the number 40 taking in account it starts at 0 and ends in 63 so i dont remember how was that supposed to be done

magic cliffBOT
#
  1. Wait patiently for a helper to come along.
  2. Once someone helps you, say thank you and close the thread with:
+close
  1. Feel free to nominate the person for helper of the week in #helper-nominations
  2. Do not ping the mods, unless someone is breaking the rules.
  3. If you're happy with the help you got here, and the server overall, you can contribute financially as well:
twilit notch
#

You can use binary numbers to encode your position in the tree.

solar flower
#

wdym?

#

and cant i use normal numbers? because im supposed to do it that way

#

i remember it had smth to do with comparing the index to the half of the size and then decide if right or left and then do smth else and repeat

twilit notch
#

In other words, you look at your binary index of an item on a tree, starting from the left, and make turns: 0 means left, 1 means right.

solar flower
#

mb is not a binary tree but it can have as maximun 2 branches

#

but it doesnt have to be binary like 2 every time

#

and there should be easier way using normal numbers, thats gonna overcomplicate the code

twilit notch
#

Hm... Not sure, then. I don't really have much experience in coding - most I can do is some simple numerical stuff.

solar flower
#

if u can find a way for example

#

the index 32 should start at right

#

and then go all lefts

#

because if we divide the tree in 2 big

#

the 32 is the left most in the right big branch

#

or the 0 should be the most left in the left first branching

#

or 33 should go right then all lefts and then in the last one go right instead of left

solar flower
#

+close