简体   繁体   中英

How do I write this node counter?

Hi I am trying to create a function that counts the number of nodes in the binary tree. I am getting an error that says mismatch of functions. I have gotten other errors and can't seem to get it to work. I know the idea just am having a hard time figuring this one out. Thank You! Edit - My error is mismatch of parameter list.

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

   TreeNode *root;

   void insert(TreeNode *&, TreeNode *&);
   void NodeNumber(TreeNode *&, int&);  //My NodeNumber declaration
 public:
   BinaryTree()
   {
      root = nullptr;
   }

   void insertNode(T);
   int NodeNum();
};

template <class T>
void BinaryTree<T>::insertNode(T item)
{
   TreeNode *newNode = nullptr;

   newNode = new TreeNode;
   newNode->value = item;
   newNode->left = newNode->right = nullptr;

   insert(root, newNode);
}

template <class T>
void BinaryTree<T>::NodeNumber(TreeNode *&root, int&)
{
   if (root = nullptr)
      return;
   else
      root->right;
   root->left;
   count = count + 2;
}

template <class T>
int BinaryTree<T>::NodeNum()
{
   int count = 0;
   NodeNumber(root,count);
   return count;
}

You have numerous mis-designs and errors in this class. I will focus on the outright errors. I don't know which of those mis-designs has been mandated by your professor and which are yours.

BinaryTree<T>::NodeNumber , as it is currently written, will crash every time. To figure out why, think carefully about exactly what this line does:

if (root = nullptr)

How does that line differ from these two?

root = nullptr;
if (root)

Secondly, what do the lines:

root->left;

and:

root->right;

do exactly? Why do you think they do that?

Lastly, when exactly should you be adding to count and why? Where is that true?

You didn't give a name to the 2nd parameter in this function, which I assume should be count .

// original
template <class T>
void BinaryTree<T>::NodeNumber(TreeNode *&root, int&)
{
   if (root = nullptr)
      return;
   else
      root->right;
   root->left;
   count = count + 2;
}

A few comments:

  • If you have a non-null root pointer, you want to visit both left and right child trees. It looks weird that right is in the "else" case, while left is not. I suggest getting rid of the "else" and just return if root is null, and otherwise process both left and right after the if.

  • You are not testing if the root pointer is null; you are setting it to null.

  • There is no reason to pass a reference to the root pointer

  • Your statements like "root->right" do not do anything. You want to recurse down the left child and recurse down the right child, so need to call NodeNumber again and pass your children as the root of these recursive calls, and also pass "count" down too.

  • Why do you increment by 2? Each node should only count as 1. (Its children will account for themselves as you recurse down them, so only add one for the node itself.)

  • I prefer to return the count rather than use an "out" parameter

Therefore, consider something like this:

template <class T>
int BinaryTree<T>::NodeNumber(TreeNode *root)
{
    if (root == nullptr)
        return 0;
    int count = 1;
    count += NodeNumber(root->right);
    count += NodeNumber(root->left);
    return count;
}

And of course, adjust the declaration and calls accordingly.

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