bintreeV1

This commit is contained in:
Tobias Busch 2025-12-12 22:06:36 +01:00
parent a13c7acf22
commit f4aa2c7f57
3 changed files with 56 additions and 3 deletions

View File

@ -9,11 +9,48 @@
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */ * `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
static TreeNode* createNode(const void* data, size_t dataSize) {
TreeNode* newNode = calloc(1, sizeof(TreeNode));
if(!newNode) return NULL;
newNode->data = malloc(dataSize);
if(!newNode->data) {
free(newNode);
return 0;
}
newNode->data = data;
return newNode;
}
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates // 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). // if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
//1. Fall: isDuplicate = NULL -> accepts
//2. Fall: other -> ignores duplicates and
TreeNode *addToTree(TreeNode* root, const void* data, size_t dataSize, CompareFctType compareFct, int* isDuplicate) TreeNode *addToTree(TreeNode* root, const void* data, size_t dataSize, CompareFctType compareFct, int* isDuplicate)
{ {
TreeNode newNode = createNode(data); if(isDuplicate != NULL) {
*isDuplicate = 0;
}
if(!root) { //wurzerl == 0;
TreeNode* newNode = createNode(data, dataSize);
return newNode;
}
//wenn duplicate NUll -> doppelt hinzufügen
//
if(compareFct(data, root->data) < 0){
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
} else if (compareFct(data, root->data) > 0) {
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
} else if (compareFct(data, root->data) == 0) {
if(isDuplicate != NULL) {
*isDuplicate = 1;
} else {
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
}
}
} }
// 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. // 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.

View File

@ -3,7 +3,14 @@
#include <stdlib.h> #include <stdlib.h>
typedef int (*CompareFctType)(const void *arg1, const void *arg2); typedef int (*CompareFctType)(const void *arg1, const void *arg2); //ganz zahlen
void foo(void* ptr) {
unsigned int* casted_pointer = ptr;
unsigned int** a = &casted_pointer; //pointer pointer
unsigned int* b = casted_pointer; //pointer
unsigned int c = *casted_pointer; //wert
}
typedef struct node typedef struct node
{ {

View File

@ -5,6 +5,15 @@
#include "numbers.h" #include "numbers.h"
#include "bintree.h" #include "bintree.h"
CompareFctType compareFct(const void *arg1, const void *arg2) {
const unsigned int *entry1 = arg1;
const unsigned int *entry2 = arg2;
int result = *entry2 - *entry1;
return result;
}
//TODO: getDuplicate und createNumbers implementieren //TODO: getDuplicate und createNumbers implementieren
/* * * Erzeugen eines Arrays mit der vom Nutzer eingegebenen Anzahl an Zufallszahlen. /* * * Erzeugen eines Arrays mit der vom Nutzer eingegebenen Anzahl an Zufallszahlen.
* Sicherstellen, dass beim Befüllen keine Duplikate entstehen. * Sicherstellen, dass beim Befüllen keine Duplikate entstehen.