Skip to content

Commit

Permalink
Added min() and max() functions to tree
Browse files Browse the repository at this point in the history
Added a min() function and a max() function to the tree. They are
simple (using recursion) and usefull so why not have them?
  • Loading branch information
StefanBossbaly committed May 23, 2012
1 parent 42e4e6b commit fcb2285
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,31 @@ void tree_treverse_inorder(struct tree *tree) {
tree_treverse_inorder_helper(tree->root);
printf("\n");
}

static void *tree_min_helper(struct node *node) {
if (node->left != NULL)
return tree_min_helper(node->left);
else
return node->data;
}

void *tree_min(struct tree *tree) {
if (tree->root != NULL)
return tree_min_helper(tree->root);
else
return NULL;
}

static void *tree_max_helper(struct node *node) {
if (node->right != NULL)
return tree_max_helper(node->right);
else
return node->data;
}

void *tree_max(struct tree *tree) {
if (tree->root != NULL)
return tree_max_helper(tree->root);
else
return NULL;
}
3 changes: 3 additions & 0 deletions tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ void tree_insert(struct tree *tree, void *data, size_t size);
int tree_contains(struct tree *tree, void *data);
void tree_dealloc(struct tree *tree);
void tree_treverse_inorder(struct tree *tree);
void *tree_min(struct tree *tree);
void *tree_max(struct tree *tree);


#endif /* TREE_H_ */

0 comments on commit fcb2285

Please sign in to comment.