-
Notifications
You must be signed in to change notification settings - Fork 0
/
14-binary_tree_balance.c~
66 lines (53 loc) · 1.49 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
58
59
60
61
62
63
64
#include "binary_trees.h"
/**
* binary_tree_balance - measures the balance factor of a tree
* @tree: tree to measure the balance factor
* Description:
* The balance factor of a binary tree is the difference in
* heights of its two subtrees (hR - hL).
* The balance factor (bf) of a height balanced binary tree may take on one
* of the values -1, 0, +1. An AVL node is
* "leftheavy" when bf = 1,
* "equalheight" when bf = 0, and
* "rightheavy" when bf = +1
*
* Return: tree balance factor(bf) (int)
* else 0 when tree is NULL
*/
int binary_tree_balance(const binary_tree_t *tree)
{
int hR, hL, bf;
if (!tree)
return (0);
hR = tree->left ? (int)binary_tree_height(tree->left) : -1;
hL = tree->right ? (int)binary_tree_height(tree->right) : -1;
bf = hR - hL;
return (bf);
}
/**
* binary_tree_height- Traverses left,right,root subtrees returns the max height
* @tree: tree root node to be traversed
* Return: Integer value representing height of tree
*/
size_t binary_tree_height(const binary_tree_t *tree)
{
size_t left_height, right_height;
left_height = 0;
right_height = 0;
/* node should not be NULL*/
if (!tree)
{
return (0);
}
else
{
/* traverse left*/
left_height = tree->left ? 1 + binary_tree_height(tree->left) : 0;
/* traverse right*/
right_height = tree->right ? 1 + binary_tree_height(tree->right) : 0;
if (left_height >= right_height)
return (left_height);
else
return (right_height);
}
}