-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path124-sorted_array_to_avl.c
executable file
·50 lines (44 loc) · 1.23 KB
/
124-sorted_array_to_avl.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
#include "binary_trees.h"
/**
* sorted_array_to_avl - builds an AVL tree from an array
* @array: a pointer to the first element of the array to be converted
* @size: number of elements in the array
*
* Return: a pointer to the root node of the created AVL tree
* NULL on failure
*/
avl_t *sorted_array_to_avl(int *array, size_t size)
{
avl_t *tree = NULL;
size_t middle;
if (!array)
return (NULL);
middle = (size - 1) / 2;
tree = binary_tree_node(NULL, array[middle]);
sata_helper(&tree, array, -1, middle);
sata_helper(&tree, array, middle, size);
return (tree);
}
/**
* sata_helper - helper that builds an AVL tree from an array
* @root: double pointer to the root node of the subtree
* @array: a pointer to the first element of the array to be converted
* @lo: lower bound index
* @hi: upper bound index
*/
void sata_helper(avl_t **root, int *array, size_t lo, size_t hi)
{
avl_t *new = NULL;
size_t middle;
if (hi - lo > 1)
{
middle = (hi - lo) / 2 + lo;
new = binary_tree_node(*root, array[middle]);
if (array[middle] > (*root)->n)
(*root)->right = new;
else if (array[middle] < (*root)->n)
(*root)->left = new;
sata_helper(&new, array, lo, middle);
sata_helper(&new, array, middle, hi);
}
}