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 measures the depth of a node in a binary tree
*Prototype: size_t binary_tree_depth(const binary_tree_t tree);**
Where tree is a pointer to the node to measure the depth
If tree is NULL, your function must return 0
alex@/tmp/binary_trees$ cat 10-main.c
Write a function that measures the depth of a node in a binary tree
*Prototype: size_t binary_tree_depth(const binary_tree_t tree);** Where tree is a pointer to the node to measure the depth If tree is NULL, your function must return 0 alex@/tmp/binary_trees$ cat 10-main.c
include
include
include "binary_trees.h"
/**
Return: Always 0 (Success) / int main(void) { binary_tree_t root; size_t depth;
root = binary_tree_node(NULL, 98); root->left = binary_tree_node(root, 12); root->right = binary_tree_node(root, 402); binary_tree_insert_right(root->left, 54); binary_tree_insert_right(root, 128); binary_tree_print(root);
depth = binary_tree_depth(root); printf("Depth of %d: %lu\n", root->n, depth); depth = binary_tree_depth(root->right); printf("Depth of %d: %lu\n", root->right->n, depth); depth = binary_tree_depth(root->left->right); printf("Depth of %d: %lu\n", root->left->right->n, depth); return (0); } alex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 10-binary_tree_depth.c 10-main.c 0-binary_tree_node.c 2-binary_tree_insert_right.c -o 10-depth alex@/tmp/binary_trees$ ./10-depth .-------(098)--. (012)--. (128)--. (054) (402) Depth of 98: 0 Depth of 128: 1 Depth of 54: 2 alex@/tmp/binary_trees$