You are on page 1of 4

#include <stdio.

h>
#include <stdlib.h>
#include "bst.h"
/* here are the functions that are implemented within this file but
* are not allowed to be called by other files
* These are helper functions necessary because we now have split
* the struct of a bsttree from the struct of a bstnode.
*/
void do_to_all_nodes(bstnode *node, void (*func)(void*))
{
if(node == NULL)
{
return;
}
do_to_all_nodes(node->lsub, func(void*));
node->item = func(node->item);
do_to_all_nodes(node->rsub, func(void*));
}

void accum_all_nodes_helper(bstnode *node, int (*func)(void*), int modified_sum)


{
if(node == NULL)
{
return;
}
accum_all_nodes(node->lsub, func(void*), modified_sum);
modified_sum += func(node->item);
accum_all_nodes(node->rsub, func(void*), modified_sum);
}
int accum_all_nodes_helper(bstnode *node, int (*func)(void*))
{
int n = 0
accum_all_nodes_helper(node, func(void*), n);
return n;
}

void* find_node(bstnode *node, void *item,


int (*compare)(void*, void*))
{
if(node == NULL)
{
return;
}
if(compare(item, node->item) == 0)
{
return node;
}
else if (compare(item, node->item) == -1)
{
return find_node(node->lsub, item, compare(void*, void*));
}
else if (compare(item, node->item) == 1)
{
return find(node->rsub, item, compare(void*, void*));
}
}

/* CREATION FUNCTIONS */
/* create an empty bst */
bst* create_bst()
{
bst *tree = (bst *)malloc(sizeof(bst));
tree->root->item = NULL;
tree->root->lsub = NULL;
tree->root->rsub = NULL;
}
/* INFORMATIONAL FUNCTIONS */
/* provide some generic funcions that will apply the same
* function to all nodes in a bst
*/
/* do_to_all_nodes -
* apply a function (func) to all nodes in the subtree rooted at (node)
*/
void do_to_all_nodes(bstnode *node, void (*func)(void*))
{
}
/* do_to_all -
* apply a function (func) to all nodes in the tree
*/
void do_to_all(bst *tree, void (*func)(void*))
{
if (tree)
do_to_all_nodes(tree->root, func);
}
/* accum_all_nodes -
* apply a function (func) to all nodes in the subtree rooted at (node)
* and sum up the results.
*/
int accum_all_nodes(bstnode *node, int (*func)(void*))
{
return 0;
}
/* accum_all -
* apply a function (func) to all nodes in the tree and sum up the results.
*/
int accum_all(bst *tree, int (*func)(void*))
{
if (tree)
return accum_all_nodes(tree->root, func);
}
/* find -
* find the item in the subtree rooted at node that is equivalent
* (as described by compare) to particular item (item).
*/
void* find_node(bstnode *node, void* item, int (*compare)(void*, void*))
{
return NULL;
}
void* find(bst *tree, void* item, int (*compare)(void*, void*))
{
if (tree)
return find_node(tree->root, item, compare);
}
/* print_bst -
* print the items in the tree from smallest to largest
* (as described by the compare function used for insertion)
*/
void print_node(bstnode *node, void (*print_item)(void *))
{
if(node == NULL)
{
return;
}
if(node->left == NULL)
{
print_item(node->item);
return;
}
print_node(node->left, print_item);
print_item(node->item);
print_node(node->right, print_item);

}
void print_bst(bst *tree, void (*print_item)(void *))
{
print_node(tree->root, print_item);
}
/* MODIFICATION FUNCTIONS */
/* insertion in sorted order. Return the root of the subtree of the
* modified tree at this point.
*/
bstnode* bstnode_insert(bstnode *node, void *item,
int (*compare)(void *, void *))
{
if(node == NULL)
{
node->item = item;
return node;
}
else if(compare(item, node->item) <= 0)
{
bstnode_insert(node->lsub, item, compare(void *, void *));
}
else if(compare(item, node->item) == 1)
{
bstnode_insert(node->rsub, item, compare(void *, void *));
}
}

void bst_insert(bst *tree, void *item, int (*compare)(void *, void *))


{
if (tree)
tree->root = bstnode_insert(tree->root, item, compare);
}

/* ITERATOR */
/* iterate
* create an iterator that navigates the tree with an inorder
* traversal. If a non-NULL tree is passed in, then the
* iterator resets to the first node. It returns the item in the
* first node (as defined by inorder traversal). If a NULL tree
* is passed in, then return the item in the next node.
*
* Hint: The entire navigation occurs when the non-NULL tree is
* passed in. The results are stored in a data structure, which is
* accessed each time to provide the next one. You may make a
* helper function for this.
*/
void* iterate(bst* tree)
{
return NULL;
}

/* free_bst
* frees all of the nodes in the bst tree
*/
void free_node(bstnode* node)
{
if(node == NULL)
{
return;
}
free_node(node->lsub);
free(node->item);
free_node(node->rsub);
}
void free_bst(bst* tree)
{
free_node(tree->root);
}

You might also like