generated from freudenreichan/info2Praktikum-DobleSpiel
129 lines
3.1 KiB
C
129 lines
3.1 KiB
C
#include <stdlib.h>
|
||
#include <string.h>
|
||
#include "bintree.h"
|
||
|
||
// --------------------------
|
||
// Hilfsfunktion für Vergleich von unsigned int
|
||
// --------------------------
|
||
static int compareUint(const void *a, const void *b)
|
||
{
|
||
unsigned int x = *(unsigned int *)a;
|
||
unsigned int y = *(unsigned int *)b;
|
||
if (x < y) return -1;
|
||
if (x > y) return 1;
|
||
return 0;
|
||
}
|
||
|
||
// --------------------------
|
||
// addToTree
|
||
// --------------------------
|
||
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
||
{
|
||
if (!compareFct)
|
||
compareFct = compareUint;
|
||
|
||
if (!root)
|
||
{
|
||
TreeNode *node = malloc(sizeof(TreeNode));
|
||
if (!node) return NULL;
|
||
node->data = malloc(dataSize);
|
||
if (!node->data)
|
||
{
|
||
free(node);
|
||
return NULL;
|
||
}
|
||
memcpy(node->data, data, dataSize);
|
||
node->left = node->right = NULL;
|
||
if (isDuplicate) *isDuplicate = 0;
|
||
return node;
|
||
}
|
||
|
||
int cmp = compareFct(data, root->data);
|
||
if (cmp == 0)
|
||
{
|
||
if (isDuplicate) *isDuplicate = 1;
|
||
return root;
|
||
}
|
||
else if (cmp < 0)
|
||
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||
else
|
||
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
||
|
||
return root;
|
||
}
|
||
|
||
// --------------------------
|
||
// clearTree
|
||
// --------------------------
|
||
void clearTree(TreeNode *root)
|
||
{
|
||
if (!root) return;
|
||
clearTree(root->left);
|
||
clearTree(root->right);
|
||
free(root->data);
|
||
free(root);
|
||
}
|
||
|
||
// --------------------------
|
||
// treeSize
|
||
// --------------------------
|
||
unsigned int treeSize(const TreeNode *root)
|
||
{
|
||
if (!root) return 0;
|
||
return 1 + treeSize(root->left) + treeSize(root->right);
|
||
}
|
||
|
||
// --------------------------
|
||
// nextTreeData (In-Order Traversal mit Stack) – minimal
|
||
// --------------------------
|
||
#include "stack.h"
|
||
void *nextTreeData(TreeNode *root)
|
||
{
|
||
static StackNode *stack = NULL;
|
||
static TreeNode *current = NULL;
|
||
|
||
if (root) current = root;
|
||
|
||
while (current)
|
||
{
|
||
stack = push(stack, current);
|
||
current = current->left;
|
||
}
|
||
|
||
if (!stack) return NULL;
|
||
|
||
TreeNode *node = top(stack);
|
||
stack = pop(stack);
|
||
|
||
current = node->right;
|
||
return node->data;
|
||
}
|
||
|
||
// --------------------------
|
||
// Zusätzliche Hilfsfunktion contains für numbers.c
|
||
// --------------------------
|
||
int contains(TreeNode *root, unsigned int value)
|
||
{
|
||
if (!root) return 0;
|
||
unsigned int nodeValue = *(unsigned int *)root->data;
|
||
if (value == nodeValue) return 1;
|
||
if (value < nodeValue) return contains(root->left, value);
|
||
return contains(root->right, value);
|
||
}
|
||
|
||
// --------------------------
|
||
// Hilfsfunktion insert für numbers.c
|
||
// --------------------------
|
||
TreeNode* insert(TreeNode *root, unsigned int value)
|
||
{
|
||
int isDup = 0;
|
||
return addToTree(root, &value, sizeof(unsigned int), compareUint, &isDup);
|
||
}
|
||
|
||
// --------------------------
|
||
// Hilfsfunktion freeTree für numbers.c
|
||
// --------------------------
|
||
void freeTree(TreeNode *root)
|
||
{
|
||
clearTree(root);
|
||
} |