所以我正在做一個簡單的二叉樹來存儲整數,但我不斷收到一個「表達式必須有指針類類型」錯誤插入函數,這可能只是我沒有注意到的東西很明顯,但我相對較新的C++,所以任何幫助,將不勝感激。由於C++二叉樹指針問題
struct node
{
//Data stored in this node of the tree
int data;
//The left branch of the tree
node *left;
//The right branch of the tree
node *right;
};
using namespace std;
//Inserts a value into the tree
void insert(node **tree, int value)
{
if (*tree == nullptr)
{
//Create new node
*tree = new node;
//Set new value
(*tree)->data = value;
//Set branches to nullptr
(*tree)->left = nullptr;
(*tree)->right = nullptr;
}
else
{
if (value < (*tree)->data)
insert(*tree->left, value);//ERROR HERE
if (value >(*tree)->data)
insert(*tree->right, value);//ERROR HERE
}
}
//Deletes the tree
void delete_tree(node *tree)
{
if (tree != NULL)
{
delete_tree(tree->left);
delete_tree(tree->right);
delete tree;
}
}
//Prints the tree in order
void inorder(node *tree)
{
if (tree != nullptr)
{
inorder(tree->left);
cout << tree->data << " ";
inorder(tree->right);
}
}
int main(int argc, char **argv)
{
while (true){
//if(userinputflag=0)
//else node *tree = input number
node *tree = nullptr;
while (true)
{
int num;
cout << "Enter number (-1 to exit): ";
cin >> num;
if (num == -1)
break;
insert(&tree, num);
}
inorder(tree);
cout << endl;
}
}
您是否嘗試過'插入(&((* tree) - > left),value);'already? – 2015-04-03 19:01:20
您應該在這裏發佈之前刪除不相關的代碼*。 – 2015-04-03 19:01:29