-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path101-binary_tree_levelorder.c
55 lines (41 loc) · 998 Bytes
/
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
#include "binary_trees.h"
/**
* binary_tree_size - measures the size of a binary tree
* @tree: tree
* Return: size of tree
*/
size_t binary_tree_size(const binary_tree_t *tree)
{
size_t left, right;
if (tree == NULL)
return (0);
left = binary_tree_size(tree->left);
right = binary_tree_size(tree->right);
return (left + right + 1);
}
/**
* binary_tree_levelorder - a binary tree using level-order traversal
* @tree: tree
* @func: function pointer
*/
void binary_tree_levelorder(const binary_tree_t *tree, void (*func)(int))
{
binary_tree_t **queue, *curr;
size_t front = 0, rear = 0;
if (tree == NULL || func == NULL)
return;
queue = malloc(sizeof(binary_tree_t *) * binary_tree_size(tree));
if (queue == NULL)
return;
queue[rear++] = (binary_tree_t *)tree;
while (front < rear)
{
curr = queue[front++];
func(curr->n);
if (curr->left != NULL)
queue[rear++] = curr->left;
if (curr->right != NULL)
queue[rear++] = curr->right;
}
free(queue);
}