-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path102-binary_tree_is_complete.c
113 lines (107 loc) · 1.91 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "binary_trees.h"
/**
* new_node - Function that creates a new_node in a linked_list
* @node: Type pointer of node to be created
* Return: the node created
*/
link_t *new_node(binary_tree_t *node)
{
link_t *new;
new = malloc(sizeof(link_t));
if (new == NULL)
{
return (NULL);
}
new->node = node;
new->next = NULL;
return (new);
}
/**
* free_q - Function that free the nodes at the linked list
* @head: Node of the linked_list
*/
void free_q(link_t *head)
{
link_t *temp_node;
while (head)
{
temp_node = head->next;
free(head);
head = temp_node;
}
}
/**
* _push - Function that pushes a node into the stack
* @node: Type pointer of node of the tree
* @head: Type head node of in the stack
* @tail: Type tail node of in the stack
*/
void _push(binary_tree_t *node, link_t *head, link_t **tail)
{
link_t *new;
new = new_node(node);
if (new == NULL)
{
free_q(head);
exit(1);
}
(*tail)->next = new;
*tail = new;
}
/**
* _pop - Function that pops a node into the stack
* @head: Type head node of in the stack
*/
void _pop(link_t **head)
{
link_t *temp_node;
temp_node = (*head)->next;
free(*head);
*head = temp_node;
}
/**
* binary_tree_is_complete - Function that checks if a binary tree is complete
* @tree: Type pointer of node of the tree
* Return: 1 if is complete 0 if it is not
*/
int binary_tree_is_complete(const binary_tree_t *tree)
{
link_t *head, *tail;
int flag = 0;
if (tree == NULL)
{
return (0);
}
head = tail = new_node((binary_tree_t *)tree);
if (head == NULL)
{
exit(1);
}
while (head != NULL)
{
if (head->node->left != NULL)
{
if (flag == 1)
{
free_q(head);
return (0);
}
_push(head->node->left, head, &tail);
}
else
flag = 1;
if (head->node->right != NULL)
{
if (flag == 1)
{
free_q(head);
return (0);
}
_push(head->node->right, head, &tail);
}
else
flag = 1;
_pop(&head);
}
return (1);
}