#include #include "stack.h" #include "bintree.h" //TODO: binären Suchbaum implementieren /* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv), Done * `clearTree`: gibt den gesamten Baum frei (rekursiv), Done * `treeSize`: zählt die Knoten im Baum (rekursiv), * `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */ static TreeNode *root = NULL; TreeNode *addToTree (TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate); void *nextTreeData (TreeNode *root); void clearTree (TreeNode *root); unsigned int treeSize (const TreeNode *root); // self declared functions TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate); void clearTreeRec (TreeNode *currentNode); void clearNode (TreeNode *node); void treeSizeRec (const TreeNode *currentNode, unsigned int *nodeCount); // 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). // returned Value is new root TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate) { // create a node TreeNode *newNode; newNode = calloc(1, sizeof(TreeNode)); newNode->data = calloc(1, dataSize); newNode->left = NULL; newNode->right = NULL; memcpy(newNode->data, data, dataSize); return addToTreeRec(root, newNode, compareFct, isDuplicate); } TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate) { if (currentNode == NULL) { isDuplicate = 0; return newNode; } else if (compareFct(newNode->data, currentNode->data) < 0) { currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate); } else if (compareFct(newNode->data, currentNode->data) > 0) { currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate); } else { //duplicate if (isDuplicate == NULL) { currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate); } else { *isDuplicate = 1; } } return currentNode; } // 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. // Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element, // push the top node and push all its left nodes. // Needs stack!! void *nextTreeData(TreeNode *root) { } // Releases all memory resources (including data copies). void clearTree(TreeNode *root) { clearTreeRec(root); } void clearTreeRec(TreeNode *currentNode) { if (currentNode != NULL) { clearTree(currentNode->left); clearNode(currentNode); clearTree(currentNode->right); } } void clearNode(TreeNode *node) { free(node->data); node->data = NULL; node->left = NULL; node->right = NULL; free(node); node = NULL; } // Returns the number of entries in the tree given by root. unsigned int treeSize(const TreeNode *root) { unsigned int amountOfNodes = 0; treeSizeRec(root, &amountOfNodes); return amountOfNodes; } void treeSizeRec(const TreeNode *currentNode, unsigned int *nodeCount) { if (currentNode != NULL) { treeSizeRec(currentNode->left, nodeCount); *nodeCount++; treeSizeRec(currentNode->right, nodeCount); } }