bintree fertig

This commit is contained in:
Walter Schenk 2025-12-14 20:52:14 +01:00
parent 0c020ba402
commit 1a9eb873dd

111
bintree.c
View File

@ -2,55 +2,65 @@
#include "stack.h" #include "stack.h"
#include "bintree.h" #include "bintree.h"
//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),
* `clearTree`: gibt den gesamten Baum frei (rekursiv), * `clearTree`: gibt den gesamten Baum frei (rekursiv),
* `treeSize`: zählt die Knoten im Baum (rekursiv), * `treeSize`: zählt die Knoten im Baum (rekursiv),
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */ * `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
static TreeNode *createNode(const void *data, size_t dataSize)
{
static TreeNode* createNode(const void* data, size_t dataSize) { TreeNode *newNode = calloc(1, sizeof(TreeNode));
TreeNode* newNode = calloc(1, sizeof(TreeNode)); if (!newNode)
if(!newNode) return NULL; return NULL;
newNode->data = malloc(dataSize); newNode->data = malloc(dataSize);
if(!newNode->data) { if (!newNode->data)
{
free(newNode); free(newNode);
return 0; return 0;
} }
newNode->data = data; newNode->data = data;
return newNode; return newNode;
} }
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates // Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
// 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).
//1. Fall: isDuplicate = NULL -> accepts // 1. Fall: isDuplicate = NULL -> accepts
//2. Fall: other -> ignores duplicates and // 2. Fall: other -> ignores duplicates and
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)
{ {
if(isDuplicate != NULL) { if (isDuplicate != NULL)
{
*isDuplicate = 0; *isDuplicate = 0;
} }
if(!root) { //wurzerl == 0; if (!root)
TreeNode* newNode = createNode(data, dataSize); { // wurzerl == 0;
TreeNode *newNode = createNode(data, dataSize);
return newNode; return newNode;
} }
//wenn duplicate NUll -> doppelt hinzufügen // wenn duplicate NUll -> doppelt hinzufügen
// //
if(compareFct(data, root->data) < 0){ if (compareFct(data, root->data) < 0)
{
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate); root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
} else if (compareFct(data, root->data) > 0) { }
else if (compareFct(data, root->data) > 0)
{
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate); root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
} else if (compareFct(data, root->data) == 0) { }
if(isDuplicate != NULL) { else if (compareFct(data, root->data) == 0)
{
if (isDuplicate != NULL)
{
*isDuplicate = 1; *isDuplicate = 1;
} else { }
else
{
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate); root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
} }
} }
} }
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, // Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL,
@ -61,49 +71,64 @@ TreeNode *addToTree(TreeNode* root, const void* data, size_t dataSize, CompareFc
void *nextTreeData(TreeNode *root) void *nextTreeData(TreeNode *root)
{ {
static StackNode *stack = NULL; static StackNode *stack = NULL;
if (root != NULL) { if (root != NULL)
{
clearStack(stack); clearStack(stack);
stack = push(NULL, root); stack = push(NULL, root);
while(root->left != NULL) { while (root->left != NULL)
{
root = root->left; root = root->left;
stack = push(stack,root); stack = push(stack, root);
} }
} }
if (stack == NULL) { if (stack == NULL)
{
return NULL; return NULL;
} }
TreeNode* result = (TreeNode*)top(stack); TreeNode *result = (TreeNode *)top(stack);
stack = pop(stack); stack = pop(stack);
root = result; root = result;
if (root->right != NULL) { if (root->right != NULL)
{
root = root->right; root = root->right;
stack = push(stack,root); stack = push(stack, root);
while(root->left != NULL) { while (root->left != NULL)
{
root = root->left; root = root->left;
stack = push(stack,root); stack = push(stack, root);
} }
} }
return result; return result;
} }
// Releases all memory resources (including data copies). // Releases all memory resources (including data copies).
void clearTree(TreeNode *root) void clearTree(TreeNode *root)
{ {
TreeNode* clear = nextTreeData(root); if (root == NULL)
while(clear != NULL){ {
free(clear->data); return;
free(clear);
clear = nextTreeData(NULL);
} }
clearTree(root->left);
clearTree(root->right);
free(root->data);
free(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)
{ {
unsigned int treeSize = 0;
TreeNode* data = nextTreeData(root);
while (data != NULL)
{
treeSize++;
data = nextTreeData(NULL);
}
return treeSize;
} }