我想在我的C++編譯器上執行二叉搜索樹,但是當我在編譯器上輸入下面的代碼時,我得到錯誤說'[root]'不命名一個類型和19 [錯誤]期望'{'標記之前的非限定id。我嘗試將指針的根改爲整數,但它給出了一個轉換錯誤。二進制搜索樹程序說根不會命名一個類型
#include<iostream>
using namespace std;
struct bstnode{
int data;
bstnode*left;
bstnode*right;
};
bstnode* root;
root = 0;
bstnode*getnewnode(int data)
{
bstnode* newnode=new bstnode();
newnode->data=data;
newnode->left=newnode->right=NULL;
return newnode;
}
void insert(bstnode*root,int data);
{
{
if(root==NULL)
root=getnewnode(data);
}
else if(data<=root->data)
{
root->left=insert(root->left,data);
}
else
{
root->right=insert(root->right,root);
}
return root;
}
bool search(bstnode*root,int data)
{
if(root==NULL) return false;
else if(root->data=data) return true;
else if(data<=root->data) return search(root->left,data);
else return search(root->right,data);
}
int main()
{
bstnode*root=NULL;
root=insert(root,15);
root=insert(root,10);
root=insert(root,12);
int number;
cout<<"enter no";
cin>>no;
}
做的方式已經定義了bstnode爲結構 – Saubhagya
什麼意外的標記? – Saubhagya