[英]Binary Tree Inorder traversal error: no matching function for call
我正在尝试构建二进制树,但我一直收到错误。 当我在main()
调用我的Inorder()
函数时,我得到错误:
错误:没有用于调用'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;
}
Inorder
声明如下:
void Inorder(TreeNode *p)
它需要一个TreeNode *
参数。 也许你打算通过BT.Inorder( BT.root )
?
好吧,你的void Inorder(TreeNode *p)
接受一个参数,而你的函数调用cout << "Inorder: " << BT.Inorder() << endl;
没有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.