Używam metody rekurencyjnej, aby utworzyć binarne drzewo poszukiwań. Moim celem jest, aby znaleźć najniższe element w drzewie. Poniżej jest mój kod.
Wprowadzenie
node insert(node root, int value)
{
if ( root == NULL )
{
return ((newNode(value)));
}
if ( root->info == value )
{
std::cout<<Duplicate entry found!<<std::endl;
return root;
}
else if ( root->info > value )
{
root->lChild = insert(root->lChild,value);
}
else if ( root->info < value )
{
root->rChild = insert(root->rChild,value);
}
else
std::cout<<Some error has occurred.Time to debug!<<std::endl;
return root;
}
Funkcja MINVALUE
int minValue(node curPtr)
{
node temp = curPtr;
while ( curPtr )
{
temp = curPtr->lChild;
}
return (temp->info);
}
Powodem, dla którego (IMO) moja MINVALUE () wkracza w nieskończonej pętli jest spowodowane curPtr nie zawsze jest NULL. Jak mogę to zrobić po NULL Mam wstawione dane za pomocą funkcji insert ().
EDIT: Okazało się, że bug..so głupie. Dzięki Raymond
Poniżej jest edytowany MINVALUE ()
int minValue(node curPtr)
{
node temp = curPtr;
while ( temp->lChild )
{
temp = temp->lChild;
}
return (temp->info);
}
Dzięki Kelly.













