-
Notifications
You must be signed in to change notification settings - Fork 0
/
16-binary_tree_is_perfect.c
executable file
·72 lines (63 loc) · 1.75 KB
/
16-binary_tree_is_perfect.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
65
66
67
68
69
70
71
72
#include "binary_trees.h"
/**
* binary_tree_is_perfect - A function that checks if binary tree is perfect.
* @tree: A pointer to the root of the tree.
* Return: 1 if binary tree is perfect or 0 in otherwise.
*/
int binary_tree_is_perfect(const binary_tree_t *tree)
{
if (!tree)
return (0);
if ((binary_tree_is_full(tree) == 1) &&
(binary_tree_balance(tree) == 0))
return (1);
return (0);
}
/**
* binary_tree_is_full - A function that checks if a binary tree is full.
* @tree: A pointer to the root node of the tree.
* Return: 1 if tree is full or 0 in otherwise.
*/
int binary_tree_is_full(const binary_tree_t *tree)
{
if (!tree)
return (0);
if (!tree->left && !tree->right)
return (1);
if (tree->left && tree->right)
return (binary_tree_is_full(tree->left) &&
binary_tree_is_full(tree->right));
return (0);
}
/**
* binary_tree_balance - A function that measures the balance factor of a
* binary tree.
* @tree: Pointer to root node to calculate balance factor.
* Return: The balance factor of the tree.
*/
int binary_tree_balance(const binary_tree_t *tree)
{
size_t left_height = 0, right_height = 0;
if (!tree)
return (0);
left_height = binary_tree_height(tree->left);
right_height = binary_tree_height(tree->right);
return (left_height - right_height);
}
/**
* binary_tree_height - gets the height of a binary tree
* @tree: root node to draw height from for tree
* Return: size_t representing height, 0 on failure or NULL.
*/
size_t binary_tree_height(const binary_tree_t *tree)
{
size_t left_size = 0;
size_t right_size = 0;
if (!tree)
return (0);
left_size += 1 + binary_tree_height(tree->left);
right_size += 1 + binary_tree_height(tree->right);
if (left_size > right_size)
return (left_size);
return (right_size);
}