A collection of C programs for learning and implementing binary tree operations, including traversal, insertion, deletion, and validation. Ideal for understanding fundamental data structures and algorithms in C programming.
Write a function that goes through a binary tree using pre-order traversal
Prototype: void binary_tree_preorder(const binary_tree_t tree, void (func)(int));
Where tree is a pointer to the root node of the tree to traverse
And func is a pointer to a function to call for each node. The value in the node must be passed as a parameter to this function.
If tree or func is NULL, do nothing
alex@/tmp/binary_trees$ cat 6-main.c
include
include
include "binary_trees.h"
/**
print_num - Prints a number
@n: Number to be printed
*/
void print_num(int n)
{
printf("%d\n", n);
}
/**
main - Entry point
Return: Always 0 (Success)
/
int main(void)
{
binary_tree_t root;
Write a function that goes through a binary tree using pre-order traversal
Prototype: void binary_tree_preorder(const binary_tree_t tree, void (func)(int)); Where tree is a pointer to the root node of the tree to traverse And func is a pointer to a function to call for each node. The value in the node must be passed as a parameter to this function. If tree or func is NULL, do nothing alex@/tmp/binary_trees$ cat 6-main.c
include
include
include "binary_trees.h"
/**
/**
Return: Always 0 (Success) / int main(void) { binary_tree_t root;
root = binary_tree_node(NULL, 98); root->left = binary_tree_node(root, 12); root->right = binary_tree_node(root, 402); root->left->left = binary_tree_node(root->left, 6); root->left->right = binary_tree_node(root->left, 56); root->right->left = binary_tree_node(root->right, 256); root->right->right = binary_tree_node(root->right, 512);
binary_tree_print(root); binary_tree_preorder(root, &print_num); return (0); } alex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 6-main.c 6-binary_tree_preorder.c 0-binary_tree_node.c -o 6-pre alex@/tmp/binary_trees$ ./6-pre .-------(098)-------. .--(012)--. .--(402)--. (006) (056) (256) (512) 98 12 6 56 402 256 512 alex@/tmp/binary_trees$