-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101-binary_tree_levelorder.c
More file actions
70 lines (56 loc) · 1.19 KB
/
101-binary_tree_levelorder.c
File metadata and controls
70 lines (56 loc) · 1.19 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
#include "binary_trees.h"
/**
* recursive_height - measures the height of a binary tree
* @tree: tree root
*
* Return: height
*/
size_t recursive_height(const binary_tree_t *tree)
{
size_t left = 0;
size_t right = 0;
if (tree == NULL)
return (0);
left = recursive_height(tree->left);
right = recursive_height(tree->right);
if (left > right)
return (left + 1);
return (right + 1);
}
/**
* print_level - prints nodes at the same level
* @tree: tree root
* @level: level node
* @func: pointer to a function
*
* Return: no return
*/
void print_level(const binary_tree_t *tree, int level, void (*func)(int))
{
if (tree == NULL)
return;
if (level == 1)
func(tree->n);
else if (level > 1)
{
print_level(tree->left, level - 1, func);
print_level(tree->right, level - 1, func);
}
}
/**
* binary_tree_levelorder - prints data in level-order traversal
* @tree: tree root
* @func: pointer to a function
*
* Return: no return
*/
void binary_tree_levelorder(const binary_tree_t *tree, void (*func)(int))
{
size_t height;
size_t i;
if (tree == NULL || func == NULL)
return;
height = recursive_height(tree);
for (i = 1; i <= height; i++)
print_level(tree, i, func);
}