Compare commits
3
Commits
branchjens
...
7dca38c37f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7dca38c37f | ||
|
|
03495bddf0 | ||
|
|
1763ac1f6d |
@@ -2,19 +2,80 @@
|
|||||||
#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 *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);
|
||||||
|
|
||||||
|
// selfdeclaredfunctions
|
||||||
|
TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate);
|
||||||
|
|
||||||
|
|
||||||
// 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?
|
||||||
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
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate)
|
||||||
|
{
|
||||||
|
if (currentNode == NULL)
|
||||||
|
{
|
||||||
|
isDuplicate = 0;
|
||||||
|
return newNode;
|
||||||
|
}
|
||||||
|
else if (compareFct(newNode->data, currentNode->data) < 0)
|
||||||
|
{
|
||||||
|
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
|
||||||
|
}
|
||||||
|
else if (compareFct(newNode->data, currentNode->data) > 0)
|
||||||
|
{
|
||||||
|
currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//duplicate
|
||||||
|
if (isDuplicate == NULL)
|
||||||
|
{
|
||||||
|
return newNode;
|
||||||
|
}
|
||||||
|
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.
|
// 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.
|
||||||
@@ -23,14 +84,17 @@ 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)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user