大家好,我犯了逻辑错误,但我不认为有错。二叉搜索树(BST)
谢谢:))
我的算法
#include <iostream> //iostream
using namespace std;
struct node{
struct node *left;
struct node *right;
int data;
};
void add(node *p,int sayi){
if(p==NULL){
p=new node();
p->data=sayi;
p->left=NULL;
p->right=NULL;
}
else if(p->data>=sayi){
add(p->left,sayi);
}
else {
add(p->right,sayi);
}
}
void postorder(node *p)
{
if(p!=NULL)
{
if(p->left!=NULL)
postorder(p->left);
if(p->right!=NULL)
postorder(p->right);
cout<< p->data<<endl;
}
else{
cout<<"hata"<<endl;
}
}
void main(){
struct node *k=NULL ;
int sayi=0;
while(sayi!=-1){
cout<<"Bir sayi giriniz...";
cin>>sayi;
add(k,sayi);
}
postorder(k);
system("pause");
}
什么是你用'sayi'在'main'循环做什么?你的输出是什么? – Mene
我想获得用户数量并创建bst。 sayi平均数。 – cengiz
不要忘记'删除'所有这些“新”节点! – Johnsyweb