-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14-binary_tree_balance.c
More file actions
37 lines (31 loc) · 862 Bytes
/
Copy path14-binary_tree_balance.c
File metadata and controls
37 lines (31 loc) · 862 Bytes
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
#include "binary_trees.h"
/**
* binary_tree_height_1 - measures the hight of the tree
* @tree:pointer to the root node of the tree
* Return: 0 if tree is NULL else the hight
*/
int binary_tree_height_1(const binary_tree_t *tree)
{
int h_left, h_right;
if (tree == NULL)
return (0);
h_left = binary_tree_height_1(tree->left);
h_right = binary_tree_height_1(tree->right);
if (h_left >= h_right)
return (h_left + 1);
return (h_right + 1);
}
/**
* binary_tree_balance - measures the balance factor of a binary tree
* @tree: a pointer to the root node of the tree to measure the balance factor
* Return: 0 or balance factor
*/
int binary_tree_balance(const binary_tree_t *tree)
{
int left, right;
if (tree == NULL)
return (0);
left = binary_tree_height_1(tree->left);
right = binary_tree_height_1(tree->right);
return (left - right);
}