// BST.cpp : Defines the entry point for the console application.
//
#include <iostream>
using namespace std;
struct lista
{
lista(int V)
{
value=V;
L=R=0;
}
int value;
lista* L;
lista* R;
};
void add(lista* start, int val)
{
if(start==NULL)
{
start = new lista(val);
return;
}
lista* work = start;
while(true)
{
if(val > work->value)
{
if(work->R == NULL)
{
work->R = new lista(val);
return;
}
else
work = work->R;
}
else
{
if(work->L == NULL)
{
work->L = new lista(val);
return;
}
else
work = work->L;
}
}
}
void print(lista* start)
{
if(start == NULL)
return;
print(start->L);
cout<<start->value<<endl;
print(start->R);
}
int main()
{
lista* st = NULL;
add(st,10);
add(st,5);
add(st,8);
add(st,15);
add(st,11);
add(st,3);
print(st);
system("pause");
return 0;
}
Jest to drzewo sortowania binarnego. Dlaczego nie działa dodawanie elementów, jeżeli wskaźnik na początku jest NULL, a poźniej jest on zastąpiony przez new lista(val)?
Pozdrawiam:)