Binaerbaum angefangen
This commit is contained in:
parent
0d1d9cf126
commit
30ec4efd57
8
.idea/Info2-Pr-Doble-Spiel.iml
generated
8
.idea/Info2-Pr-Doble-Spiel.iml
generated
@ -1,8 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="CPP_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
<module classpath="CIDR" type="CPP_MODULE" version="4" />
|
||||
79
bintree.c
79
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;
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user