generated from freudenreichan/info2Praktikum-DobleSpiel
Merge remote-tracking branch 'origin/main' into alex
This commit is contained in:
commit
6d869c2d3b
4 files changed
+162
-2
No files matched your search
+27
-2
@@ -7,11 +7,23 @@
|
|||||||
* `clearTree`: gibt den gesamten Baum frei (rekursiv),
|
* `clearTree`: gibt den gesamten Baum frei (rekursiv),
|
||||||
* `treeSize`: zählt die Knoten im Baum (rekursiv),
|
* `treeSize`: zählt die Knoten im Baum (rekursiv),
|
||||||
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
|
* `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 *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
||||||
{
|
{
|
||||||
|
// create new node
|
||||||
|
TreeNode* newNode = (TreeNode *)calloc(1, sizeof(TreeNode));
|
||||||
|
newNode->data = calloc(1, dataSize);
|
||||||
|
memcpy(newNode->data, data, dataSize);
|
||||||
|
|
||||||
|
if (root == NULL) return newNode;
|
||||||
|
if (compareFct(data, root->data)) {
|
||||||
|
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||||||
|
} else if (compareFct(root->data, data)) {
|
||||||
|
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,11 +38,24 @@ void *nextTreeData(TreeNode *root)
|
|||||||
// Releases all memory resources (including data copies).
|
// Releases all memory resources (including data copies).
|
||||||
void clearTree(TreeNode *root)
|
void clearTree(TreeNode *root)
|
||||||
{
|
{
|
||||||
|
if(root->left != NULL){
|
||||||
|
clearTree(root->left);
|
||||||
|
free(root->left->data);
|
||||||
|
free(root->left);
|
||||||
|
root->left = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(root->right != NULL){
|
||||||
|
clearTree(root->right);
|
||||||
|
free(root->right->data);
|
||||||
|
free(root->right);
|
||||||
|
root->right;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 *root)
|
||||||
{
|
{
|
||||||
|
if(root == NULL) return 0;
|
||||||
|
return 1 + treeSize(root->left) + treeSize(root->right);
|
||||||
}
|
}
|
||||||
@@ -14,11 +14,67 @@
|
|||||||
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
|
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
|
||||||
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
||||||
// creating random numbers.
|
// creating random numbers.
|
||||||
|
|
||||||
|
|
||||||
|
static unsigned int randNumExists(unsigned int randNum, TreeNode *node){
|
||||||
|
unsigned int randExists = 0;
|
||||||
|
while(node != NULL || node->left != NULL || node->right != NULL){
|
||||||
|
if(node->data == randNum){
|
||||||
|
randExists = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(randNum < node->data){
|
||||||
|
node = node->left;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
node = node->right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
unsigned int *createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
|
srand(time(NULL));
|
||||||
|
int randomNumber = rand() % len + len;
|
||||||
|
TreeNode randRoot;
|
||||||
|
randRoot.data = randomNumber;
|
||||||
|
randRoot.left = NULL;
|
||||||
|
randRoot.right = NULL;
|
||||||
|
unsigned int randomNumbers[len];
|
||||||
|
randomNumbers[0] = randRoot.data;
|
||||||
|
int i = 1;
|
||||||
|
int pairExists = 0;
|
||||||
|
while(i < len-1){
|
||||||
|
randomNumber = rand() % len + len;
|
||||||
|
if(!randNumExists(randomNumber, &randRoot)){
|
||||||
|
randomNumbers[i++] = randomNumber;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
if(!pairExists){
|
||||||
|
randomNumbers[i++] = randomNumber;
|
||||||
|
pairExists = 1;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
{
|
||||||
|
randomNumber = rand() % len + len;
|
||||||
|
}while(randNumExists(randomNumber));
|
||||||
|
randomNumbers[i++] = randomNumber;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!pairExists){
|
||||||
|
randomNumbers[len-1] = randomNumbers[rand() % (len-1)];
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
randomNumbers[len-1] = rand() % len + len;
|
||||||
|
}
|
||||||
|
|
||||||
|
return randomNumbers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
||||||
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -12,7 +12,9 @@ StackNode *push(StackNode *stack, void *data)
|
|||||||
{
|
{
|
||||||
StackNode *newItem = (StackNode*)calloc(1, sizeof(StackNode));
|
StackNode *newItem = (StackNode*)calloc(1, sizeof(StackNode));
|
||||||
if (newItem == NULL) return NULL;
|
if (newItem == NULL) return NULL;
|
||||||
|
if (stack != NULL) {
|
||||||
stack->left = newItem;
|
stack->left = newItem;
|
||||||
|
}
|
||||||
newItem->right = stack;
|
newItem->right = stack;
|
||||||
newItem->left = NULL;
|
newItem->left = NULL;
|
||||||
newItem->data = data;
|
newItem->data = data;
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
#include "unity.h"
|
||||||
|
#include "stack.h"
|
||||||
|
|
||||||
|
void test_stackCreate() {
|
||||||
|
int* firstData = calloc(1, sizeof(int));
|
||||||
|
*firstData = 1;
|
||||||
|
StackNode* initial = push(NULL, firstData);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(1, *((int *)initial->data));
|
||||||
|
TEST_ASSERT_NULL(initial->left);
|
||||||
|
TEST_ASSERT_NULL(initial->right);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_stackPush() {
|
||||||
|
int* firstData = calloc(1, sizeof(int));
|
||||||
|
*firstData = 1;
|
||||||
|
int* secondData = calloc(1, sizeof(int));
|
||||||
|
*secondData = 2;
|
||||||
|
|
||||||
|
StackNode* tree = push(NULL, firstData);
|
||||||
|
tree = push(tree, secondData);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(2, *((int *)tree->data));
|
||||||
|
TEST_ASSERT_EQUAL_INT(2, *((int*)top(tree)));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_stackPop() {
|
||||||
|
int* firstData = calloc(1, sizeof(int));
|
||||||
|
*firstData = 1;
|
||||||
|
int* secondData = calloc(1, sizeof(int));
|
||||||
|
*secondData = 2;
|
||||||
|
|
||||||
|
StackNode* tree = push(NULL, firstData);
|
||||||
|
tree = push(tree, secondData);
|
||||||
|
|
||||||
|
free(tree->data);
|
||||||
|
tree = pop(tree);
|
||||||
|
TEST_ASSERT_EQUAL_INT(1, *((int*)top(tree)));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// void test_stackClear() {
|
||||||
|
// int* firstData = calloc(1, sizeof(int));
|
||||||
|
// *firstData = 1;
|
||||||
|
// int* secondData = calloc(1, sizeof(int));
|
||||||
|
// *secondData = 2;
|
||||||
|
|
||||||
|
// StackNode* stack = push(NULL, firstData);
|
||||||
|
// stack = push(stack, secondData);
|
||||||
|
|
||||||
|
// clearStack(stack);
|
||||||
|
// TEST_ASSERT_NULL(stack);
|
||||||
|
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
void setUp(void) {
|
||||||
|
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
||||||
|
}
|
||||||
|
|
||||||
|
void tearDown(void) {
|
||||||
|
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
printf("\n============================\nStack tests\n============================\n");
|
||||||
|
RUN_TEST(test_stackCreate);
|
||||||
|
RUN_TEST(test_stackPush);
|
||||||
|
RUN_TEST(test_stackPop);
|
||||||
|
// RUN_TEST(test_stackClear);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
Reference in new issue
Block a user