forked from freudenreichan/info2Praktikum-DobleSpiel
Implement bintree.c
This commit is contained in:
3 files changed
+128
-17
No files matched your search
+87
-11
@@ -2,35 +2,111 @@
|
|||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
#include "bintree.h"
|
#include "bintree.h"
|
||||||
|
|
||||||
//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. */
|
|
||||||
|
|
||||||
// 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).
|
||||||
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
TreeNode *addToTree(TreeNode *root, const void *const data, const size_t dataSize, const CompareFctType compareFct, int *isDuplicate)
|
||||||
{
|
{
|
||||||
|
if(data == NULL || dataSize == 0 || compareFct == NULL)
|
||||||
|
return root;
|
||||||
|
|
||||||
|
if(root == NULL)
|
||||||
|
{
|
||||||
|
if(isDuplicate != NULL)
|
||||||
|
*isDuplicate = 0;
|
||||||
|
|
||||||
|
root = malloc(sizeof(TreeNode));
|
||||||
|
if(root == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
*root = (TreeNode){
|
||||||
|
.data = malloc(dataSize),
|
||||||
|
.left = NULL,
|
||||||
|
.right = NULL,
|
||||||
|
};
|
||||||
|
|
||||||
|
if(root->data == NULL)
|
||||||
|
{
|
||||||
|
free(root);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(root->data, data, dataSize);
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int comparisonValue = compareFct(data, root->data);
|
||||||
|
|
||||||
|
if(comparisonValue > 0)
|
||||||
|
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
||||||
|
else if(comparisonValue < 0 || isDuplicate == NULL)
|
||||||
|
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||||||
|
|
||||||
|
if(isDuplicate != NULL && comparisonValue == 0)
|
||||||
|
*isDuplicate = 1;
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
static StackNode *treeNodeHistory = NULL;
|
||||||
|
|
||||||
|
static TreeNode *getSmallest(TreeNode *const root)
|
||||||
|
{
|
||||||
|
TreeNode *smallest = root;
|
||||||
|
while(smallest != NULL)
|
||||||
|
{
|
||||||
|
treeNodeHistory = push(treeNodeHistory, smallest);
|
||||||
|
smallest = smallest->left;
|
||||||
|
}
|
||||||
|
|
||||||
|
return top(treeNodeHistory);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
void *nextTreeData(TreeNode *root)
|
void *nextTreeData(TreeNode *const root)
|
||||||
{
|
{
|
||||||
|
if(root != NULL)
|
||||||
|
{
|
||||||
|
clearStack(treeNodeHistory);
|
||||||
|
treeNodeHistory = NULL;
|
||||||
|
return getSmallest(root)->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(treeNodeHistory == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
TreeNode *last = top(treeNodeHistory);
|
||||||
|
treeNodeHistory = pop(treeNodeHistory);
|
||||||
|
|
||||||
|
if(last->right == NULL)
|
||||||
|
{
|
||||||
|
TreeNode *const current = top(treeNodeHistory);
|
||||||
|
return current == NULL ? NULL : current->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
return getSmallest(last->right)->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Releases all memory resources (including data copies).
|
// Releases all memory resources (including data copies).
|
||||||
void clearTree(TreeNode *root)
|
void clearTree(TreeNode *const root)
|
||||||
{
|
{
|
||||||
|
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.
|
// Returns the number of entries in the tree given by root.
|
||||||
unsigned int treeSize(const TreeNode *root)
|
unsigned int treeSize(const TreeNode *const root)
|
||||||
{
|
{
|
||||||
|
if(root == NULL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
}
|
return treeSize(root->left) + treeSize(root->right) + 1;
|
||||||
|
}
|
||||||
+6
-6
@@ -32,18 +32,18 @@ doble : main.o $(program_obj_files)
|
|||||||
$(program_obj_files): %.o: %.c
|
$(program_obj_files): %.o: %.c
|
||||||
$(CC) -c $(FLAGS) $^ -o $@
|
$(CC) -c $(FLAGS) $^ -o $@
|
||||||
|
|
||||||
|
# --------------------------
|
||||||
|
# Unit Tests
|
||||||
|
# --------------------------
|
||||||
|
bintreeTests: bintree.o test_bintree.c stack.o $(unityfolder)/unity.c
|
||||||
|
$(CC) $(CFLAGS) -I$(unityfolder) -o runBintreeTests test_bintree.c bintree.o stack.o $(unityfolder)/unity.c
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# compile_commands.json für clangd erzeugen (benötigt bear)
|
# compile_commands.json für clangd erzeugen (benötigt bear)
|
||||||
# --------------------------
|
# --------------------------
|
||||||
compiledb:
|
compiledb:
|
||||||
bear -- $(CC) $(CFLAGS) -fsyntax-only -I$(raylibfolder) -I$(unityfolder) $(wildcard *.c)
|
bear -- $(CC) $(CFLAGS) -fsyntax-only -I$(raylibfolder) -I$(unityfolder) $(wildcard *.c)
|
||||||
|
|
||||||
# --------------------------
|
|
||||||
# Unit Tests
|
|
||||||
# --------------------------
|
|
||||||
unitTests:
|
|
||||||
echo "needs to be implemented"
|
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Clean
|
# Clean
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "unity.h"
|
||||||
|
|
||||||
|
void test_erfolg(void)
|
||||||
|
{
|
||||||
|
int number = 1;
|
||||||
|
TEST_ASSERT_NOT_NULL(&number);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_fehler(void)
|
||||||
|
{
|
||||||
|
int *number = NULL;
|
||||||
|
TEST_ASSERT_NOT_NULL(number);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setUp(void)
|
||||||
|
{
|
||||||
|
// TODO: Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
||||||
|
}
|
||||||
|
|
||||||
|
void tearDown(void)
|
||||||
|
{
|
||||||
|
// TODO: Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
printf("\n============================\nbinary tree tests\n============================\n");
|
||||||
|
RUN_TEST(test_erfolg);
|
||||||
|
RUN_TEST(test_fehler);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
Reference in new issue
Block a user