-
Notifications
You must be signed in to change notification settings - Fork 0
/
102-binary_tree_is_complete.c
131 lines (116 loc) · 2.61 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "binary_trees.h"
levelorder_queue_t *create_node(binary_tree_t *node);
void free_queue(levelorder_queue_t *head);
void push(binary_tree_t *node, levelorder_queue_t *head,
levelorder_queue_t **tail);
void pop(levelorder_queue_t **head);
int binary_tree_is_complete(const binary_tree_t *tree);
/**
* create_node - Creates a new levelorder_queue_t node.
* @node: The binary tree node for the new node to contain.
*
* Return: If an error occurs, NULL.
* Otherwise, a pointer to the new node.
*/
levelorder_queue_t *create_node(binary_tree_t *node)
{
levelorder_queue_t *new;
new = malloc(sizeof(levelorder_queue_t));
if (new == NULL)
return (NULL);
new->node = node;
new->next = NULL;
return (new);
}
/**
* free_queue - Frees a levelorder_queue_t queue.
* @head: A pointer to the head of the queue.
*/
void free_queue(levelorder_queue_t *head)
{
levelorder_queue_t *tmp;
while (head != NULL)
{
tmp = head->next;
free(head);
head = tmp;
}
}
/**
* push - Pushes a node to the back of a levelorder_queue_t queue.
* @node: The binary tree node to print and push.
* @head: A double pointer to the head of the queue.
* @tail: A double pointer to the tail of the queue.
*
* Description: Upon malloc failure, exits with a status code of 1.
*/
void push(binary_tree_t *node, levelorder_queue_t *head,
levelorder_queue_t **tail)
{
levelorder_queue_t *new;
new = create_node(node);
if (new == NULL)
{
free_queue(head);
exit(1);
}
(*tail)->next = new;
*tail = new;
}
/**
* pop - Pops the head of a levelorder_queue_t queue.
* @head: A double pointer to the head of the queue.
*/
void pop(levelorder_queue_t **head)
{
levelorder_queue_t *tmp;
tmp = (*head)->next;
free(*head);
*head = tmp;
}
/**
* binary_tree_is_complete - Checks if a binary tree is complete.
* @tree: A pointer to the root node of the tree to traverse.
*
* Return: If the tree is NULL or not complete, 0.
* Otherwise, 1.
*
* Description: Upon malloc failure, exits with a status code of 1.
*/
int binary_tree_is_complete(const binary_tree_t *tree)
{
levelorder_queue_t *head, *tail;
unsigned char flag = 0;
if (tree == NULL)
return (0);
head = tail = create_node((binary_tree_t *)tree);
if (head == NULL)
exit(1);
while (head != NULL)
{
if (head->node->left != NULL)
{
if (flag == 1)
{
free_queue(head);
return (0);
}
push(head->node->left, head, &tail);
}
else
flag = 1;
if (head->node->right != NULL)
{
if (flag == 1)
{
free_queue(head);
return (0);
}
push(head->node->right, head, &tail);
}
else
flag = 1;
pop(&head);
}
return (1);
}