家庭作业帮助,无效参数二叉树

时间:2011-11-14 00:45:34

标签: c++ binary-tree

我的作业有问题。奇怪的是,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 ==========

源代码main:

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); 
}

感谢你。 另一个函数用相似的名字调用它,我错过了它,修复并感谢你的帮助

2 个答案:

答案 0 :(得分:1)

错误消息告诉您正好问题是什么;那些功能需要参数。你是在没有争论的情况下打电话给他们的。

答案 1 :(得分:1)

int binaryTreeType<elemType>::nodeCount(binaryTreeNode<elemType> *p) const

cout << endl << "The node count is: " << treeRoot.nodeCount();

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

从代码中看起来你应该传递根节点。