Okay so right now I have a program that is running. It makes a tree and each tree has a linked list attached to it.
The tree is somewhat binary but in a hierarchy form so its not in the correct number order. Basically each node is referred to as a "costcenter" and each costcenter has a linked list which are expenses which has a name and an amount.
It puts the tree and linked list together right now but im brainstorming on the second output. It outputs one just by adding all expenses up in the list and displaying it by the node - if the nodes is under another node the node above it gets the amount as well. There are lots of ways of doing the second output but im trying to find one where its fast and doesn't require much I was thinking somewhat recursive... either way whats your opinion on the fastest way to do this one? It may require a list just because its in a hierarchy form.
If you need to traverse all the parent nodes or simply all the nodes in general, a recursive traversal would be the best route for you to take. I am a little unclear exactly what it is that you are trying to do so if you need more help please explain a little more.
Yes I need to transverse all the nodes but then each node of the binary tree had a linked list and I needed to be able to add all the amounts in the linked list to get the total for each node but then each node also counts for all nodes under it.
I actually figured it out - I might post the code in a bit in the other forum a bit later.
This depends on several things, of course. I'd say that a linked list is right out, since it has few suitable properties to work as a symbol table. A binary tree might work, if you already have one and don't have to spend time writing and debugging it. My choice would be a hash table, I think that is more or less the default for this purpose.
-----------------------------------------------
Well a hash table wouldnt work well - all the names are in number format - that is the names of the costcenters and the names of the expenses so id have to mod the number and those are really easily going to overlap.
My binary tree and linked list solution did work - I ended up keeping a few more variables (time vs space) to make it run faster. It keeps an end total of how much the node has "spent" and all the parent nodes also get a copy of the expense.
I posted the source of this up a few days ago if you guys want to see...
View Source Code here - CodeCall
For what you are doing (if I understand it correctly), I would almost made a node class that holds onto the individual values in a list. Then have it hold onto a total sum and have a sum option in it to grab the sum of all the values in that list and update the main sum. This way, you don't end up with a larger runtime than what is required, especially in this tree which will already run in log time for a single lookup.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks