-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path102-binary_tree_is_complete.c
More file actions
64 lines (52 loc) · 1.16 KB
/
102-binary_tree_is_complete.c
File metadata and controls
64 lines (52 loc) · 1.16 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
61
62
63
64
#include "binary_trees.h"
/**
* count_nodes - Counts ths inside a tree
* @root: node
*
* Return: Number odes
*/
int count_nodes(binary_tree_t *root)
{
if (!root)
return (0);
return (1 + count_nodes(root->left) + count_nodes(root->right));
}
/**
* is_complete - Checks if a tree is complete
* @root: Pointer to tree's root
* @index: Index of the node been evaluated
* @n: number of trees nod
*
* Return: 1 if the tree is a heap, 0 otherwise
*/
int is_complete(binary_tree_t *root, int index, int n)
{
if (!root)
return (0);
if (index >= n)
return (0);
if (!root->left && !root->right)
return (1);
if (root->right && !root->left)
return (0);
if (root->left && !root->right)
return (is_complete(root->left, index * 2 + 1, n));
return (is_complete(root->left, index * 2 + 1, n) &&
is_complete(root->right, index * 2 + 2, n));
}
/**
* binary_tree_is_complete - check for bt complete
* @tree: Pointer to root
*
* Return: 1 if
*/
int binary_tree_is_complete(const binary_tree_t *tree)
{
int nod;
binary_tree_t *root;
if (!tree)
return (0);
root = (binary_tree_t *)tree;
nod = count_nodes(root);
return (is_complete(root, 0, nod));
}