#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. */

static StackNode *iteratorStack = NULL;

// 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 == NULL)
    {
        // Neue Node anlegen
        TreeNode *newNode = malloc(sizeof(TreeNode));
        if (!newNode) return NULL;

        newNode->data = malloc(dataSize);
        if(!newNode->data)
        {
            free(newNode);
            return NULL;
        }

        memcpy(newNode->data, data, dataSize);

        newNode->left = NULL;
        newNode->right = NULL;

        if (isDuplicate)
            *isDuplicate = 0;

        return newNode;
    }

    int cmp = compareFct(data, root->data);

    if (cmp == 0)
    {
        if (isDuplicate)
            *isDuplicate = 1;
        return root;  // Einfügen verhindern
    }

    if (cmp < 0)
        root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
    else
        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.
// 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)
{
    if (root != NULL)
    {
        clearStack(iteratorStack);
        iteratorStack = NULL;

        // Root + alle linken Nachfolger pushen
        TreeNode *current = root;
        while (current != NULL)
        {
            iteratorStack = push(iteratorStack, current);
            current = current->left;
        }
    }

    // Wenn Stack leer -> fertig
    if (iteratorStack == NULL)
        return NULL;

    // Top-Node holen
    TreeNode *node = (TreeNode *)top(iteratorStack);
    iteratorStack = pop(iteratorStack);

    // rechten Teilbaum + linke Kette pushen
    TreeNode *right = node->right;
    while (right != NULL)
    {
        iteratorStack = push(iteratorStack, right);
        right = right->left;
    }

    return node->data;
}

// Releases all memory resources (including data copies).
void clearTree(TreeNode *root)
{
    if (root != NULL)
    {
        clearTree(root->left);
        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)
{
    if (root == NULL)
        return 0;

    return 1 + treeSize(root->left) + treeSize(root->right);
}