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 "stack.h"
|
||||||
#include "bintree.h"
|
#include "bintree.h"
|
||||||
|
|
||||||
|
static StackNode *stack;
|
||||||
// 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),
|
||||||
@ -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).
|
// 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 *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 (compareFct(root->data, data) < 0)
|
||||||
if (data < root->data)
|
|
||||||
{
|
{
|
||||||
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;
|
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.
|
// push the top node and push all its left nodes.
|
||||||
void *nextTreeData(TreeNode *root)
|
void *nextTreeData(TreeNode *root)
|
||||||
{
|
{
|
||||||
|
return NULL;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Releases all memory resources (including data copies).
|
// Releases all memory resources (including data copies).
|
||||||
void clearTree(TreeNode *root)
|
void clearTree(TreeNode *root)
|
||||||
{
|
{
|
||||||
@ -49,7 +50,7 @@ void clearTree(TreeNode *root)
|
|||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (root->left != NULL)
|
if (root->left != NULL)
|
||||||
{
|
{
|
||||||
clearTree(root->left);
|
clearTree(root->left);
|
||||||
|
|||||||
2
stack.c
2
stack.c
@ -13,7 +13,7 @@ StackNode *push(StackNode *stack, void *data)
|
|||||||
StackNode *tempNode, *newNode;
|
StackNode *tempNode, *newNode;
|
||||||
|
|
||||||
newNode = malloc(sizeof(StackNode));
|
newNode = malloc(sizeof(StackNode));
|
||||||
newNode->value = *(int *)data;
|
newNode->value = data;
|
||||||
newNode->next = NULL;
|
newNode->next = NULL;
|
||||||
|
|
||||||
if (stack == 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
|
//TODO: passenden Datentyp als struct anlegen
|
||||||
typedef struct Node {
|
typedef struct Node {
|
||||||
int value;
|
void *value;
|
||||||
struct Node* next;
|
struct Node* next;
|
||||||
} StackNode;
|
} StackNode;
|
||||||
|
|
||||||
|
|||||||
@ -25,7 +25,7 @@ void test_push(void)
|
|||||||
TEST_ASSERT_EQUAL_INT(2, testNode->next->value);
|
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
|
StackNode* node = malloc(sizeof(StackNode)); // allocate memory on heap
|
||||||
if (node == NULL) {
|
if (node == NULL) {
|
||||||
perror("malloc failed");
|
perror("malloc failed");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user