-
Notifications
You must be signed in to change notification settings - Fork 0
/
14-binary_tree_balance.c
57 lines (45 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
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdlib.h>
#include <stddef.h>
#include "binary_trees.h"
/**
* binary_tree_height - measures the height of a binary tree.
*
* @tree: a pointer to the root node of the tree.
* Return: height of tree, O if tree is NULL.
*/
size_t binary_tree_height(const binary_tree_t *tree)
{
size_t left_height = 0;
size_t right_height = 0;
if (tree == NULL)
return (0);
/* Only make recursive calls if left or right subtree exists*/
if (tree->left)
left_height = 1 + binary_tree_height(tree->left);
else
left_height = 1;
if (tree->right)
right_height = 1 + binary_tree_height(tree->right);
else
right_height = 1;
if (left_height > right_height)
return (left_height);
else
return (right_height);
}
/**
* binary_tree_balance - measures the balance factor of a binary tree.
*
* @tree: a pointer to the root node of the tree.
* Return: balance factor, 0 when tree is NULL.
*/
int binary_tree_balance(const binary_tree_t *tree)
{
int left_height = 0;
int right_height = 0;
if (tree == NULL)
return (0);
left_height = binary_tree_height(tree->left);
right_height = binary_tree_height(tree->right);
return (left_height - right_height);
}