at a state of not working

This commit is contained in:
Tobias Grampp 2025-12-04 16:18:24 +01:00
parent cbafb5f0f2
commit b7249a9c1c
3 changed files with 15 additions and 16 deletions

View File

@ -1,6 +1,9 @@
#include <string.h> #include <string.h>
#include "stack.h" #include "stack.h"
#include "bintree.h" #include "bintree.h"
#include <stdio.h>
StackNode *stackRoot = NULL;//Generates a new, empty stack for next tree data
//TODO: binären Suchbaum implementieren //TODO: binären Suchbaum implementieren
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv), /* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv),
@ -12,12 +15,17 @@
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added). // if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate) TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
{ {
printf("Sind in add to tree. Root ist: %d\n", root);
if(!root) if(!root)
{ {
//root = malloc(sizeof(TreeNode)); //root = malloc(sizeof(TreeNode));
printf("NULL detected\n");
TreeNode *newNode = malloc(sizeof(TreeNode)); TreeNode *newNode = malloc(sizeof(TreeNode));
printf("Node Allocated\n");
newNode->data = malloc(dataSize); newNode->data = malloc(dataSize);
printf("data allcoated\n");
strcpy(newNode->data, data); strcpy(newNode->data, data);
printf("data pasted\n");
if(isDuplicate) if(isDuplicate)
*isDuplicate = 0; *isDuplicate = 0;
return newNode; return newNode;
@ -47,26 +55,16 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
// push the top node and push all its left nodes. // push the top node and push all its left nodes.
void *nextTreeData(TreeNode *root) void *nextTreeData(TreeNode *root)
{ {
StackNode *stackRoot = NULL;//Generates a new, empty stack if(root)
stackRoot = iterateThroughTree(root, stackRoot); //Fills the stack via the helping function iterateThroughTree
StackNode * tempBuffer = pop(stackRoot);
clearStack(stackRoot);
return tempBuffer;
}
//Function to aid nextTreeData
StackNode *iterateThroughTree(TreeNode *root, StackNode *stackRoot)
{
if(!root)
{ {
return stackRoot; stackRoot = push(root->data, stackRoot);
nextTreeData(root->left);
nextTreeData(root->right);
} }
stackRoot = iterateThroughTree(root->left, stackRoot);
stackRoot = push(stackRoot, root->data);
stackRoot = iterateThroughTree(root->right, stackRoot);
return stackRoot; return stackRoot;
} }
// Releases all memory resources (including data copies). // Releases all memory resources (including data copies).
void clearTree(TreeNode *root) void clearTree(TreeNode *root)
{ {

View File

@ -66,7 +66,9 @@ void loadHighscores(const char *path)
if(name != NULL && scoreStr != NULL) if(name != NULL && scoreStr != NULL)
{ {
HighscoreEntry entry = createHighscoreEntry(name, strtol(scoreStr, NULL, 10)); HighscoreEntry entry = createHighscoreEntry(name, strtol(scoreStr, NULL, 10));
printf("Pre Tree");
highscoreTree = addToTree(highscoreTree, &entry, sizeof(entry), compareHighscoreEntries, NULL); highscoreTree = addToTree(highscoreTree, &entry, sizeof(entry), compareHighscoreEntries, NULL);
printf("Post Tree");
} }
} }

1
main.c
View File

@ -77,7 +77,6 @@ int main(int argc, char *argv[])
} }
else else
printf("Leider ist %u nicht korrekt. Richtig waere %u gewesen.\n", userInput, duplicate); printf("Leider ist %u nicht korrekt. Richtig waere %u gewesen.\n", userInput, duplicate);
loadHighscores(highscorePath); loadHighscores(highscorePath);
showHighscores(); showHighscores();
saveHighscores(highscorePath); saveHighscores(highscorePath);