-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path134-heap_to_sorted_array.c
64 lines (53 loc) · 1.07 KB
/
134-heap_to_sorted_array.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
#include "binary_trees.h"
#include <stdbool.h>
/**
* bit_size - Measures the height of a binary tree
*
* @tree: Pointer to the node to measures the size
*
* Return: The size of the tree starting at @node
*/
size_t bit_size(const binary_tree_t *tree)
{
size_t left_size = 0;
size_t right_size = 0;
if (tree == NULL)
return (0);
if (tree->left)
left_size = 1 + bit_size(tree->left);
if (tree->right)
right_size = 1 + bit_size(tree->right);
return (left_size + right_size);
}
/**
* heap_to_sorted_array - converts a Binary Max Heap
* to a sorted array of integers
*
* @heap: a pointer to the root node of the heap to convert
* @size: an address to store the size of the array
*
* Return: sorted array
*/
int *heap_to_sorted_array(heap_t *heap, size_t *size)
{
int i;
int *array = NULL;
if (!heap || !size)
return (NULL);
*size = bit_size(heap) + 1;
array = malloc(sizeof(int) * (*size));
if (!array)
return (NULL);
for (i = 0; i < (int)*size; i++)
{
if (heap)
{
array[i] = heap_extract(&heap);
}
else
{
break;
}
}
return (array);
}