二叉树Inorder遍历错误:没有匹配的调用函数

时间:2014-02-21 02:19:21

标签: c++

我正在尝试构建二进制树,但我一直收到错误。当我在Inorder()中调用main()函数时,我收到错误:

  

错误:没有匹配函数来调用'BinaryTree :: Inorder()'。

我希望有人可以帮我解决这个问题。

#include <iostream>
using namespace std;

class BinaryTree{
    private:
        struct TreeNode{
            TreeNode *left;
            TreeNode *right;
            int data;
        };
        TreeNode *root;

    public:
        BinaryTree(){
            root = NULL;
        }

        void Inorder(TreeNode *p){
            if(p != NULL){
                Inorder(p -> left);
                cout<< p -> data;
                Inorder(p -> right);
            }
        }

        void InsertData(int data){
            TreeNode *t = new TreeNode;
            TreeNode *parent;
            t -> data = data;
            t -> left = NULL;
            t -> right = NULL;
            parent = NULL;

            //is this a new tree?
            if (isEmpty())
                root = t;
            else{
               TreeNode *curr;
               curr = root;
               while(curr){
                   parent = curr;
                   if (t -> data > curr -> data)
                        curr = curr -> right;
                   else
                        curr = curr -> left;
               }
               if(t -> data < parent -> data)
                    parent -> left = t;
               else
                    parent -> right =t;
            }
        }

        bool isEmpty(){
            return root == NULL;
        }
};

int main(){
    BinaryTree BT;
    int num;   

    while (cin >> num)
        BT.InsertData(num);    

    cout << "Inorder: " << BT.Inorder() << endl;  
    return 0;
}

2 个答案:

答案 0 :(得分:2)

Inorder声明如下:

        void Inorder(TreeNode *p)

它需要一个TreeNode *参数。也许你打算通过BT.Inorder( BT.root )

答案 1 :(得分:1)

好吧,你的void Inorder(TreeNode *p)接受了争论,而你的函数调用cout << "Inorder: " << BT.Inorder() << endl;却没有。{/ p>