diff --git a/09_bst/main.c b/09_bst/main.c index b4d5fbf..0c9c75b 100644 --- a/09_bst/main.c +++ b/09_bst/main.c @@ -18,7 +18,7 @@ int main() printf("\nNames in archive:\n"); printNames(); printf("\n"); - +/* for(int i = 0; i < lenNamesToRemove; i++) { printf("Removing %s ...\n", namesToRemove[i]); @@ -34,7 +34,7 @@ int main() printf("\nNames in archive:\n"); printNames(); - printf("\n"); + printf("\n");*/ return EXIT_SUCCESS; } \ No newline at end of file diff --git a/09_bst/namesarchive.c b/09_bst/namesarchive.c index c7eda6b..b9fb3f3 100644 --- a/09_bst/namesarchive.c +++ b/09_bst/namesarchive.c @@ -3,73 +3,72 @@ #include #include -typedef struct queueElem +typedef struct node { char *name; - struct queueElem *next; -} QueueElem; + struct node *left; + struct node *right; +} Node; -static QueueElem *head = NULL; +static Node *root = NULL; -static QueueElem *createEmptyElem() +static Node *createNameNode(const char *name) { - return calloc(1, sizeof(QueueElem)); -} - -static QueueElem *createNameElem(const char *name) -{ - QueueElem *newElem = NULL; + Node *newNode = NULL; if(name != NULL) { - newElem = createEmptyElem(); + newNode = calloc(1, sizeof(Node)); - if(newElem != NULL) + if(newNode != NULL) { - newElem->name = malloc(sizeof(char) * (strlen(name)+1)); + newNode->name = malloc(sizeof(char) * (strlen(name)+1)); - if(newElem->name != NULL) - strcpy(newElem->name, name); + if(newNode->name != NULL) + strcpy(newNode->name, name); else { - free(newElem); - newElem = NULL; + free(newNode); + newNode = NULL; } } } - return newElem; + return newNode; } -static QueueElem *addNameRec(QueueElem* elem, const char *newName) +static Node *addNameRec(Node* root, const char *newName) { - if(elem == NULL) - elem = createNameElem(newName); + if(root == NULL) + root = createNameNode(newName); + else if(strcmp(newName, root->name) <= 0) + root->left = addNameRec(root->left, newName); else - elem->next = addNameRec(elem->next, newName); + root->right = addNameRec(root->right, newName); - return elem; + return root; } void addName(const char *name) { - head = addNameRec(head, name); + root = addNameRec(root, name); } -static void printNamesRec(QueueElem *elem) +static void printNamesRec(Node *root) { - if(elem != NULL) + if(root != NULL) { - printf("%s\n", elem->name); - printNamesRec(elem->next); + printNamesRec(root->left); + printf("%s\n", root->name); + printNamesRec(root->right); } } void printNames() { - printNamesRec(head); + printNamesRec(root); } - +/* static void disposeElem(QueueElem *elem) { if(elem != NULL) @@ -112,3 +111,4 @@ void clearArchive() clearElemRek(head); head = NULL; } +*/ \ No newline at end of file