99 lines
3.7 KiB
C
99 lines
3.7 KiB
C
#include <string.h>
|
|
#include "stack.h"
|
|
#include "bintree.h"
|
|
|
|
//TODO: binären Suchbaum implementieren
|
|
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv),
|
|
* `clearTree`: gibt den gesamten Baum frei (rekursiv),
|
|
* `treeSize`: zählt die Knoten im Baum (rekursiv),
|
|
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
|
|
|
|
// 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)
|
|
{
|
|
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;
|
|
}
|
|
|
|
|
|
/*
|
|
else{
|
|
//if the element is larger than the current element but not larger than the next one, insert it in the middle
|
|
TreeNode* newnode = malloc(sizeof(TreeNode));
|
|
newnode->data = malloc(dataSize);
|
|
memcpy(newnode->data, data, dataSize);
|
|
newnode->left = root;
|
|
newnode->right = root->right;
|
|
root->right->left = newnode;
|
|
root->right = newnode;
|
|
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.
|
|
// 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.
|
|
void *nextTreeData(TreeNode *root)
|
|
{
|
|
|
|
}
|
|
|
|
// Releases all memory resources (including data copies).
|
|
void clearTree(TreeNode *root)
|
|
{
|
|
|
|
}
|
|
|
|
// Returns the number of entries in the tree given by root.
|
|
unsigned int treeSize(const TreeNode *root)
|
|
{
|
|
|
|
} |