Compare commits
No commits in common. "2176383f1e6b9c77bf017c7d30436ebe3e15ce61" and "7dca38c37f9956a00aa20c238234744dcb6580c4" have entirely different histories.
2176383f1e
...
7dca38c37f
59
bintree.c
59
bintree.c
@ -4,8 +4,8 @@
|
|||||||
|
|
||||||
|
|
||||||
//TODO: binären Suchbaum implementieren
|
//TODO: binären Suchbaum implementieren
|
||||||
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv), Done
|
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv),
|
||||||
* `clearTree`: gibt den gesamten Baum frei (rekursiv), Done
|
* `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. */
|
||||||
|
|
||||||
@ -18,17 +18,14 @@
|
|||||||
void clearTree (TreeNode *root);
|
void clearTree (TreeNode *root);
|
||||||
unsigned int treeSize (const TreeNode *root);
|
unsigned int treeSize (const TreeNode *root);
|
||||||
|
|
||||||
// self declared functions
|
// selfdeclaredfunctions
|
||||||
TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate);
|
TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate);
|
||||||
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
|
// 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).
|
||||||
|
|
||||||
// returned Value is new root
|
// returned Value is new root?
|
||||||
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)
|
||||||
{
|
{
|
||||||
// create a node
|
// create a node
|
||||||
@ -66,11 +63,11 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType
|
|||||||
//duplicate
|
//duplicate
|
||||||
if (isDuplicate == NULL)
|
if (isDuplicate == NULL)
|
||||||
{
|
{
|
||||||
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
|
return newNode;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
*isDuplicate = 1;
|
isDuplicate = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,8 +79,6 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType
|
|||||||
// 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.
|
// 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,
|
// 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.
|
// push the top node and push all its left nodes.
|
||||||
|
|
||||||
// Needs stack!!
|
|
||||||
void *nextTreeData(TreeNode *root)
|
void *nextTreeData(TreeNode *root)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -93,53 +88,13 @@ void *nextTreeData(TreeNode *root)
|
|||||||
// Releases all memory resources (including data copies).
|
// Releases all memory resources (including data copies).
|
||||||
void clearTree(TreeNode *root)
|
void clearTree(TreeNode *root)
|
||||||
{
|
{
|
||||||
clearTreeRec(root);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void clearTreeRec(TreeNode *currentNode)
|
|
||||||
{
|
|
||||||
if (currentNode != NULL)
|
|
||||||
{
|
|
||||||
clearTree(currentNode->left);
|
|
||||||
clearNode(currentNode);
|
|
||||||
clearTree(currentNode->right);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void clearNode(TreeNode *node)
|
|
||||||
{
|
|
||||||
free(node->data);
|
|
||||||
node->data = NULL;
|
|
||||||
|
|
||||||
node->left = NULL;
|
|
||||||
node->right = NULL;
|
|
||||||
|
|
||||||
free(node);
|
|
||||||
node = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 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 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user