简体   繁体   中英

How do I get the number of leaves on my binary tree code?

I have been getting 0 as the number of leaves on my binary tree. I do not see any problems with my code and would like some feedback. Thank You!:)

MAIN.cpp

int main()
{   
    BinaryTree <int> a;
    a.insertNode(13);
    a.insertNode(28);
    a.insertNode(8);
    a.insertNode(14);
    cout << a.numNodes() << endl;
    cout << a.numLeafNodes() << endl;
    cout << a.height() << endl;
    cout << a.getWidth() << endl;
    system("PAUSE");
}

Binary Tree Class header. This is my class that has the functions.

    template<class T>
class BinaryTree
    {
private:
    struct TreeNode
    {
        T value;
        TreeNode *left;
        TreeNode *right;
    };

    TreeNode *root;

    void insert(TreeNode *&, TreeNode *&);
    int countNodes(TreeNode *&nodePtr);
    void countLeaves(TreeNode* nodePtr);
    int getTreeHeight(TreeNode* nodePtr);
    int width(TreeNode* nodePtr);
public:
    BinaryTree()
    {
        root = nullptr;
    }

    void insertNode(T);
    int numNodes();
    int numLeafNodes();
    int height();
    int getWidth();
};

My Functions for getting the number of leaves. This is Where I am not sure the issue is.

template <class T>
int BinaryTree<T>::numLeafNodes()
{
    int leafCount = 0;
    countLeaves(root);
    return leafCount;
}

template <class T>
void BinaryTree<T>::countLeaves(TreeNode* nodePtr)
{
    if (nodePtr)
    {

        countLeaves(nodePtr->left);
        countLeaves(nodePtr->right);
        if (nodePtr->left == NULL && nodePtr->right == NULL)
        {
            int leafCount = 0;
            leafCount = leafCount + 1;
        }
    }
}
template <class T>
int BinaryTree<T>::numLeafNodes()
{
    int leafCount = 0;

This defines a variable named leafCount .

countLeaves(root);

This doesn't change the leafCount defined above.

return leafCount;

...so this returns leafCount , still holding the value 0 .

template <class T>
void BinaryTree<T>::countLeaves(TreeNode* nodePtr)
{
    if (nodePtr)
    {

        countLeaves(nodePtr->left);
        countLeaves(nodePtr->right);
        if (nodePtr->left == NULL && nodePtr->right == NULL)
        {
            int leafCount = 0;

This defines another, completely separate variable, that also happens to be named leafCount .

            leafCount = leafCount + 1;

This increments the variable defined immediately above.

        }

...and here this leafCount goes out of scope and is destroyed, without affecting anything outside its scope at all (and since there's no observable effect from this leafCount being created, incremented, or destroyed, chances are the the compiler will optimize all that out).

What you'd typically want would be something like returning a value that indicates the number of leaf nodes encountered. This would figure out whether the current node is a leaf node, and if so return 1. Otherwise, it would return the sum of the number of leaf nodes of its left and right sub-trees.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM