Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
01bc613189 | ||
|
|
79a6eb7267 | ||
|
|
53c12ce0ed | ||
|
|
8deeb6417e | ||
|
|
00fafec3dc | ||
|
|
cc927ad723 |
@@ -2,144 +2,35 @@
|
||||
#include "stack.h"
|
||||
#include "bintree.h"
|
||||
|
||||
|
||||
//TODO: binären Suchbaum implementieren
|
||||
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv), Done
|
||||
* `clearTree`: gibt den gesamten Baum frei (rekursiv), Done
|
||||
/* * `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. */
|
||||
|
||||
|
||||
static TreeNode *root = NULL;
|
||||
|
||||
|
||||
TreeNode *addToTree (TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate);
|
||||
void *nextTreeData (TreeNode *root);
|
||||
void clearTree (TreeNode *root);
|
||||
unsigned int treeSize (const TreeNode *root);
|
||||
|
||||
// self declared functions
|
||||
TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate);
|
||||
void clearTreeRec (TreeNode *currentNode);
|
||||
void clearNode (TreeNode *node);
|
||||
void treeSizeRec (const TreeNode *currentNode, unsigned int *nodeCount);
|
||||
|
||||
|
||||
// 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).
|
||||
|
||||
// returned Value is new root
|
||||
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
||||
{
|
||||
// create a node
|
||||
TreeNode *newNode;
|
||||
|
||||
|
||||
newNode = calloc(1, sizeof(TreeNode));
|
||||
newNode->data = calloc(1, dataSize);
|
||||
newNode->left = NULL;
|
||||
newNode->right = NULL;
|
||||
memcpy(newNode->data, data, dataSize);
|
||||
|
||||
|
||||
return addToTreeRec(root, newNode, compareFct, isDuplicate);
|
||||
}
|
||||
|
||||
|
||||
TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate)
|
||||
{
|
||||
if (currentNode == NULL)
|
||||
{
|
||||
isDuplicate = 0;
|
||||
return newNode;
|
||||
}
|
||||
else if (compareFct(newNode->data, currentNode->data) < 0)
|
||||
{
|
||||
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
|
||||
}
|
||||
else if (compareFct(newNode->data, currentNode->data) > 0)
|
||||
{
|
||||
currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate);
|
||||
}
|
||||
else
|
||||
{
|
||||
//duplicate
|
||||
if (isDuplicate == NULL)
|
||||
{
|
||||
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
|
||||
}
|
||||
else
|
||||
{
|
||||
*isDuplicate = 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return currentNode;
|
||||
}
|
||||
|
||||
|
||||
// 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.
|
||||
|
||||
// Needs stack!!
|
||||
void *nextTreeData(TreeNode *root)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Releases all memory resources (including data copies).
|
||||
void clearTree(TreeNode *root)
|
||||
{
|
||||
clearTreeRec(root);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void clearTreeRec(TreeNode *currentNode)
|
||||
{
|
||||
if (currentNode != NULL)
|
||||
{
|
||||
clearTree(currentNode->left);
|
||||
clearNode(currentNode);
|
||||
clearTree(currentNode->right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void clearNode(TreeNode *node)
|
||||
{
|
||||
free(node->data);
|
||||
node->data = NULL;
|
||||
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
|
||||
free(node);
|
||||
node = NULL;
|
||||
}
|
||||
|
||||
|
||||
// Returns the number of entries in the tree given by root.
|
||||
unsigned int treeSize(const TreeNode *root)
|
||||
{
|
||||
unsigned int amountOfNodes = 0;
|
||||
|
||||
|
||||
treeSizeRec(root, &amountOfNodes);
|
||||
|
||||
|
||||
return amountOfNodes;
|
||||
}
|
||||
|
||||
void treeSizeRec(const TreeNode *currentNode, unsigned int *nodeCount)
|
||||
{
|
||||
if (currentNode != NULL)
|
||||
{
|
||||
treeSizeRec(currentNode->left, nodeCount);
|
||||
*nodeCount++;
|
||||
treeSizeRec(currentNode->right, nodeCount);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include "unity.h"
|
||||
#include "bintree.h"
|
||||
|
||||
#define MAX_TEST_NAME_LEN 10
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char name[MAX_TEST_NAME_LEN];
|
||||
int score;
|
||||
} HighscoreEntry;
|
||||
|
||||
|
||||
// test if addToTree expands tree correctly
|
||||
// by going down the path where the given pice of data is expected
|
||||
// and checking if the pice of data is found there
|
||||
void test_addToTreeExpandsTreeCorrectlyNoDoubles(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
// test if nextTreeData returns the next pice of data correctly
|
||||
// needs Stack!!!
|
||||
|
||||
|
||||
// test if clear Tree frees all node.name and node memory AND sets them to zero
|
||||
// aditionally tests if the memoryspaces have been cleared
|
||||
|
||||
|
||||
// tests if treeSize returns correct amount of nodes in Tree
|
||||
// by using addNode a given number of times and testing to see if
|
||||
// the treeSize matches the number of nodes added
|
||||
|
||||
|
||||
// main, strings together all tests
|
||||
int main()
|
||||
{
|
||||
printf("\n============================\nBinary Tree tests\n============================\n");
|
||||
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1 +1,2 @@
|
||||
player_name;9943
|
||||
player1;3999
|
||||
|
||||
@@ -38,6 +38,11 @@ $(program_obj_filesobj_files): %.o: %.c
|
||||
unitTests:
|
||||
echo "needs to be implemented"
|
||||
|
||||
stack: stack.c
|
||||
$(CC) $(FLAGS) -c stack stack.c
|
||||
|
||||
test_stack: stack.o test_stack.c $(unityfolder)/unity.c
|
||||
$(CC) $(FLAGS) -o runstackTests test_stack.c stack.o $(unityfolder)/unity.c
|
||||
# --------------------------
|
||||
# Clean
|
||||
# --------------------------
|
||||
|
||||
@@ -10,24 +10,64 @@
|
||||
// Pushes data as pointer onto the stack.
|
||||
StackNode *push(StackNode *stack, void *data)
|
||||
{
|
||||
StackNode *newNode = malloc(sizeof(StackNode));
|
||||
if(newNode == NULL)
|
||||
{
|
||||
//printf("Fehler Bei Speicherallozierung!");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
newNode->data = data;
|
||||
newNode->next = stack;
|
||||
|
||||
return newNode;
|
||||
}
|
||||
|
||||
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
||||
// freed by caller.)
|
||||
StackNode *pop(StackNode *stack)
|
||||
{
|
||||
if(stack == NULL)
|
||||
{
|
||||
//printf("Fehlerhafte Adresse uebergeben");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
StackNode *next = stack->next;
|
||||
|
||||
free(stack);
|
||||
|
||||
return next;
|
||||
}
|
||||
|
||||
// Returns the data of the top element.
|
||||
void *top(StackNode *stack)
|
||||
{
|
||||
if(stack == NULL)
|
||||
{
|
||||
//printf("Fehlerhafte Adresse uebergeben");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return stack->data;
|
||||
}
|
||||
|
||||
// Clears stack and releases all memory.
|
||||
void clearStack(StackNode *stack)
|
||||
{
|
||||
if(stack == NULL)
|
||||
{
|
||||
//printf("Fehlerhafte Adresse uebergeben");
|
||||
return;
|
||||
}
|
||||
|
||||
StackNode *currentNode = stack;
|
||||
StackNode *nextNode;
|
||||
|
||||
while(currentNode != NULL)
|
||||
{
|
||||
nextNode = currentNode->next;
|
||||
free(currentNode);
|
||||
currentNode = nextNode;
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,11 @@ The latest element is taken from the stack. */
|
||||
#include <stdlib.h>
|
||||
|
||||
//TODO: passenden Datentyp als struct anlegen
|
||||
typedef struct StackNode
|
||||
{
|
||||
void *data;
|
||||
struct StackNode *next;
|
||||
} StackNode;
|
||||
|
||||
// Pushes data as pointer onto the stack.
|
||||
StackNode *push(StackNode *stack, void *data);
|
||||
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "unity.h"
|
||||
#include "stack.h"
|
||||
|
||||
void setUp(void) {
|
||||
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
||||
}
|
||||
|
||||
void tearDown(void) {
|
||||
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
||||
}
|
||||
|
||||
|
||||
|
||||
void testeStackBeschreiben()
|
||||
{
|
||||
//printf("=== Test: push() ===\n");
|
||||
|
||||
StackNode *stack = NULL;
|
||||
int wert1 = 42;
|
||||
stack = push(stack, &wert1);
|
||||
|
||||
int *topValue = (int *)(stack->data);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(topValue);
|
||||
TEST_ASSERT_EQUAL_INT(42, *topValue);
|
||||
|
||||
|
||||
int wert2 = 12;
|
||||
stack = push(stack, &wert2);
|
||||
|
||||
topValue = (int *)(stack->data);
|
||||
int *secondValue = (int *)((stack->next)->data);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(topValue);
|
||||
TEST_ASSERT_EQUAL_INT(12, *topValue);
|
||||
TEST_ASSERT_NOT_NULL(secondValue);
|
||||
TEST_ASSERT_EQUAL_INT(42, *secondValue);
|
||||
|
||||
|
||||
//printf("=== Ende Test: push() ===\n\n");
|
||||
return;
|
||||
}
|
||||
|
||||
void testepop()
|
||||
{
|
||||
//printf("=== Test: pop() ===\n");
|
||||
|
||||
StackNode *stack = NULL;
|
||||
int wert1 = 20;
|
||||
int wert2 = 74;
|
||||
|
||||
stack = push(stack, &wert1);
|
||||
stack = push(stack, &wert2);
|
||||
|
||||
stack = pop(stack);
|
||||
|
||||
int *topValue = (int *)(stack->data);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(topValue);
|
||||
TEST_ASSERT_EQUAL_INT(20, *topValue);
|
||||
|
||||
//printf("=== Ende Test: pop() ===\n\n");
|
||||
}
|
||||
|
||||
void testetop()
|
||||
{
|
||||
//printf("=== Test: top() ===\n");
|
||||
|
||||
StackNode *stack = NULL;
|
||||
int wert1 = 20;
|
||||
int wert2 = 74;
|
||||
|
||||
stack = push(stack, &wert1);
|
||||
stack = push(stack, &wert2);
|
||||
|
||||
int *topValue = top(stack);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(topValue);
|
||||
TEST_ASSERT_EQUAL_INT(74, *topValue);
|
||||
|
||||
//printf("=== Ende Test: top() ===\n\n");
|
||||
}
|
||||
|
||||
void testeclearStack()
|
||||
{
|
||||
//printf("=== Test: clearStack() ===\n");
|
||||
|
||||
StackNode *stack = NULL;
|
||||
int wert1 = 20;
|
||||
int wert2 = 74;
|
||||
|
||||
stack = push(stack, &wert1);
|
||||
stack = push(stack, &wert2);
|
||||
|
||||
clearStack(stack);
|
||||
|
||||
//printf("Test: clearStack() aufgerufen. Speicher freigegeben.\n");
|
||||
//printf("=== Ende Test: clearStack() ===\n\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
|
||||
//printf("...");
|
||||
|
||||
RUN_TEST(testeStackBeschreiben);
|
||||
RUN_TEST(testepop);
|
||||
RUN_TEST(testetop);
|
||||
RUN_TEST(testeclearStack);
|
||||
|
||||
/*testeStackBeschreiben();
|
||||
testepop();
|
||||
testetop();
|
||||
testeclearStack();*/
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
Reference in New Issue
Block a user