Frage

Having a little trouble with counting the size of my Binary Tree, and the number of leaves in my binary tree. Should be a simple problem but I having trouble figuring it out nonetheless.

So I'm getting an error: " error: no matching function for call to âBinaryNode::BinaryNode(BinaryNode*&)â ". This is occuring on lines 218, 219, 230, 232 (of my .cpp file). All with a note from my header file: bet.h:6: note: candidates are: BinaryNode::BinaryNode() BinaryNode::BinaryNode(const BinaryNode&). which is on line 6 of my header file. So here's the code. First the start of my header file (that holds my interfaces):

#include <string>

using namespace std;

struct BinaryNode
{                      //This is line 6
    string element;
    BinaryNode* leftNode;
    BinaryNode* rightNode;
};

class BET
{
public:
    BET();
    BET(const string postfix);
    BET(const BET&);
    ~BET();
    bool buildFromPostfix(const string postfix);
    const BET& operator= (const BET&);
    void printInfixExpression();
    void printPostfixExpression();
    size_t size();
    size_t leaf_nodes();
    bool empty();

private:
    void printInfixExpression(BinaryNode *n);
    void makeEmpty(BinaryNode* &t);
    BinaryNode* clone(BinaryNode* t) const;
    BinaryNode* headNode;
    void printPostfixExpression(BinaryNode *n);
    size_t size(BinaryNode *t);
    size_t leaf_nodes(BinaryNode *t);
};

Next, here's the part of my .cpp file that's giving me errors. (they are the private functions):

/*----Public functions--(calls private functions)----*/

size_t BET::size(){
    size(headNode);
}

size_t BET::leaf_nodes(){
    leaf_nodes(headNode);
}

/*----Private functions--(returns num nodes)----*/

size_t BET::size(BinaryNode *t){
    if(t == NULL)
      return 0;
    else {
      int count = 1;
      count += BinaryNode(t->leftNode);    //line 218
      count += BinaryNode(t->rightNode);   //line 219
      return count;
    }
}

size_t BET::leaf_nodes(BinaryNode *t){
    int count = 0;
    if (t->leftNode == NULL && t->rightNode == NULL)
      count++;
    else {
      if (t->leftNode != NULL)
        count += BinaryNode(t->leftNode);    //line 230
      if (t->rightNode != NULL)
        count += BinaryNode(t->rightNode);   //line 232
    }
return count;
}

note: I know it's not suggested to have the interfaces in a separate file. But this is the way I have to do it.

War es hilfreich?

Lösung

You need a recursive call from within these functions but instead you are attempting to create a node and add the node to an integer. You need this sort of changes:

count += leaf_nodes(t->leftNode); //BinaryNode(t->leftNode);    //line 230

Andere Tipps

You have either to dereference BinaryNode* pointer in your calls to BinaryNode constructor,

e.g.:

BinaryNode(*(t->leftNode));

or to provide BinaryNode constructor that would take BinaryNode* as an argument.

This is not all yet, because, as it was already mentioned, lines like count += BinaryNode(t->leftNode); aren't going to work, because you try to add an object instance to an integer, which doesn't make much sense.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top