-
Notifications
You must be signed in to change notification settings - Fork 0
/
14-binary_tree_balance.c
45 lines (38 loc) · 1.15 KB
/
14-binary_tree_balance.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "binary_trees.h"
/**
* binary_tree_balance - This function measures the balance factor
* of a binary tree
* @tree: A pointer to the root node of the tree
* Return: The balance factor, otherwise 0 if tree is NULL
*/
int binary_tree_balance(const binary_tree_t *tree)
{
int balance = 0, left_depth = 0, right_depth = 0;
if (!tree)
return (0);
if (tree->left)
left_depth = 1 + binary_tree_height(tree->left);
if (tree->right)
right_depth = 1 + binary_tree_height(tree->right);
balance = left_depth - right_depth;
return (balance);
}
/**
* binary_tree_height - This function measures the height of a binary tree
* @tree: A pointer to the root node
* Return: Height of the tree, or 0 if tree is NULL
*/
size_t binary_tree_height(const binary_tree_t *tree)
{
size_t left_depth = 0, right_depth = 0;
if (!tree || (!tree->left && !tree->right))
return (0);
/* compute the depth of each subtree if they exist */
left_depth = binary_tree_height(tree->left);
right_depth = binary_tree_height(tree->right);
/* compare the values of each depth */
if (left_depth >= right_depth)
return (left_depth + 1);
else
return (right_depth + 1);
}