diff --git a/bintree.c b/bintree.c index 22625e6..ca4ae56 100644 --- a/bintree.c +++ b/bintree.c @@ -1,6 +1,9 @@ #include #include "stack.h" #include "bintree.h" +#include + +StackNode *stackRoot = NULL;//Generates a new, empty stack for next tree data //TODO: binären Suchbaum implementieren /* * `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). 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) { //root = malloc(sizeof(TreeNode)); + printf("NULL detected\n"); TreeNode *newNode = malloc(sizeof(TreeNode)); + printf("Node Allocated\n"); newNode->data = malloc(dataSize); + printf("data allcoated\n"); strcpy(newNode->data, data); + printf("data pasted\n"); if(isDuplicate) *isDuplicate = 0; 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. void *nextTreeData(TreeNode *root) { - StackNode *stackRoot = NULL;//Generates a new, empty stack - 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) + 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; } + // Releases all memory resources (including data copies). void clearTree(TreeNode *root) { diff --git a/highscore.c b/highscore.c index fe8a458..273ebb4 100644 --- a/highscore.c +++ b/highscore.c @@ -66,7 +66,9 @@ void loadHighscores(const char *path) if(name != NULL && scoreStr != NULL) { HighscoreEntry entry = createHighscoreEntry(name, strtol(scoreStr, NULL, 10)); + printf("Pre Tree"); highscoreTree = addToTree(highscoreTree, &entry, sizeof(entry), compareHighscoreEntries, NULL); + printf("Post Tree"); } } diff --git a/main.c b/main.c index 34163d0..30008e7 100644 --- a/main.c +++ b/main.c @@ -77,7 +77,6 @@ int main(int argc, char *argv[]) } else printf("Leider ist %u nicht korrekt. Richtig waere %u gewesen.\n", userInput, duplicate); - loadHighscores(highscorePath); showHighscores(); saveHighscores(highscorePath);