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");
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;
}

View File

@ -3,73 +3,72 @@
#include <stdio.h>
#include <string.h>
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;
}
*/