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)
{
root = malloc(sizeof(node));
*node newNode;
*newNode.data = malloc(dataSize);
strcpy(*newNode.data, data);
*newNode.left = NULL;
*newNode.right = NULL;
//root = malloc(sizeof(TreeNode));
TreeNode *newNode = malloc(sizeof(TreeNode));
newNode->data = malloc(dataSize);
strcpy(newNode->data, data);
if(isDuplicate)
*isDuplicate = 0;
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
{
if(!isDuplicate)
*left = addToTree(right, data, dataSize, compareFct, isDuplicate);
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
else
{
*isDuplicate = 1;
@ -51,7 +49,7 @@ 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);
StackNode * tempBuffer = pop(stackRoot);
clearStack(stackRoot);
return tempBuffer;
}
@ -64,7 +62,7 @@ StackNode *iterateThroughTree(TreeNode *root, StackNode *stackRoot)
return stackRoot;
}
stackRoot = iterateThroughTree(root->left, stackRoot);
stackNode = push(stackNode, root->data);
stackRoot = push(stackRoot, root->data);
stackRoot = iterateThroughTree(root->right, stackRoot);
return stackRoot;
}
@ -72,12 +70,12 @@ StackNode *iterateThroughTree(TreeNode *root, StackNode *stackRoot)
// Releases all memory resources (including data copies).
void clearTree(TreeNode *root)
{
if(left)
clearTree(left);
if(right)
clearTree(right);
free(data);
data = NULL;
if(root->left)
clearTree(root->left);
if(root->right)
clearTree(root->right);
free(root->data);
root->data = NULL;
free(root);
root = NULL;
}
@ -88,10 +86,10 @@ unsigned int treeSize(const TreeNode *root)
if(root)
{
unsigned int tempSize = 0;
if(left)
tempSize += treeSize(left);
if(right)
tempSize += treeSize(right);
if(root->left)
tempSize += treeSize(root->left);
if(root->right)
tempSize += treeSize(root->right);
return tempSize + 1;
}
return 0;

View File

@ -2,6 +2,7 @@
#define BINTREE_H
#include <stdlib.h>
#include "stack.h"
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.
unsigned int treeSize(const TreeNode *root);
//Function to aid nextTreeData
StackNode iterateThroughTree(TreeNode *root, StackNode stackRoot);
StackNode *iterateThroughTree(TreeNode *root, StackNode *stackRoot);
#endif

View File

@ -11,7 +11,7 @@
* Duplizieren eines zufälligen Eintrags im Array.
* 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
// creating random numbers.
unsigned int *createNumbers(unsigned int len){

Binary file not shown.