Änderung bintree.c
This commit is contained in:
parent
2e93327ba5
commit
a309d2dfc2
87
bintree.c
87
bintree.c
@ -1,4 +1,4 @@
|
|||||||
#include <string.h>
|
#include <string.h> // für memcpy
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
#include "bintree.h"
|
#include "bintree.h"
|
||||||
|
|
||||||
@ -11,8 +11,46 @@
|
|||||||
// 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).
|
||||||
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)
|
||||||
{
|
|
||||||
|
|
||||||
|
{
|
||||||
|
// 1. Leerer Baum → neuen Knoten anlegen
|
||||||
|
if (root == NULL) // Baum muss leer sein
|
||||||
|
{
|
||||||
|
TreeNode *node = malloc(sizeof(TreeNode)); // Speicherreservierung
|
||||||
|
if (!node) return NULL; //Abbruch wenn kein Speicher verfügbar
|
||||||
|
|
||||||
|
node->data = malloc(dataSize); // Speicher für Daten reservieren
|
||||||
|
if (!node->data) { // Bei Fehler -> freigeben des Knotens und abbrechen
|
||||||
|
free(node);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
memcpy(node->data, data, dataSize); // Daten kopieren (nicht Zeiger)
|
||||||
|
|
||||||
|
node->left = node->right = NULL; // Neuer Knoten hat keine Kinder
|
||||||
|
|
||||||
|
if (isDuplicate) *isDuplicate = 0; // Rückmeldung (kein Duplikat)
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Vergleich, um Einfügerichtung zu bestimmen
|
||||||
|
int cmp = compareFct(data, root->data);
|
||||||
|
|
||||||
|
if (cmp < 0) // kleiner nach links weitergeben (rekursiv)
|
||||||
|
{
|
||||||
|
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||||||
|
}
|
||||||
|
else if (cmp > 0) // größer nach rechts weitergeben
|
||||||
|
{
|
||||||
|
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Duplikat
|
||||||
|
if (isDuplicate) *isDuplicate = 1;
|
||||||
|
// wird nicht eingefügt (kein Änderung am Baum)
|
||||||
|
}
|
||||||
|
|
||||||
|
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.
|
// 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.
|
||||||
@ -20,17 +58,62 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
|
|||||||
// push the top node and push all its left nodes.
|
// push the top node and push all its left nodes.
|
||||||
void *nextTreeData(TreeNode *root)
|
void *nextTreeData(TreeNode *root)
|
||||||
{
|
{
|
||||||
|
static StackNode *iterStack = NULL; // behält den Wert zw. Funktionsaufrufen (speichert welche Knoten noch besucht werden müssen)
|
||||||
|
|
||||||
|
// Wird ein neuer Baum übergeben?
|
||||||
|
if (root != NULL)
|
||||||
|
{
|
||||||
|
// alten Stack löschen
|
||||||
|
clearStack(iterStack);
|
||||||
|
iterStack = NULL;
|
||||||
|
|
||||||
|
// Root + alle linken Knoten auf Stack pushen
|
||||||
|
TreeNode *cur = root;
|
||||||
|
while (cur != NULL)
|
||||||
|
{
|
||||||
|
iterStack = push(iterStack, cur);
|
||||||
|
cur = cur->left;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kein Baum mehr → Besuchen aller Knoten beendet
|
||||||
|
if (iterStack == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
// Top-Knoten (als nächstes Reihe)
|
||||||
|
TreeNode *node = (TreeNode *)top(iterStack);
|
||||||
|
iterStack = pop(iterStack); //entfernen des Knoten
|
||||||
|
|
||||||
|
// Rechtsknoten
|
||||||
|
TreeNode *right = node->right; //nach Besuch bei rechten Knoten in rechten Teilbaum
|
||||||
|
while (right != NULL)
|
||||||
|
{
|
||||||
|
iterStack = push(iterStack, right);
|
||||||
|
right = right->left; // von dort ganz nach links und alles pushen
|
||||||
|
}
|
||||||
|
|
||||||
|
return node->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Releases all memory resources (including data copies).
|
// Releases all memory resources (including data copies).
|
||||||
void clearTree(TreeNode *root)
|
void clearTree(TreeNode *root)
|
||||||
{
|
{
|
||||||
|
if (root == NULL) //Abbruchbedingung
|
||||||
|
return;
|
||||||
|
|
||||||
|
clearTree(root->left); // Links und rechts löschen
|
||||||
|
clearTree(root->right);
|
||||||
|
|
||||||
|
free(root->data); // Daten freigeben
|
||||||
|
free(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of entries in the tree given by root.
|
// Returns the number of entries in the tree given by root.
|
||||||
unsigned int treeSize(const TreeNode *root)
|
unsigned int treeSize(const TreeNode *root)
|
||||||
{
|
{
|
||||||
|
if (root == NULL) // Leerer Baum
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return 1 + treeSize(root->left) + treeSize(root->right);
|
||||||
|
// Größe = 1 + Größe links + Größe rechts
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user