大家好,我犯了邏輯錯誤,但我不認爲有錯。二叉搜索樹(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