xChaos
xChaos

Reputation: 41

Sum of Each Branch in a Binary Search Tree

My assignment is to find the sum of all nodes on each branch in a binary search tree using recursion, and compare them to a user input value. If the user input value matches a sum of one of the branches, the function should return true.

binary search tree

In other words, the sum of 32+24+21+14=91. The sum of 32+24+28+25=109. The sum of 32+24+28+31=115 etc. I have tried many different methods, but cant seem to figure out how to traverse each branch accurately. So far I have only been able to traverse and find the sum of the left-most branch.

I am using the method of subtracting each node from the user input value. If the value reaches 0 at a Leaf-node, then clearly the user-input matches the node-sum of that branch on the tree.

The particular points of difficulty for me are when the branch diverges, such as at the node [24] and [28]. I clearly am getting something very simple wrong, but I cant figure it out.

Below is the condensed code I've written so far, in the form of two companion methods (also required for the assignment).

public:
bool findBranchSum1(int value) throw (InvalidTreeArgument) {
    if (root == nullptr)
        throw InvalidTreeArgument();
    return(findBranchSum(root, value));
}

private:
bool findBranchSum(NodePtr node, int value) throw (InvalidTreeArgument)
{
    bool result = false;
    if (root == nullptr)
        throw InvalidTreeArgument();

    value -= node->getElement(); //subtract current node from user-input value. 

    cout << "Current Value = " << value << endl; //help track value changes

    if (node->getLeftSide() == nullptr && node->getRightSide() == nullptr)
        {
            if (value == 0)
            {
                result = true;
                return(true);
            }
            else
                return(false);
        }
    else
    {
        if (node->getLeftSide() != nullptr)
        {
            node = node->getLeftSide(); //advance to next Left node
            result = findBranchSum(node, value); //recursive call using new node
        }
        if (node->getRightSide() != nullptr)
        {
            node = node->getRightSide(); //advance to next Right node
            result = findBranchSum(node, value); //recursive call using new node
        }
        return(result);
    }
}

What am I doing wrong, and how can I fix my code to find the sum of each branch on the tree? Thank you in advance. I apologize for any errors in my format, or missing information.

Upvotes: 0

Views: 3112

Answers (3)

nabanita majumder
nabanita majumder

Reputation: 1

In Java, i tried this-

private static void branchSumsUtil(TreeNode root, List<Integer> sumArray, int runningSum) {

    if (root == null){
        return;
    }

    int newRunningSum = runningSum + root.key;

    if (root.left == null && root.right == null){
        sumArray.add(newRunningSum);
    }
    branchSumsUtil(root.left, sumArray, newRunningSum);
    branchSumsUtil(root.right, sumArray, newRunningSum);
}

Upvotes: 0

Arun
Arun

Reputation: 20383

I might try something like the following.

bool IsLeaf(Node const * node) {
  return node && !node->left && !node->right;
}

bool CheckPathSum(Node const * node, int const target, int const sum_so_far) {
  if (!node) return false;
  int const sum = sum_so_far + node->element;
  if IsLeaf(node) && (sum == target)  return true;
  return CheckPathSum(node->left, target, sum) ||
         CheckPathSum(node->right, target, sum);
}

Call as
CheckPathSum(root, target, 0);

Upvotes: 0

Jean-Baptiste Yun&#232;s
Jean-Baptiste Yun&#232;s

Reputation: 36401

This is wrong:

if (node->getLeftSide() != nullptr)
{
  node = node->getLeftSide(); //advance to next Left node
  result = findBranchSum(node, value); //recursive call using new node
}
if (node->getRightSide() != nullptr)
{
  node = node->getRightSide(); //advance to next Right node
  result = findBranchSum(node, value); //recursive call using new node
}

because you move to the left and then to the right branch of the left (node is changed by your assignment), if it exists! Change to:

if (node->getLeftSide() != nullptr)
{
  result = findBranchSum(node->getLeftSide(), value);
}
if (node->getRightSide() != nullptr)
{
  result = findBranchSum(node->getRightSide(), value);
}

Your return value management is also broken, change it to:

if (node->getLeftSide() != nullptr)
{
  result = findBranchSum(node->getLeftSide(), value);
}
if (!result && node->getRightSide() != nullptr) // cut exploration if previous was correct...
{
  result = findBranchSum(node->getRightSide(), value);
}
return result;

if you need to stop at the first correct branch.

Upvotes: 1

Related Questions