177 lines
4.9 KiB
C
177 lines
4.9 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include "stack.h"
|
|
#include "bintree.h"
|
|
|
|
|
|
//TODO: binären Suchbaum implementieren
|
|
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv), Done
|
|
* `clearTree`: gibt den gesamten Baum frei (rekursiv), Done
|
|
* `treeSize`: zählt die Knoten im Baum (rekursiv), Done
|
|
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
|
|
|
|
|
|
static TreeNode *root = NULL;
|
|
|
|
|
|
TreeNode *addToTree (TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate);
|
|
void *nextTreeData (TreeNode *root);
|
|
void clearTree (TreeNode *root);
|
|
unsigned int treeSize (const TreeNode *root);
|
|
|
|
// self declared functions
|
|
TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate, const int root);
|
|
void clearTreeRec (TreeNode *currentNode);
|
|
void clearNode (TreeNode *node);
|
|
void treeSizeRec (const TreeNode *currentNode, unsigned int *nodeCount);
|
|
|
|
|
|
// 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).
|
|
|
|
// returned Value is new root
|
|
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
|
{
|
|
// create a node
|
|
TreeNode *newNode;
|
|
|
|
|
|
newNode = calloc(1, sizeof(TreeNode));
|
|
newNode->data = calloc(1, dataSize);
|
|
newNode->left = NULL;
|
|
newNode->right = NULL;
|
|
memcpy(newNode->data, data, dataSize);
|
|
|
|
|
|
return addToTreeRec(root, newNode, compareFct, isDuplicate, 1);
|
|
}
|
|
|
|
|
|
TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate, const int root)
|
|
{
|
|
if ((currentNode == NULL))
|
|
{
|
|
// printf("Node == NULL\n");
|
|
if ((isDuplicate == NULL) || root)
|
|
{
|
|
return newNode;
|
|
}
|
|
else
|
|
{
|
|
return currentNode;
|
|
}
|
|
}
|
|
else if ((compareFct(currentNode->data, newNode->data) < 0))
|
|
{
|
|
// printf("<0\n");
|
|
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0);
|
|
}
|
|
else if ((compareFct(currentNode->data, newNode->data) > 0))
|
|
{
|
|
// printf(">0\n");
|
|
currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate, 0);
|
|
}
|
|
else if ((compareFct(currentNode->data, newNode->data) == 0))
|
|
{
|
|
// printf("==0\n");
|
|
if (isDuplicate == NULL)
|
|
{
|
|
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0);
|
|
}
|
|
else
|
|
{
|
|
*isDuplicate = 1;
|
|
}
|
|
}
|
|
|
|
|
|
return currentNode;
|
|
}
|
|
|
|
|
|
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction.
|
|
// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element,
|
|
// push the top node and push all its left nodes.
|
|
|
|
// Needs stack!!
|
|
void *nextTreeData(TreeNode *root)
|
|
{
|
|
|
|
}
|
|
|
|
|
|
// Releases all memory resources (including data copies).
|
|
void clearTree(TreeNode *root)
|
|
{
|
|
clearTreeRec(root);
|
|
}
|
|
|
|
|
|
void clearTreeRec(TreeNode *currentNode)
|
|
{
|
|
if (currentNode != NULL)
|
|
{
|
|
clearTree(currentNode->left);
|
|
|
|
clearTree(currentNode->right);
|
|
|
|
clearNode(currentNode);
|
|
/*
|
|
printf("1\n");
|
|
free(¤tNode->data);
|
|
currentNode->data = NULL;
|
|
printf("2\n");
|
|
// free(currentNode);
|
|
// currentNode = NULL;
|
|
printf("3\n");
|
|
// */
|
|
}
|
|
}
|
|
|
|
|
|
void clearNode(TreeNode *node)
|
|
{
|
|
// printf("in clearNode\n");
|
|
// printf("node-> data = %u\n", node->data);
|
|
// printf("node-> data = %u\n", _ADDRESSOF(node->data));
|
|
free(node->data);
|
|
node->data = NULL;
|
|
// printf("node-> data = %u\n", &node->data);
|
|
// printf("data freed \n");
|
|
node->left = NULL;
|
|
node->right = NULL;
|
|
// printf("left & right = Null\n");
|
|
printf("node = %u\n", &node);
|
|
// printf("node = %u\n", _ADDRESSOF(node));
|
|
free(node);
|
|
// printf("node = %d\n", node);
|
|
node = NULL;
|
|
// printf("node = %u\n", &node);
|
|
// printf("freed node\n");
|
|
}
|
|
|
|
|
|
// Returns the number of entries in the tree given by root.
|
|
unsigned int treeSize(const TreeNode *root)
|
|
{
|
|
unsigned int amountOfNodes = 0;
|
|
|
|
|
|
treeSizeRec(root, &amountOfNodes);
|
|
|
|
|
|
return amountOfNodes;
|
|
}
|
|
|
|
void treeSizeRec(const TreeNode *currentNode, unsigned int *nodeCount)
|
|
{
|
|
if (currentNode != NULL)
|
|
{
|
|
treeSizeRec(currentNode->left, nodeCount);
|
|
*nodeCount++;
|
|
treeSizeRec(currentNode->right, nodeCount);
|
|
}
|
|
}
|
|
|