From a800ec9bac1b3414c537258c40949ba3acbdc1b1 Mon Sep 17 00:00:00 2001 From: regis37 Date: Fri, 28 Nov 2025 23:36:44 +0100 Subject: [PATCH] addToTree is done --- .idea/.gitignore | 8 + .idea/editor.xml | 580 ++++++++++++++++++++++++++++ .idea/info2Praktikum-DobleSpiel.iml | 2 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + bintree.c | 24 ++ numbers.c | 3 +- numbers.h | 1 - 8 files changed, 630 insertions(+), 2 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/editor.xml create mode 100644 .idea/info2Praktikum-DobleSpiel.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml 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..1f0ef49 --- /dev/null +++ b/.idea/editor.xml @@ -0,0 +1,580 @@ + + + + + \ No newline at end of file diff --git a/.idea/info2Praktikum-DobleSpiel.iml b/.idea/info2Praktikum-DobleSpiel.iml new file mode 100644 index 0000000..4c94235 --- /dev/null +++ b/.idea/info2Praktikum-DobleSpiel.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..1f4f1dc --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ 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..ccff54b 100644 --- a/bintree.c +++ b/bintree.c @@ -12,7 +12,31 @@ // 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 (!root) { + TreeNode *newNode = (TreeNode *)malloc(sizeof(TreeNode)); + if (!newNode) return NULL; + newNode->data = malloc(dataSize); + if (!newNode->data) { + free(newNode); + return NULL; + } + memcpy(newNode->data, data, dataSize); + newNode->left = NULL; + newNode->right = NULL; + if (isDuplicate) *isDuplicate = 0; + return newNode; + } + int cmp = compareFct(data, root->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 { // cmp == 0 → Duplikat + if (isDuplicate) *isDuplicate = 1; + // Duplikate ignorieren, wenn isDuplicate != NULL + } + 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. diff --git a/numbers.c b/numbers.c index f59d9a2..179dcf9 100644 --- a/numbers.c +++ b/numbers.c @@ -16,7 +16,8 @@ // creating random numbers. unsigned int *createNumbers(unsigned int len) { - + srand(0); + int r = rand(); } // Returns only the only number in numbers which is present twice. Returns zero on errors. diff --git a/numbers.h b/numbers.h index 2315581..c905bf6 100644 --- a/numbers.h +++ b/numbers.h @@ -8,5 +8,4 @@ unsigned int *createNumbers(unsigned int len); // Returns only the only number in numbers which is present twice. Returns zero on errors. unsigned int getDuplicate(const unsigned int *numbers, unsigned int len); - #endif \ No newline at end of file