forked from freudenreichan/info2Praktikum-DobleSpiel
Add toArray to bintree.c
This commit is contained in:
3 files changed
+29
-1
No files matched your search
@@ -1,3 +1,4 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "stack.h"
|
||||
#include "bintree.h"
|
||||
@@ -113,3 +114,19 @@ unsigned int treeSize(const TreeNode *const root)
|
||||
|
||||
return treeSize(root->left) + treeSize(root->right) + 1;
|
||||
}
|
||||
|
||||
void *toArray(TreeNode *const root, const size_t dataSize) {
|
||||
if (root == NULL) return NULL;
|
||||
|
||||
const unsigned int elementCount = treeSize(root);
|
||||
unsigned char *array = calloc(elementCount, dataSize);
|
||||
if (array == NULL) return NULL;
|
||||
|
||||
unsigned int index = 0;
|
||||
for (void *element = nextTreeData(root); element != NULL && index < elementCount; element = nextTreeData(NULL)) {
|
||||
memcpy(array + index * dataSize, element, dataSize);
|
||||
index++;
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
Reference in new issue
Block a user