Compare commits
2 Commits
35b09294bc
...
6b3294d40b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6b3294d40b | ||
|
|
a018971c04 |
29
bintree.c
29
bintree.c
@ -2,6 +2,7 @@
|
||||
#include "stack.h"
|
||||
#include "bintree.h"
|
||||
|
||||
static StackNode *stack;
|
||||
// TODO: binären Suchbaum implementieren
|
||||
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv),
|
||||
* `clearTree`: gibt den gesamten Baum frei (rekursiv),
|
||||
@ -12,25 +13,23 @@
|
||||
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
|
||||
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
||||
{
|
||||
TreeNode newNode = {data, NULL, NULL};
|
||||
|
||||
|
||||
if(root == NULL)
|
||||
if (root == NULL)
|
||||
{
|
||||
return &newNode;
|
||||
TreeNode *newNode = malloc(sizeof(TreeNode));
|
||||
newNode->data = malloc(dataSize);
|
||||
newNode->data = data;
|
||||
newNode->left = NULL;
|
||||
newNode->right = NULL;
|
||||
return newNode;
|
||||
}
|
||||
|
||||
if (data < root->data)
|
||||
if (compareFct(root->data, data) < 0)
|
||||
{
|
||||
root->left = addToTree(root->left, data, dataSize,compareFct, isDuplicate);
|
||||
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||||
}
|
||||
else if(data > root->data)
|
||||
else if (compareFct(root->data, data) > 0)
|
||||
{
|
||||
root->right = addToTree(root->right, data, dataSize,compareFct, isDuplicate);
|
||||
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
@ -39,9 +38,11 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
|
||||
// push the top node and push all its left nodes.
|
||||
void *nextTreeData(TreeNode *root)
|
||||
{
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Releases all memory resources (including data copies).
|
||||
void clearTree(TreeNode *root)
|
||||
{
|
||||
@ -49,7 +50,7 @@ void clearTree(TreeNode *root)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (root->left != NULL)
|
||||
{
|
||||
clearTree(root->left);
|
||||
|
||||
2
stack.c
2
stack.c
@ -13,7 +13,7 @@ StackNode *push(StackNode *stack, void *data)
|
||||
StackNode *tempNode, *newNode;
|
||||
|
||||
newNode = malloc(sizeof(StackNode));
|
||||
newNode->value = *(int *)data;
|
||||
newNode->value = data;
|
||||
newNode->next = NULL;
|
||||
|
||||
if (stack == NULL)
|
||||
|
||||
2
stack.h
2
stack.h
@ -9,7 +9,7 @@ The latest element is taken from the stack. */
|
||||
|
||||
//TODO: passenden Datentyp als struct anlegen
|
||||
typedef struct Node {
|
||||
int value;
|
||||
void *value;
|
||||
struct Node* next;
|
||||
} StackNode;
|
||||
|
||||
|
||||
@ -25,7 +25,7 @@ void test_push(void)
|
||||
TEST_ASSERT_EQUAL_INT(2, testNode->next->value);
|
||||
}
|
||||
|
||||
StackNode* setup(int value, StackNode* next) {
|
||||
StackNode* setup(void *value, StackNode* next) {
|
||||
StackNode* node = malloc(sizeof(StackNode)); // allocate memory on heap
|
||||
if (node == NULL) {
|
||||
perror("malloc failed");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user