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 post-order traversal
Prototype: void binary_tree_postorder(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 8-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 post-order traversal
Prototype: void binary_tree_postorder(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 8-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_postorder(root, &print_num); return (0); } alex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 8-main.c 8-binary_tree_postorder.c 0-binary_tree_node.c -o 8-post alex@/tmp/binary_trees$ ./8-post .-------(098)-------. .--(012)--. .--(402)--. (006) (056) (256) (512) 6 56 12 256 512 402 98 alex@/tmp/binary_trees$