diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/editor.xml b/.idea/editor.xml new file mode 100644 index 0000000..226ca24 --- /dev/null +++ b/.idea/editor.xml @@ -0,0 +1,580 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..bedf918 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/bintree.c b/bintree.c index 5cf82a9..a16bf2d 100644 --- a/bintree.c +++ b/bintree.c @@ -12,7 +12,52 @@ // 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) { + if(data!= NULL && dataSize > 0) + { + if(root == NULL) //Abbruchbedingung: Keine Wurzel vorhanden, deshalb fügen wir hier einen neuen Knote ein + { + TreeNode *newNode = (TreeNode *)malloc(sizeof(TreeNode)); + if(newNode == NULL) + { + return NULL; + } + newNode->data = malloc(dataSize); + if(newNode->data == NULL) + { + free(newNode); + return NULL; + } + memcpy(newNode->data, data, dataSize); + newNode->left = NULL; + newNode->right = NULL; + if(isDuplicate!= NULL) + { + *isDuplicate = 1; + } + + return newNode; + } + int cmp = compareFct(root->data, &data); + if(cmp > 0) + { + root->left = addToTree(root->left, &data, dataSize, compareFct, &isDuplicate); + } + else if(cmp < 0){ + root->right = addToTree(root->right, &data, dataSize, compareFct, &isDuplicate); + } + else + { + if (isDuplicate) { + *isDuplicate = 1; + } + else { + // isDuplicate == NULL → trotzdem ein Duplikat einfügen (z.B. rechts) + root->right = addToTree(root->right, data, dataSize, compareFct, NULL); + } + } + } + return NULL; } // 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.