See my reply in Solution 1 for why root level siblings are not getting traversed.
I think a better way to do this, would be to have a member variable called TotalNumberOfChildren.
When you create a branch, TotalNumberOfChildren is set to zero.
Add a member function:
void IncrementChildren(int number_added) {
TotalNumberOfChildren += number_added;
if (this.parent)
parent.IncrementChildren(number_added);
}
In the Add(Branch b) function, call IncrementChildren(b.TotalNumberOfChildren + 1) when you add a child.
(If you implement removeChild() you'll have to implement DecrementChildren() similarly.)
Doing it that way, the TotalNumberOfChildren field will be valid at all times.
Then if you need POP, it's just:
float POP() {
if (this.parent) {
return (this.TotalNumberOfChildren + 1) * 100.0 /
this.parent.TotalNumberOfChildren;
} else {
return 100.0;
}
}
And of course, POT is just:
(this.TotalOfChildren + 1) * 100.0 / root.TotalOfChildren
Depending on how you want to use POT, child nodes can traverse all the parent pointers to get to root in order to report POT, or the caller who wants to know POT can be required to have access to root and do the calculation himself given TotalOfChildren.