Add bst (add, print, removal missing).

This commit is contained in:
paulusja 2025-12-04 13:46:24 +01:00
parent 0e461863b6
commit 93aaf59201
2 changed files with 33 additions and 33 deletions

View File

@ -18,7 +18,7 @@ int main()
printf("\nNames in archive:\n"); printf("\nNames in archive:\n");
printNames(); printNames();
printf("\n"); printf("\n");
/*
for(int i = 0; i < lenNamesToRemove; i++) for(int i = 0; i < lenNamesToRemove; i++)
{ {
printf("Removing %s ...\n", namesToRemove[i]); printf("Removing %s ...\n", namesToRemove[i]);
@ -34,7 +34,7 @@ int main()
printf("\nNames in archive:\n"); printf("\nNames in archive:\n");
printNames(); printNames();
printf("\n"); printf("\n");*/
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -3,73 +3,72 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
typedef struct queueElem typedef struct node
{ {
char *name; char *name;
struct queueElem *next; struct node *left;
} QueueElem; 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)); Node *newNode = NULL;
}
static QueueElem *createNameElem(const char *name)
{
QueueElem *newElem = NULL;
if(name != 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) if(newNode->name != NULL)
strcpy(newElem->name, name); strcpy(newNode->name, name);
else else
{ {
free(newElem); free(newNode);
newElem = NULL; 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) if(root == NULL)
elem = createNameElem(newName); root = createNameNode(newName);
else if(strcmp(newName, root->name) <= 0)
root->left = addNameRec(root->left, newName);
else else
elem->next = addNameRec(elem->next, newName); root->right = addNameRec(root->right, newName);
return elem; return root;
} }
void addName(const char *name) 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(root->left);
printNamesRec(elem->next); printf("%s\n", root->name);
printNamesRec(root->right);
} }
} }
void printNames() void printNames()
{ {
printNamesRec(head); printNamesRec(root);
} }
/*
static void disposeElem(QueueElem *elem) static void disposeElem(QueueElem *elem)
{ {
if(elem != NULL) if(elem != NULL)
@ -112,3 +111,4 @@ void clearArchive()
clearElemRek(head); clearElemRek(head);
head = NULL; head = NULL;
} }
*/