Adding node values of binary tree

This is a discussion on Adding node values of binary tree within the C Programming forums, part of the General Programming Boards category; Hello to everyone.. I have made a function in order to add the values of the nodes on each path ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    25

    Adding node values of binary tree

    Hello to everyone..

    I have made a function in order to add the values of the nodes on each path of the tree. For example if I enter numbers 7, 8, 9, 4, 1 then for the path (7>4>1) the cost must be 7+4+1=12.
    The maximum cost is wanted.

    I give you the code of the function. I have a problem because despite the fact it adds correctly for the traversal to the left, it doesn't seem to "remember" correctly the maxCost value when changing traversal. In addition, it seems to return in the end the root value as maxCost.

    Could you help me??

    Code:
    int findcosthelp (node n, int cost, int maxCost)
    {
        
        if (n != NULL) {
            cost=cost + n->key;
            printf("%d %d ", cost, maxCost);
            if (cost > maxCost)
            maxCost = cost;
            findcosthelp(n->left, cost, maxCost); 
            findcosthelp(n->right, cost, maxCost);       
       }
       
       return cost;
    }
    
    int findcost (node n, int cost, int maxCost)
    {
        cost=0;
        maxcost=0;
        findcosthelp(n, cost, maxcost);   
       
    }

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    25
    Anyone??

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Well, you code does nothing to get the maxCost out of either function.
    You don't actually need to pass in cost or maxCost either.

    Just perform the recursive calls to get the subtree costs, then return the cost of the current node plus the larger from each subtree. The only parameters the function needs is the node, and you only need one function, not two.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    25
    What do you mean "plus the larger from each subtree"?

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Okay so you know how to call this function right?
    Assuming you eliminate the parameters that should not be there then that looks like this:
    Code:
    int cost;
    cost = findcost(n);
    Well, to find the larger subtree cost, you call findcost once with n->left and then again with n->right (the two subtrees). This will give you two possible values. All you need to do then is return whichever of those numbers is greater.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete Binary Tree Node
    By matrixx333 in forum C Programming
    Replies: 4
    Last Post: 11-29-2009, 11:39 PM
  2. Getting the parent of a node in a binary tree
    By budala in forum C Programming
    Replies: 4
    Last Post: 09-18-2009, 12:36 PM
  3. Delete a node in a binary tree
    By alice in forum C Programming
    Replies: 2
    Last Post: 07-05-2004, 05:01 AM
  4. binary tree node structure
    By Kirsten in forum C Programming
    Replies: 2
    Last Post: 04-26-2002, 08:02 PM
  5. binary tree: adding first node works than crashes
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 10:49 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21