作业帮助,无效的参数二进制树

Homework help, invalid parameters binary trees

本文关键字:参数 二进制 无效 帮助 作业      更新时间:2023-10-16

我的家庭作业有问题。奇怪的是,treeHeight的功能起作用了。它是由这本书提供的。我为这个作业做了nodeCount和leavesCount,当作者做的那个没有时,我得到了这些构建错误

构建错误:

1>  testProgBinarySearchTree.cpp
1>testProgBinarySearchTree.cpp(49): error C2660: 'binaryTreeType<elemType>::nodeCount'     : function does not take 0 arguments
1>          with
1>          [
1>              elemType=int
1>          ]
1>testProgBinarySearchTree.cpp(50): error C2660: 'binaryTreeType<elemType>::leavesCount' : function does not take 0 arguments
1>          with
1>          [
1>              elemType=int
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

源代码主要:

cout << endl<<"Line 24: Tree Height: "
     << treeRoot.treeHeight() << endl;              //Line 24
cout << endl << "The node count is: " << treeRoot.nodeCount();
cout << endl << "The leaf count is: " << treeRoot.leavesCount();    

类别:

int height(binaryTreeNode<elemType> *p) const;
  //Function to return the height of the binary tree
  //to which p points. 
int max(int x, int y) const;
  //Returns the larger of x and y.
int nodeCount(binaryTreeNode<elemType> *p) const;
  //Function to return the number of nodes in the binary 
  //tree to which p points 
int leavesCount(binaryTreeNode<elemType> *p) const;
  //Function to return the number of leaves in the binary 
  //tree to which p points 
template <class elemType>
int binaryTreeType<elemType>::height(binaryTreeNode<elemType> *p) const
{
    if (p == NULL)
        return 0;
    else
        return 1 + max(height(p->llink), height(p->rlink));
}
template <class elemType>
int binaryTreeType<elemType>::max(int x, int y) const
{
    if (x >= y)
        return x;
    else
        return y;
}
template <class elemType>
int binaryTreeType<elemType>::nodeCount(binaryTreeNode<elemType> *p) const
{
    if (p == null)
    return 0;
else
    return nodeCount(p->llink) + nodeCount(p->rlink) + 1;
}
template <class elemType>
int binaryTreeType<elemType>::leavesCount(binaryTreeNode<elemType> *p) const
{
    if (p == null)
    return 0;
else
    if ( leavesCount(p->llink) == 0 && leavesCount(p->rlink) == 0)
        return 1;
    else 
        return leavesCount(p->llink) + leavesCount (p->rlink); 
}

想清楚了,谢谢。另一个函数用类似的名称调用它,我错过了它,修复了它,并感谢

的帮助

错误消息告诉您精确地问题是什么;这些函数需要参数。你在没有争论的情况下给他们打电话。

int binaryTreeType<elemType>::nodeCount(binaryTreeNode<elemType> *p) const
cout << endl << "The node count is: " << treeRoot.nodeCount();

您的错误表明nodeCount不接受零个参数。从您的函数签名,您需要传入一个binaryTreeNode<elemType> *p,而您调用它的方式是不传入任何内容

从代码中看,您应该将根节点传入。