bintree fertig
This commit is contained in:
parent
0c020ba402
commit
1a9eb873dd
107
bintree.c
107
bintree.c
@ -2,20 +2,21 @@
|
||||
#include "stack.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),
|
||||
* `clearTree`: gibt den gesamten Baum frei (rekursiv),
|
||||
* `treeSize`: zählt die Knoten im Baum (rekursiv),
|
||||
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
|
||||
* `clearTree`: gibt den gesamten Baum frei (rekursiv),
|
||||
* `treeSize`: zählt die Knoten im Baum (rekursiv),
|
||||
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
|
||||
|
||||
|
||||
|
||||
static TreeNode* createNode(const void* data, size_t dataSize) {
|
||||
TreeNode* newNode = calloc(1, sizeof(TreeNode));
|
||||
if(!newNode) return NULL;
|
||||
static TreeNode *createNode(const void *data, size_t dataSize)
|
||||
{
|
||||
TreeNode *newNode = calloc(1, sizeof(TreeNode));
|
||||
if (!newNode)
|
||||
return NULL;
|
||||
|
||||
newNode->data = malloc(dataSize);
|
||||
if(!newNode->data) {
|
||||
if (!newNode->data)
|
||||
{
|
||||
free(newNode);
|
||||
return 0;
|
||||
}
|
||||
@ -25,32 +26,41 @@ static TreeNode* createNode(const void* data, size_t dataSize) {
|
||||
}
|
||||
// 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).
|
||||
//1. Fall: isDuplicate = NULL -> accepts
|
||||
//2. Fall: other -> ignores duplicates and
|
||||
TreeNode *addToTree(TreeNode* root, const void* data, size_t dataSize, CompareFctType compareFct, int* isDuplicate)
|
||||
// 1. Fall: isDuplicate = NULL -> accepts
|
||||
// 2. Fall: other -> ignores duplicates and
|
||||
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
||||
{
|
||||
if(isDuplicate != NULL) {
|
||||
if (isDuplicate != NULL)
|
||||
{
|
||||
*isDuplicate = 0;
|
||||
}
|
||||
if(!root) { //wurzerl == 0;
|
||||
TreeNode* newNode = createNode(data, dataSize);
|
||||
if (!root)
|
||||
{ // wurzerl == 0;
|
||||
TreeNode *newNode = createNode(data, dataSize);
|
||||
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);
|
||||
} else if (compareFct(data, root->data) > 0) {
|
||||
}
|
||||
else if (compareFct(data, root->data) > 0)
|
||||
{
|
||||
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;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
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,
|
||||
@ -61,49 +71,64 @@ TreeNode *addToTree(TreeNode* root, const void* data, size_t dataSize, CompareFc
|
||||
void *nextTreeData(TreeNode *root)
|
||||
{
|
||||
static StackNode *stack = NULL;
|
||||
if (root != NULL) {
|
||||
if (root != NULL)
|
||||
{
|
||||
clearStack(stack);
|
||||
stack = push(NULL, root);
|
||||
while(root->left != NULL) {
|
||||
while (root->left != NULL)
|
||||
{
|
||||
root = root->left;
|
||||
stack = push(stack,root);
|
||||
stack = push(stack, root);
|
||||
}
|
||||
}
|
||||
|
||||
if (stack == NULL) {
|
||||
if (stack == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TreeNode* result = (TreeNode*)top(stack);
|
||||
TreeNode *result = (TreeNode *)top(stack);
|
||||
stack = pop(stack);
|
||||
root = result;
|
||||
|
||||
if (root->right != NULL) {
|
||||
if (root->right != NULL)
|
||||
{
|
||||
root = root->right;
|
||||
stack = push(stack,root);
|
||||
while(root->left != NULL) {
|
||||
stack = push(stack, root);
|
||||
while (root->left != NULL)
|
||||
{
|
||||
root = root->left;
|
||||
stack = push(stack,root);
|
||||
stack = push(stack, root);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Releases all memory resources (including data copies).
|
||||
void clearTree(TreeNode *root)
|
||||
{
|
||||
TreeNode* clear = nextTreeData(root);
|
||||
while(clear != NULL){
|
||||
free(clear->data);
|
||||
free(clear);
|
||||
clear = nextTreeData(NULL);
|
||||
if (root == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
clearTree(root->left);
|
||||
clearTree(root->right);
|
||||
|
||||
free(root->data);
|
||||
|
||||
free(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