Avrei bisogno di aiuto nell'implementazione di un BST, il codice non presenta errori di sintassi ma errori logici in quanto appena cerco di inserire dei valori l'albero risulta sempre vuoto. C'è qualcuno che ha la pazienza di capire dov'è l'errore nella funzione di inserimento? o che abbia una implementazione con la quale possa confrontare? Grazie.
il codice che ho scritto è:
Bst.c
codice:#include <stdlib.h> #include "BST.h" typedef struct STnode *link; #define BST_INSERT_COLLISION 1 struct STnode { char red; Item item; Key key; link l, r; }; struct bst { link head; int count; }; link newNode(Item item, Key key, char red) { link local = malloc(sizeof(*local)); if ((local) == NULL) { return NULL; } local->item = item; local->key = key; local->red = red; local->l = NULL; local->r = NULL; return local; } Bst newBst() { Bst local = malloc(sizeof(*local)); if (local == NULL) { return NULL; } local->count = 0; local->head = NULL; return local; } int bstCount(Bst this) { return this->count; } Item bstSearchR(link this, Key key) { char comparison; if (this == NULL) { return nullItem; } if (!(comparison = keyCompare(this->key, key))) { return this->item; } else if (comparison == 1 && (this->r) != NULL) { return bstSearchR(this->r, key); } else if (comparison == 2 && (this->l) != NULL) { return bstSearchR(this->l, key); } } Item bstSearch(Bst this, Key key) { return bstSearchR(this->head, key); } link rotR(link this) { link x = this->l; this->l = x->r; x->r = this; return x; } link rotL(link this) { link x = this->r; this->r = x->l; x->l = this; return x; } int RBinsert(link *thisP, Item item, Key key, char red) { int comparison; link this = (*thisP); if (this == NULL) { if ((this = newNode(item, key, 'r')) == NULL) { (*thisP) = this; return 0; } else return -1; } //(going down in the recursion)divides the 4-nodes if (this->r != NULL && this->l != NULL) { if (((this->r->red) == 'r') && ((this->l->red) == 'r')) { this->red = 'r'; this->r->red = 'b'; this->l->red = 'b'; } } //goes left if the key to insert is less //and if the insertion is successful //(going back to the root)controls if rotations are needed if ((comparison = keyCompare(key, this->key)) == 1) { if (!RBinsert(this->l, item, key, 'b')) { if (this->red == 'r' && red == 'r') { this = rotR(this); } if (this->red == 'r' && this->l->l->red == 'r') { this = rotR(this); this->red = 'b'; this->red = 'r'; } } } //goes right otherwise & (going back to the root)same controls else if (comparison == 2) { if (!RBinsert(this->r, item, key, 'r')) { if (this->r != NULL) { if (this->red == 'r' && this->r->red == 'r' && red == 'b') { this = rotL(this); } } if ((this->r) != NULL){ if (this->r->red == 'r' && this->r->r->red == 'r') { this = rotL(this); this->red = 'b'; this->l->red = 'r'; } } } } else if (comparison == 0) { return BST_INSERT_COLLISION; } return 0; } int bstInsert(Bst this, Item item, Key key) { if (!(RBinsert(&(this->head), item, key, 0))) { this->head->red = '0'; (this->count)++; return 0; } else { return 1; } } int STinsertR(link *this, Item item, Key key) { int comparison; link local = (*this); if (local == NULL) { local = newNode (item, key, 'r'); (*this) = local; return 0; } else if ((comparison = keyCompare(local->key, key) == 1)) { STinsertR(&(local->r), item, key); } else if (comparison == 2) { STinsertR(&(local->l), item, key); } return BST_INSERT_COLLISION; } void bstVisitR(link this) { if (this == NULL) { return; } bstVisitR(this->l); itemVisit(this->item); bstVisitR(this->r); } void bstVisit(Bst this) { bstVisitR(this->head); return; } Bst.hItem.hcodice:#ifndef BST_H #define BST_H #include "Item.h" typedef struct bst *Bst; Bst newBst(); int bstInsert(Bst, Item, Key); int bstCount(Bst this); Item bstSearch(Bst this, Key key); void bstVisit(Bst this); #endif
Item.ccodice:#ifndef ITEM_H #define ITEM_H #define nullItem NULL #define nullKey NULL typedef char* Item; typedef char* Key; int keyCompare(Key, Key); void itemVisit(Item); #endif
Test.ccodice:#include <string.h> #include "Item.h" int keyCompare(char *str1, char *str2) { int tmp = strcmp(str1, str2); switch (tmp) { case 1: return 2; case -1: return 1; case 0: return 0; } } void itemVisit(char *str) { printf("%s", str); return; }
codice:#include <stdio.h> #include <stdlib.h> #include "BST.h" #define N 30 int main(void) { char uno[4] = "blab"; char due[7] = "blabla"; char tre[N] = "askis"; char quattro[N] = "disas"; char cinque[N] = "guaus"; char sei[N] = "grssac"; Bst bst = newBst(); bstInsert(bst, uno, uno); bstInsert(bst, due, due); bstInsert(bst, tre, tre); bstInsert(bst, quattro, quattro); bstInsert(bst, cinque, cinque); bstInsert(bst, sei, sei); printf ("%d\n", bstCount(bst)); bstVisit(bst); system("PAUSE"); return 0; }