views:

181

answers:

2

Hi All,

I'm going to come right out and say that I am not the worlds greatest Mathematician :D So this problem may well be simple to most of you. Unfortunately it's confusing me and have had several stabs at a workable solutions.

As with any tree, one can have many branches, many branches can have more branches and so forth until they end with a leaf node. I've got information on each leaf that indicates its value.

What I require is a clear explination on how to tackle the problem of summarising each leaf node value as a total for it's branch (parent) and doing the same for the rest but not forgetting that if a branch is shared by other branches that it is the summary of each lower level branch and leaf that is directly related to itself.

To better explain:

Root
|----Branch
|         |-Leaf 10
|----Branch
|         |----Branch
|         |-Leaf 20 |-Leaf 30
|----Branch         |-Leaf 40
|         |----Branch
|                   |----Branch
|                             |----Leaf 50
|-Leaf 60

The goal:

Root 210
|----Branch 10
|         |-Leaf 10
|----Branch 90
|         |----Branch 70
|         |-Leaf 20 |-Leaf 30
|----Branch 50      |-Leaf 40
|         |----Branch 50
|                   |----Branch 50
|                             |----Leaf 50
|-Leaf 60

I am able to identify the lowest level members (leaf nodes), the root node and the branches themselves. I don't have identification on whether or not the branch has other branches linked to itself lower down or directly linked to a leaf node. The relationship is very much bottom upwards to root. IE: The branch has no reference to who it's children are, but the children know who the parent is.

Please if something is unclear ask and I'll try and explain the problem better.

Any help would be appreciated.

A: 

You want to determine the sum of all the nodes in the tree?

Tree walking lends itself to an elegant recursive solution:

public int SumTree (TreeNode n) {
    if(n.isLeafNode) return n.value;
    return SumTree(n.left) + SumTree(n.right);
}

Assuming a binary tree.

Anon.
It is not a binary tree, but, if you can briefly explain the premise behind it, I could perhaps flesh out my own treeObject to cater for this type of logic. From the code you've given I'd need to know what n.left and n.right represent in terms of the structure.That definitely looks like a very simple solution provided I can understand how to impliment it correctly.
Microdot
.left and .right represent the children of that particular branch. If it's not a binary tree, you can instead iterate over all of its children and call SumTree on each of them.
Anon.
At the risk of sound totally incompetant, would you be able to provide a small example based on the above?
Microdot
+1  A: 

OK, lefts give this a stab.

I would go about it like this with some pseudo code

foreach leaf in knownLeafs
    parent = leaf.parent //get the leaf parent
    parent.total = parent.total + leaf.value //add leaf value to parent total
    while parent.parent != null //loop until no more parents, this would be the root
    {
     current = parent
     parent = parent.parent //move up the structure
     parent.total = parent.total + current.total
    }
next leaf

you would need to create a function that will, given a node, return the parent node

node GetParentNodeFrom(node)

the new pseudo code would look something like this

foreach leaf in knownLeafs
parent = GetParentNodeFrom(leaf) //get the leaf parent

parent.total = parent.total + leaf.value //add leaf value to parent total
while GetParentNodeFrom(parent) != null //loop until no more parents, this would be the root
{
    current = parent
    parent = GetParentNodeFrom(current) //move up the structure
    parent.total = parent.total + current.total
}
next leaf

Sorry, my mistake, you should only move the leaf value up, not the totals too. See new leafValue used.

foreach leaf in knownLeafs
parent = GetParentNodeFrom(leaf) //get the leaf parent
leafValue = leaf.value
parent.total = parent.total + leafValue //add leaf value to parent total
while GetParentNodeFrom(parent) != null //loop until no more parents, this would be the root
{
    current = parent
    parent = GetParentNodeFrom(current) //move up the structure
    parent.total = parent.total + leafValue
}
next leaf
astander
Thanks Astander! I'm going to look into your idea too. Seems like i'm not the only one struggling on this sort of problem.
Microdot
The while loop seems a bit strange, what are you enumerating on?I understand what you're getting at though, traversing the parent of the parent of the parent of the parent. I don't know the depth and not sure how to impliment the equiv. "while parent.parent != null //loop until no more parents, this would be the root"
Microdot
Just seen your update, that makes more sense. I'll report back.
Microdot
Okay, I've managed to impliment this in C#, the idea makes sense. I'm now tracking the logic on the parent enumeration, it is duplicating - almost compounding values every iteration.Remember each parent could have a direct link to a leaf node or another branch (downwards), if we traverse upwards we could be combining our total with various streams.
Microdot
:-) I completely missed that!, parent.total = parent.total + current.total would be a compound ever iteration. parent.total = parent.total + leafValue is correct and takes away sibling and branch compounding. I'd also like to thank you for the quick responses and help on this problem. In my 8/9 years development career it is the first time I've faced an implimentation so strange (where parent has no re-collection of leaf nodes) and a reverse traversal, leaf node up is the only other possible way to calculate without making large changes to existing code. You get +1 from me! Thank you.
Microdot
As a matter of fact, i had to sit very still to get this "picture" in my head. I have never had to do the reverse either, but you saying that you knew all the leafs gave that away X-)
astander