diff --git a/.idea/Info2-Pr-Doble-Spiel.iml b/.idea/Info2-Pr-Doble-Spiel.iml
index bc2cd87..4c94235 100644
--- a/.idea/Info2-Pr-Doble-Spiel.iml
+++ b/.idea/Info2-Pr-Doble-Spiel.iml
@@ -1,8 +1,2 @@
-
-
-
-
-
-
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/bintree.c b/bintree.c
index 5cf82a9..56601c2 100644
--- a/bintree.c
+++ b/bintree.c
@@ -8,11 +8,65 @@
* `treeSize`: zählt die Knoten im Baum (rekursiv),
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
+
+ TreeNode* createNode(const void *data, size_t dataSize) {
+ TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode));
+ if (newNode == NULL) {
+ // Fehlerbehandlung: Speicher konnte nicht alloziiert werden
+ return NULL;
+ }
+
+ // Speicher für die Daten allozieren und kopieren
+ newNode->data = malloc(dataSize);
+ if (newNode->data == NULL) {
+ free(newNode); // newNode freigeben, wenn Datenallokation fehlschlägt
+ return NULL;
+ }
+ memcpy(newNode->data, data, dataSize);
+
+ newNode->left = NULL;
+ newNode->right = NULL;
+ return newNode;
+ }
+
+
// 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 *newNode = createNode(data,dataSize);
+ if (root == NULL)
+ { if (isDuplicate!=NULL) {
+ *isDuplicate = 0;
+ }
+ return newNode;
+ }
+ if (compareFct(root->data, newNode->data)==0)
+ {
+ if (isDuplicate==NULL) {
+ free(newNode);
+ root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
+ }
+ else
+ {
+ free(newNode->data);
+ free(newNode);
+ *isDuplicate=1;
+ return root;
+ }
+ }
+ else if (compareFct(root->data, newNode->data)>0)
+ {
+ free(newNode);
+ root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
+ }
+ else if (compareFct(root->data, newNode -> data)<0)
+ {
+ free(newNode);
+ root->right= addToTree(root->right, data, dataSize, compareFct, isDuplicate);
+ }
+ return root;
}
// 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.
@@ -26,11 +80,34 @@ void *nextTreeData(TreeNode *root)
// Releases all memory resources (including data copies).
void clearTree(TreeNode *root)
{
-
+ if (root != NULL) {
+ if (root->left != NULL) {
+ clearTree(root->left);
+ }
+ if (root->right != NULL) {
+ 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 size = 0;
+ if (root == NULL) {
+ return 0;
+ }
+
+ if (root->left != NULL)
+ {
+ size += treeSize(root->left);
+ }
+ if (root->right != NULL)
+ {
+ size += treeSize(root->right);
+ }
+ return size+1;
}
\ No newline at end of file