forked from Atountoun/binary_trees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
102-binary_tree_is_complete.c
57 lines (52 loc) · 1.29 KB
/
102-binary_tree_is_complete.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
#include <stdio.h>
#include <stdlib.h>
#include "binary_trees.h"
/**
* binary_tree_size - counts the number of nodes of a binary tree
*
* @tree: pointer to the root node of the tree or subtree
*
* Return: the number of nodes of @tree
*/
size_t binary_tree_size(const binary_tree_t *tree)
{
if (!tree)
return (0);
return (1 + binary_tree_size(tree->left) + binary_tree_size(tree->right));
}
/**
* check_complete - checks if a binary tree is complete
*
* @node: pointer to a node of the tree
* @index: position of the node if the tree will be stored in an array
* @size: the number of nodes of the tree
*
* Return: 1 if the tree is complete, else 0
*/
int check_complete(const binary_tree_t *node, size_t index, size_t size)
{
if (!node)
return (1);
if (index >= size)
return (0);
return (
check_complete(node->left, 2 * index + 1, size)
&& check_complete(node->right, 2 * index + 2, size)
);
}
/**
* binary_tree_is_complete - checks if a binary tree is complete
*
* @tree: pointer to the root node of the tree to check
*
* Return: 1 if complete, 0 otherwise or if @tree is NULL
*/
int binary_tree_is_complete(const binary_tree_t *tree)
{
size_t size, index;
if (!tree)
return (0);
size = binary_tree_size(tree);
index = 0;
return (check_complete(tree, index, size));
}