-
Notifications
You must be signed in to change notification settings - Fork 0
/
101-binary_tree_levelorder.c
122 lines (107 loc) · 2.67 KB
/
101-binary_tree_levelorder.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
#include "binary_trees.h"
levelorder_queue_t *create_node(binary_tree_t *node);
void free_queue(levelorder_queue_t *head);
void pint_push(binary_tree_t *node, levelorder_queue_t *head,
levelorder_queue_t **tail, void (*func)(int));
void pop(levelorder_queue_t **head);
void binary_tree_levelorder(const binary_tree_t *tree, void (*func)(int));
/**
* 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;
}
}
/**
* pint_push - Runs a function on a given binary tree node and
* pushes its children into 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.
* @func: A pointer to the function to call on @node.
*
* Description: Upon malloc failure, exits with a status code of 1.
*/
void pint_push(binary_tree_t *node, levelorder_queue_t *head,
levelorder_queue_t **tail, void (*func)(int))
{
levelorder_queue_t *new;
func(node->n);
if (node->left != NULL)
{
new = create_node(node->left);
if (new == NULL)
{
free_queue(head);
exit(1);
}
(*tail)->next = new;
*tail = new;
}
if (node->right != NULL)
{
new = create_node(node->right);
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_levelorder - Traverses a binary tree using
* level-order traversal.
* @tree: A pointer to the root node of the tree to traverse.
* @func: A pointer to a function to call for each node.
*/
void binary_tree_levelorder(const binary_tree_t *tree, void (*func)(int))
{
levelorder_queue_t *head, *tail;
if (tree == NULL || func == NULL)
return;
head = tail = create_node((binary_tree_t *)tree);
if (head == NULL)
return;
while (head != NULL)
{
pint_push(head->node, head, &tail, func);
pop(&head);
}
}