Repository navigation
Expand file tree
/
Copy path101-binary_tree_levelorder.c
More file actions
119 lines (104 loc) · 2.15 KB
/
Copy path101-binary_tree_levelorder.c
File metadata and controls
119 lines (104 loc) · 2.15 KB
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
#include "binary_trees.h"
/**
* enqueue - adds a new node to the queue
* @queue: a pointer to the head pf the list
* @data: the data to be inserted into the node
*
* Return: Nothing.
*/
void enqueue(queue_t *queue, const binary_tree_t *data)
{
queue_node_t *newNode = (queue_node_t *)malloc(sizeof(queue_node_t));
if (newNode == NULL)
exit(EXIT_FAILURE);
newNode->data = (binary_tree_t *) data;
newNode->next = NULL;
if (!queue->front)
{
queue->front = newNode;
queue->rear = newNode;
}
else
{
queue->rear->next = newNode;
queue->rear = newNode;
}
}
/**
* dequeue - removed the top element of the stack
* @queue: a pointer to the head of the list
*
* Return: the node that was stored.
*/
binary_tree_t *dequeue(queue_t *queue)
{
binary_tree_t *data;
queue_node_t *temp;
if (!queue->front)
return (NULL);
data = queue->front->data;
temp = queue->front;
queue->front = queue->front->next;
free(temp);
return (data);
}
/**
* freeAll - frees all allocated memory in cases of failure and exits
* @queue: a pointer to the head of the stack
*
* Return: Nothing.
*/
void freeAll(queue_t *queue)
{
queue_node_t *current, *temp;
current = queue->front;
while (current != NULL)
{
temp = current;
current = current->next;
free(temp);
}
free(queue);
}
/**
* createQueue - This function creates a new queue
*
* Return: The queue.
*/
queue_t *createQueue(void)
{
queue_t *queue = (queue_t *)malloc(sizeof(queue_t));
if (queue == NULL)
exit(EXIT_FAILURE);
queue->front = NULL;
queue->rear = NULL;
return (queue);
}
/**
* binary_tree_levelorder - This function goes through the binary tree
* using level-order traversal
* @tree: Pointer to the root node of the tree to traverse
* @func: a pointer to a function to call for each node.
*
* Return: Nothing.
*/
void binary_tree_levelorder(const binary_tree_t *tree, void (*func)(int))
{
queue_t *queue = createQueue();
binary_tree_t *node;
if (tree && func)
{
enqueue(queue, tree);
while (queue->front)
{
node = dequeue(queue);
if (node)
{
func(node->n);
enqueue(queue, node->left);
enqueue(queue, node->right);
}
}
}
freeAll(queue);
}