-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path102-binary_tree_is_complete.c
executable file
·136 lines (116 loc) · 2.62 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
132
133
134
135
136
#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 - Function creates a new levelorder_queue_t node
* @node: Binary tree node for the new node to contain
*
* Return: On errors - NULL,
* Otherwise, a pointer to the new node
*/
levelorder_queue_t *create_node(binary_tree_t *node)
{
levelorder_queue_t *curr;
curr = malloc(sizeof(levelorder_queue_t));
if (curr == NULL)
return (NULL);
curr->node = node;
curr->next = NULL;
return (curr);
}
/**
* free_queue - Function frees a levelorder_queue_t queue
* @head: Pointer to the head of the queue
*/
void free_queue(levelorder_queue_t *head)
{
levelorder_queue_t *temp;
while (head != NULL)
{
temp = head->next;
free(head);
head = temp;
}
}
/**
* push - Function pushes a node to the back of a levelorder_queue_t queue
* @node: The binary tree node to print and push
* @head: Double pointer to the head of the queue
* @tail: 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 *curr;
curr = create_node(node);
if (curr == NULL)
{
free_queue(head);
exit(1);
}
(*tail)->next = curr;
*tail = curr;
}
/**
* pop - Function pops the head of a levelorder_queue_t queue
* @head: Double pointer to the head of the queue
*/
void pop(levelorder_queue_t **head)
{
levelorder_queue_t *temp;
temp = (*head)->next;
free(*head);
*head = temp;
}
/**
* binary_tree_is_complete - Function checks if a binary tree is complete
* @tree: Pointer to the root node of the tree to traverse
*
* Return: If the tree is NULL or not complete - (0),
* Otherwise - (1)
*
* Description: On 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 flg = 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 (flg == 1)
{
free_queue(head);
return (0);
}
push(head->node->left, head, &tail);
}
else
flg = 1;
if (head->node->right != NULL)
{
if (flg == 1)
{
free_queue(head);
return (0);
}
push(head->node->right, head, &tail);
}
else
flg = 1;
pop(&head);
}
return (1);
}