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 balance factor of a binary tree
*Prototype: int binary_tree_balance(const binary_tree_t tree);**
Where tree is a pointer to the root node of the tree to measure the balance factor
If tree is NULL, return 0
alex@/tmp/binary_trees$ cat 14-main.c
include
include
include "binary_trees.h"
/**
main - Entry point
Return: Always 0 (Success)
/
int main(void)
{
binary_tree_t root;
int balance;
Write a function that measures the balance factor of a binary tree
*Prototype: int binary_tree_balance(const binary_tree_t tree);** Where tree is a pointer to the root node of the tree to measure the balance factor If tree is NULL, return 0 alex@/tmp/binary_trees$ cat 14-main.c
include
include
include "binary_trees.h"
/**
Return: Always 0 (Success) / int main(void) { binary_tree_t root; int balance;
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_insert_left(root, 45); binary_tree_insert_right(root->left, 50); binary_tree_insert_left(root->left->left, 10); binary_tree_insert_left(root->left->left->left, 8); binary_tree_print(root);
balance = binary_tree_balance(root); printf("Balance of %d: %+d\n", root->n, balance); balance = binary_tree_balance(root->right); printf("Balance of %d: %+d\n", root->right->n, balance); balance = binary_tree_balance(root->left->left->right); printf("Balance of %d: %+d\n", root->left->left->right->n, balance); return (0); } alex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 14-binary_tree_balance.c 14-main.c 0-binary_tree_node.c 2-binary_tree_insert_right.c 1-binary_tree_insert_left.c -o 14-balance alex@/tmp/binary_trees$ ./14-balance .-------(098)--. .-------(045)--. (128)--. .--(012)--. (050) (402) .--(010) (054) (008) Balance of 98: +2 Balance of 128: -1 Balance of 54: +0 alex@/tmp/binary_trees$