-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path14-binary_tree_balance.c
More file actions
60 lines (45 loc) · 1 KB
/
14-binary_tree_balance.c
File metadata and controls
60 lines (45 loc) · 1 KB
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
58
59
60
#include "binary_trees.h"
/**
* binary_tree_height - measures the height of a binary tree
* @tree: pointer to the root node of the tree
* Return: the size or 0 if tree is null
*/
size_t binary_tree_height(const binary_tree_t *tree)
{
int counter;
if (tree == NULL)
return (0);
counter = tree_height(tree);
return ((size_t)counter);
}
/**
* tree_height - count node tree height
* @node: node to measure
* Return: height
*/
int tree_height(const binary_tree_t *node)
{
int lDepth, rDepth;
if (node == NULL)
return (0);
lDepth = tree_height(node->left);
rDepth = tree_height(node->right);
if (lDepth > rDepth)
return (lDepth + 1);
else
return (rDepth + 1);
}
/**
* binary_tree_balance - A function that calculates the balance of a tree
* @tree: The node in discussion
* Return: The balance
*/
int binary_tree_balance(const binary_tree_t *tree)
{
int hr, hl;
if (tree == NULL)
return (0);
hl = binary_tree_height(tree->left);
hr = binary_tree_height(tree->right);
return (hl - hr);
}