Il codice per HTtoBST_exemption è più colplicato di così: tu stai iterando su una lista da cui stai cancellando valori! Di fatto dopo HTdelete non dovresti più usare node.
In BSTtoList hai sbagliato la posizione del return, strano che tu non abbia ricevuto uno warning a riguardo.
BSTremoveLast era proprio implementato sbagliato.
Puoi vedere la mia soluzione qui:
- Codice:
TBST
HTtoBST_exemption(THT *ht, TBST bst)
{
for (int i = 0; i < ht->n_bucket; i++)
{
while (ht->bucket[i] && ht->bucket[i]->info.key % 10 == 0)
{
TNode *temp = ht->bucket[i];
ht->bucket[i] = ht->bucket[i]->link;
if (BSTsearch(bst, temp->info) == NULL)
{
BSTinsert(bst, temp->info);
}
nodeDestroy(temp);
}
if (ht->bucket[i] == NULL)
{
continue;
}
TNode *prev = ht->bucket[i];
TNode *node = prev->link;
while (node != NULL)
{
if (node->info.key % 10 != 0)
{
node = node->link;
prev = prev->link;
continue;
}
TNode *temp = node;
node = node->link;
prev->link = node;
if (BSTsearch(bst, temp->info) == NULL)
{
BSTinsert(bst, temp->info);
}
nodeDestroy(temp);
}
}
return bst;
}
TList
BSTtoList(TBST bst, TList list)
{
// Caso base: l'albero è vuoto, niente da aggiungere
if (bst != NULL)
{
// Divide e combina: aggiungo il sotto-albero sinistro (elementi minori)
list = BSTtoList(bst->left, list);
// Aggiungo l'elemento
list = listInsert(list, bst->info);
// Aggiungo il sotto-albero destro (elementi maggiori)
list = BSTtoList(bst->right, list);
}
return list;
}
void listPrintReverse_exemption(TList list) {
// Caso base
if(list == NULL)
return;
// Divide et Impera
listPrintReverse_exemption(list->link);
printf("%d\n",list->info.key);
}
TBST BSTremoveLast(TBST bst) {
// Caso base
if(bst == NULL)
{
return bst;
}
if( bst->right == NULL )
{
return bst->left;
}
bst->right = BSTremoveLast( bst->right );
return bst;
}
Prova a vedere se capisci il perché l'ho fatto così e le differenze con le tue versioni.