Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6e2987c818 | ||
|
|
f24ee674ef | ||
|
|
28eb9ad481 |
No files matched your search
+55
-2
@@ -12,7 +12,55 @@
|
|||||||
// 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).
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
if(root){
|
||||||
|
int cmp_val = compareFct(data, root->data);
|
||||||
|
if(cmp_val > 0){
|
||||||
|
if(root->right){
|
||||||
|
//if element is larger current node mount on right node
|
||||||
|
addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
//element is larger than the current element but there is no next element, insert
|
||||||
|
TreeNode* newnode = malloc(sizeof(TreeNode));
|
||||||
|
newnode->data = malloc(dataSize);
|
||||||
|
memcpy(newnode->data, data, dataSize);
|
||||||
|
newnode->left = NULL;
|
||||||
|
newnode->right = NULL;
|
||||||
|
root->right = newnode;
|
||||||
|
if(isDuplicate) *isDuplicate = 0;
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
else /*if(cmp_val < 0)*/{
|
||||||
|
if(!cmp_val){
|
||||||
|
//found duplicate
|
||||||
|
if(isDuplicate){
|
||||||
|
*isDuplicate = 1;
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(root->left) {
|
||||||
|
//if element is smaller than current node mount on the left node
|
||||||
|
addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
//element is not larger than the current element but there is no left element, insert
|
||||||
|
TreeNode* newnode = malloc(sizeof(TreeNode));
|
||||||
|
newnode->data = malloc(dataSize);
|
||||||
|
memcpy(newnode->data, data, dataSize);
|
||||||
|
newnode->left = NULL;
|
||||||
|
newnode->right = NULL;
|
||||||
|
root->left = newnode;
|
||||||
|
if(isDuplicate) *isDuplicate = 0;
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TreeNode* newnode = malloc(sizeof(TreeNode));
|
||||||
|
newnode->data = malloc(dataSize);
|
||||||
|
memcpy(newnode->data, data, dataSize);
|
||||||
|
newnode->left = NULL;
|
||||||
|
newnode->right = NULL;
|
||||||
|
if(isDuplicate) *isDuplicate = 0;
|
||||||
|
return newnode;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
@@ -26,7 +74,12 @@ void *nextTreeData(TreeNode *root)
|
|||||||
// Releases all memory resources (including data copies).
|
// Releases all memory resources (including data copies).
|
||||||
void clearTree(TreeNode *root)
|
void clearTree(TreeNode *root)
|
||||||
{
|
{
|
||||||
|
if(root){
|
||||||
|
clearTree(root->right);
|
||||||
|
clearTree(root->left);
|
||||||
|
free(root->data);
|
||||||
|
free(root);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of entries in the tree given by root.
|
// Returns the number of entries in the tree given by root.
|
||||||
|
|||||||
@@ -16,86 +16,11 @@
|
|||||||
// creating random numbers.
|
// creating random numbers.
|
||||||
unsigned int *createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
if (len < 2)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int *numbers = malloc(len * sizeof(unsigned int));
|
|
||||||
|
|
||||||
if (numbers == NULL)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
unsigned int value;
|
|
||||||
int duplicate;
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
duplicate = 0;
|
|
||||||
value = (rand() % (2 * len)) + 1;
|
|
||||||
|
|
||||||
for (unsigned int j = 0; j < i; j++)
|
|
||||||
{
|
|
||||||
if (numbers[j] == value)
|
|
||||||
{
|
|
||||||
duplicate = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
while (duplicate);
|
|
||||||
|
|
||||||
numbers[i] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int source = rand() % len;
|
|
||||||
unsigned int target;
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
target = rand() % len;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (target == source);
|
|
||||||
|
|
||||||
numbers[target] = numbers[source];
|
|
||||||
|
|
||||||
return numbers;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
// 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)
|
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
||||||
{
|
{
|
||||||
if (numbers == NULL || len < 2)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int *copy = malloc(len * sizeof(unsigned int));
|
|
||||||
|
|
||||||
if (copy == NULL)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(copy, numbers, len * sizeof(unsigned int));
|
|
||||||
qsort(copy, len, sizeof(unsigned int), compare);
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < len - 1; i++)
|
|
||||||
{
|
|
||||||
if (copy[i] == copy[i + 1])
|
|
||||||
{
|
|
||||||
unsigned int result = copy[i];
|
|
||||||
free(copy);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
free(copy);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
//
|
|
||||||
// Created by z0055vcx on 11.11.2026.
|
|
||||||
//
|
|
||||||
#include "numbers.h"
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include "bintree.h"
|
|
||||||
|
|
||||||
Reference in new issue
Block a user