fixed some bugs, though bintree.c doesn't work properly yet

This commit is contained in:
Tobias Grampp 2025-12-04 15:31:53 +01:00
parent c7ff696b67
commit cbafb5f0f2
4 changed files with 24 additions and 25 deletions

View File

@ -14,28 +14,26 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
{ {
if(!root) if(!root)
{ {
root = malloc(sizeof(node)); //root = malloc(sizeof(TreeNode));
*node newNode; TreeNode *newNode = malloc(sizeof(TreeNode));
*newNode.data = malloc(dataSize); newNode->data = malloc(dataSize);
strcpy(*newNode.data, data); strcpy(newNode->data, data);
*newNode.left = NULL;
*newNode.right = NULL;
if(isDuplicate) if(isDuplicate)
*isDuplicate = 0; *isDuplicate = 0;
return newNode; return newNode;
} }
if(compareFct(*data, *root->data) > 0) if(compareFct(data, root->data) > 0)
{ {
*right = addToTree(right, data, dataSize, compareFct, isDuplicate); root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
} }
else if(compareFct(*data, *root->data) < 0) else if(compareFct(data, root->data) < 0)
{ {
*left = addToTree(right, data, dataSize, compareFct, isDuplicate); root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
} }
else else
{ {
if(!isDuplicate) if(!isDuplicate)
*left = addToTree(right, data, dataSize, compareFct, isDuplicate); root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
else else
{ {
*isDuplicate = 1; *isDuplicate = 1;
@ -51,7 +49,7 @@ void *nextTreeData(TreeNode *root)
{ {
StackNode *stackRoot = NULL;//Generates a new, empty stack StackNode *stackRoot = NULL;//Generates a new, empty stack
stackRoot = iterateThroughTree(root, stackRoot); //Fills the stack via the helping function iterateThroughTree stackRoot = iterateThroughTree(root, stackRoot); //Fills the stack via the helping function iterateThroughTree
stackNode * tempBuffer = pop(stackRoot); StackNode * tempBuffer = pop(stackRoot);
clearStack(stackRoot); clearStack(stackRoot);
return tempBuffer; return tempBuffer;
} }
@ -64,7 +62,7 @@ StackNode *iterateThroughTree(TreeNode *root, StackNode *stackRoot)
return stackRoot; return stackRoot;
} }
stackRoot = iterateThroughTree(root->left, stackRoot); stackRoot = iterateThroughTree(root->left, stackRoot);
stackNode = push(stackNode, root->data); stackRoot = push(stackRoot, root->data);
stackRoot = iterateThroughTree(root->right, stackRoot); stackRoot = iterateThroughTree(root->right, stackRoot);
return stackRoot; return stackRoot;
} }
@ -72,12 +70,12 @@ StackNode *iterateThroughTree(TreeNode *root, StackNode *stackRoot)
// Releases all memory resources (including data copies). // Releases all memory resources (including data copies).
void clearTree(TreeNode *root) void clearTree(TreeNode *root)
{ {
if(left) if(root->left)
clearTree(left); clearTree(root->left);
if(right) if(root->right)
clearTree(right); clearTree(root->right);
free(data); free(root->data);
data = NULL; root->data = NULL;
free(root); free(root);
root = NULL; root = NULL;
} }
@ -88,10 +86,10 @@ unsigned int treeSize(const TreeNode *root)
if(root) if(root)
{ {
unsigned int tempSize = 0; unsigned int tempSize = 0;
if(left) if(root->left)
tempSize += treeSize(left); tempSize += treeSize(root->left);
if(right) if(root->right)
tempSize += treeSize(right); tempSize += treeSize(root->right);
return tempSize + 1; return tempSize + 1;
} }
return 0; return 0;

View File

@ -2,6 +2,7 @@
#define BINTREE_H #define BINTREE_H
#include <stdlib.h> #include <stdlib.h>
#include "stack.h"
typedef int (*CompareFctType)(const void *arg1, const void *arg2); typedef int (*CompareFctType)(const void *arg1, const void *arg2);
@ -24,5 +25,5 @@ void clearTree(TreeNode *root);
// Returns the number of entries in the tree given by root. // Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode *root); unsigned int treeSize(const TreeNode *root);
//Function to aid nextTreeData //Function to aid nextTreeData
StackNode iterateThroughTree(TreeNode *root, StackNode stackRoot); StackNode *iterateThroughTree(TreeNode *root, StackNode *stackRoot);
#endif #endif

View File

@ -11,7 +11,7 @@
* Duplizieren eines zufälligen Eintrags im Array. * Duplizieren eines zufälligen Eintrags im Array.
* in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */ * in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries. //1 Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while // Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
// creating random numbers. // creating random numbers.
unsigned int *createNumbers(unsigned int len){ unsigned int *createNumbers(unsigned int len){

Binary file not shown.