Explore BrainMass

Explore BrainMass

    Binary Trees and Tree-Traversal

    Not what you're looking for? Search our solutions OR ask your own Custom question.

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    Trace the following tree-traversal function, f(), and describe its action:

    template <typename T>
    int f(tnode<T> *t)
    { int n = 0, leftValue, rightValue;

    if (t != NULL)
    {
    if (t ->left != NULL || t->right != NULL)
    n++;
    leftValue = f(t->left);
    rightValue = f(t->right);
    return n + leftValue + rightValue;
    }
    else
    return 0;
    }

    Please provide a complete program if it makes sense.

    © BrainMass Inc. brainmass.com March 4, 2021, 7:58 pm ad1c9bdddf
    https://brainmass.com/computer-science/cpp/binary-trees-tree-traversal-134814

    Attachments

    Solution Preview

    The function gets the total number of nodes with child or children in the tree, i.e., the number of non-leaf nodes.

    Please see the attached files for the detail.

    Trace the following tree-traversal function, f(), and describe its ...

    Solution Summary

    This solution shows how to get a total nodes in a tree using tree-traversal algorithm.

    $2.49

    ADVERTISEMENT