我想构建一个二叉树,但我不断收到错误。当我把我的Inorder()
功能main()
我得到的错误:Binary Tree Inorder遍历错误:没有匹配的函数调用
error: no matching function for call to '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;
}
有趣的是,看看如何复制完全相同的代码:http://cplusplus.happycodings.com/Algorithms/code4.html ORIGINAL,http://www.cplusplus.com/forum/general/1551 2008年4月,http://www.cplusplus.com/forum/general/11408 2009年5月,http://www.cplusplus.com/forum/general/21855/ 2010年4月,https://www.facebook.com/AllComputerLanguage/帖子/ 101463913342279 2012年9月,http://stackoverflow.com/questions/13484943/print-a-binary-tree-in-a-pretty-way Nov 2012,http://stackoverflow.com/questions/20057774/bst- code-is-not-working-large-number 2013年11月,现在我们在这里:) – AndyG
@AndyG哇,令人印象深刻。 Facebook谱系。我想你可以说它是“病毒式的”......这可能是一个可行的病毒载体! – Potatoswatter