generated from freudenreichan/info2Praktikum-DobleSpiel
nextTreeData + Test
This commit is contained in:
parent
da3bd18637
commit
f4cf06ff53
@ -16,7 +16,7 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!root){
|
if (!root){ //Neuen Node
|
||||||
TreeNode *new = malloc(sizeof(TreeNode));
|
TreeNode *new = malloc(sizeof(TreeNode));
|
||||||
if (!new){
|
if (!new){
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -27,7 +27,7 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(new->data, data, dataSize);
|
memcpy(new->data, data, dataSize); //data übertragen
|
||||||
new->left = new->right = NULL;
|
new->left = new->right = NULL;
|
||||||
|
|
||||||
if (isDuplicate){
|
if (isDuplicate){
|
||||||
@ -58,7 +58,39 @@ 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)
|
||||||
{
|
{
|
||||||
|
static StackNode *stack = NULL;
|
||||||
|
static int initialized = 0;
|
||||||
|
|
||||||
|
if (root != NULL) { // Initialisierung: Stack leeren und mit root + linke elemente befüllen
|
||||||
|
while (stack != NULL) {
|
||||||
|
stack = pop(stack);
|
||||||
|
}
|
||||||
|
while (root != NULL) {
|
||||||
|
stack = push(stack, root);
|
||||||
|
root = root->left;
|
||||||
|
}
|
||||||
|
initialized = 1;
|
||||||
|
} else if (!initialized) { // Noch nicht initialisiert und kein root übergeben
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stack == NULL) { //Ende
|
||||||
|
initialized = 0;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
TreeNode *topNode = (TreeNode *)top(stack);
|
||||||
|
stack = pop(stack);
|
||||||
|
|
||||||
|
if (topNode->right != NULL) {
|
||||||
|
TreeNode *r = topNode->right;
|
||||||
|
while (r != NULL) {
|
||||||
|
stack = push(stack, r);
|
||||||
|
r = r->left;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return topNode->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Releases all memory resources (including data copies).
|
// Releases all memory resources (including data copies).
|
||||||
|
|||||||
Binary file not shown.
@ -51,6 +51,28 @@ static void testAddToTreeNoData(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void testNextTreeDataInorderTraversal(){
|
||||||
|
TreeNode *root = NULL;
|
||||||
|
int values[] = {5, 3, 8, 1, 4};
|
||||||
|
int expectedInorder[] = {1, 3, 4, 5, 8};
|
||||||
|
|
||||||
|
for(int i = 0; i < 5; i++) {
|
||||||
|
root = addToTree(root, &values[i], sizeof(int), compareInt, NULL); //Tree befüllen
|
||||||
|
}
|
||||||
|
|
||||||
|
void *data = nextTreeData(root);
|
||||||
|
TEST_ASSERT_NOT_NULL(data);
|
||||||
|
|
||||||
|
for(int i = 0; i < 5; i++){
|
||||||
|
TEST_ASSERT_NOT_NULL(data);
|
||||||
|
TEST_ASSERT_EQUAL_INT(expectedInorder[i], *(int*)data);
|
||||||
|
data = nextTreeData(NULL); // Nächstes Element
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_NULL(data); // Nach letzten Element muss NULL kommen
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void setUp(void){
|
void setUp(void){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user