Argh,
I need to create a Binary Tree with 8 layers.
These layers then read how many "children" it has on its branches, and based off of these numbers,
goes through tons of formulas.
MY PROBLEM:
How on earth do I cleanly create the tree with exactly 8 Layers?
What I mean by layer:
(example with 3 layers)
0 / \ 0 0 / / \ \ 0 0 0 0
and how do I give the Nodes IDs incrementedly?
1 / \ 2 3 / / \ \ 4 5 6 7
Im not experienced in Trees at all, as I was mostly able to simple use any form of arrays.
Well, this time I need the "branch" fact way too often...
I dont have the time to read this atm, its 2 am.
(i fell asleep at my GFs, meant to be home sooner)
Spodi, thank you so much...
will definetely work through this tommorrow!
Hope I get the time to post tommorrow already ![]()
Thank you so much!
Holy crap Spodi. God of algorithms much?
Well, the Tree I need will need to have values entered into each Node,
Thats why I want them to be adressable easily...
I thought a unique ID would be the easiest to do this.
I'll dig into that post & code later today, thats a hell of a lot for sure ![]()
Well usually the point with a tree is that you crawl it based on some criteria. For instance, a common usage is have the left node be "less than" and right node "greater than". Then, you crawl it until you find the value you want based on that recursive "less than"/"greater than" comparison.
Hmm...
Okay So I build the Binary Tree, and instead of holding a simple value,
each Node will hold an object (An Investment with details of who invested, how much, and what profit they made with this investment)
Calculating the Profit shouldnt be hard at all with the formulas I got from the guy,
and your base-tree there has everything I could think of needing.
Now one thing I'm wondering about...
the Nodes dont need IDs if the Investments have unique IDs or?
So I could just have each placed Investment get a new ID (incrementing)
and via recursive crawling I could simply look for the Node that contains the Investment.ID im looking for or?
EDIT: I need to display some layers and have these Nodes selectable.
(you click the Node and you get a menu what you want to invest into that position)
Just curious if this would require an ID system, or if I can work with the location of the Node...
i.e. how would you handle this?
P.S. when I read the fill tree, Do I get it right that it will first create the very left nodes
(with 2 children each), and then create the right nodes with its children?
Noone have time or an idea for this?
Does this help?
If you want to fill the tree completely to a certain depth, you simply do this:
As for IDs, it would be best if you avoid it. I can't see why you would want IDs for your tree, since its not like you can look it up by index. There are a few ways you can give unique IDs to your nodes, but an incrementing ID would not be recommended as it would require you to rebuild the IDs every time a node is added (what happens if you have a "gap" in your tree?).
However, if you want just a unique ID for each node, then you can do a reverse-crawling Huffman-encoding style counting. Basically, it works like this:
In code:
To test it:
We can even do a reverse-lookup by putting this in the BinaryTree class:
And to test it: