forked from freudenreichan/info2Praktikum-DobleSpiel
Merge remote-tracking branch 'origin/main'
# Conflicts: # Startcode/makefile
This commit is contained in:
commit
13a138684b
8 files changed
+343
-15
No files matched your search
@@ -0,0 +1,45 @@
|
||||
# clang-format-Konfiguration, abgeleitet aus dem bestehenden Wortsalat-Code
|
||||
BasedOnStyle: LLVM
|
||||
|
||||
# Einrückung: 4 Leerzeichen, keine Tabs
|
||||
IndentWidth: 4
|
||||
TabWidth: 4
|
||||
UseTab: Never
|
||||
ContinuationIndentWidth: 4
|
||||
|
||||
# Allman-Stil: geschweifte Klammern für Funktionen, Schleifen, if/else stehen in eigenen Zeilen
|
||||
BreakBeforeBraces: Allman
|
||||
|
||||
# Keine automatischen Zeilenumbrüche, der Code bricht lange Zeilen bereits selbst um
|
||||
ColumnLimit: 0
|
||||
ReflowComments: false
|
||||
|
||||
# Zeiger-Sternchen rechts: char *word, SearchWord *words
|
||||
PointerAlignment: Right
|
||||
DerivePointerAlignment: false
|
||||
|
||||
# Kein Leerzeichen vor der Klammer bei if/for/while und bei Funktionsaufrufen
|
||||
SpaceBeforeParens: Custom
|
||||
SpaceBeforeParensOptions:
|
||||
AfterControlStatements: false
|
||||
AfterFunctionDeclarationName: false
|
||||
AfterFunctionDefinitionName: false
|
||||
SpaceAfterCStyleCast: false
|
||||
|
||||
# Kurze Konstrukte bleiben mehrzeilig
|
||||
AllowShortIfStatementsOnASingleLine: Never
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
AllowShortBlocksOnASingleLine: Never
|
||||
AllowShortFunctionsOnASingleLine: None
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
|
||||
# Geschweifte Initialisierer: {0, 0}
|
||||
Cpp11BracedListStyle: true
|
||||
|
||||
# Includes nicht umsortieren (Reihenfolge ist teils bewusst gewählt)
|
||||
SortIncludes: Never
|
||||
IncludeBlocks: Preserve
|
||||
|
||||
# Leerzeilen
|
||||
MaxEmptyLinesToKeep: 2
|
||||
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||
@@ -0,0 +1,5 @@
|
||||
unity/*
|
||||
raylib/*
|
||||
macos-*/*
|
||||
windows/*
|
||||
linux/*
|
||||
+87
-11
@@ -2,35 +2,111 @@
|
||||
#include "stack.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
|
||||
// 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.
|
||||
// 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.
|
||||
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).
|
||||
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.
|
||||
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;
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
Tobi;4999
|
||||
player1;3999
|
||||
+10
-4
@@ -32,18 +32,24 @@ doble : main.o $(program_obj_files)
|
||||
$(program_obj_files): %.o: %.c
|
||||
$(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)
|
||||
# --------------------------
|
||||
compiledb:
|
||||
bear -- $(CC) $(CFLAGS) -fsyntax-only -I$(raylibfolder) -I$(unityfolder) $(wildcard *.c)
|
||||
|
||||
# --------------------------
|
||||
# Clean
|
||||
# --------------------------
|
||||
clean:
|
||||
ifeq ($(OS),Windows_NT)
|
||||
del /f *.o doble numbersTests.exe
|
||||
del /f *.o doble
|
||||
else
|
||||
rm -f *.o doble numbersTests
|
||||
endif
|
||||
rm -f *.o doble
|
||||
endif
|
||||
@@ -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