bintree fertig
This commit is contained in:
parent
0c020ba402
commit
1a9eb873dd
77
bintree.c
77
bintree.c
@ -8,14 +8,15 @@
|
|||||||
* `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) return NULL;
|
if (!newNode)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
newNode->data = malloc(dataSize);
|
newNode->data = malloc(dataSize);
|
||||||
if(!newNode->data) {
|
if (!newNode->data)
|
||||||
|
{
|
||||||
free(newNode);
|
free(newNode);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -29,28 +30,37 @@ static TreeNode* createNode(const void* data, size_t dataSize) {
|
|||||||
// 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)
|
||||||
|
{ // wurzerl == 0;
|
||||||
TreeNode *newNode = createNode(data, dataSize);
|
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,16 +71,19 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,10 +91,12 @@ void *nextTreeData(TreeNode *root)
|
|||||||
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);
|
||||||
}
|
}
|
||||||
@ -89,21 +104,31 @@ void *nextTreeData(TreeNode *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;
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user