-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path112-array_to_bst.c
More file actions
executable file
·46 lines (41 loc) · 1022 Bytes
/
Copy path112-array_to_bst.c
File metadata and controls
executable file
·46 lines (41 loc) · 1022 Bytes
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
#include "binary_trees.h"
/**
* array_to_bst - builds a binary search tree from array of ints
* @array: pointer to first element of array
* @size: size of int array
*
* Return: pointer to the root node of bst or NULL if failed
*/
bst_t *array_to_bst(int *array, size_t size)
{
bst_t *tree = NULL;
size_t i;
if (array == NULL || size == 0)
return (NULL);
for (i = 0; i < size; i++)
{
if (bst_insert(&tree, array[i]) == NULL)
{
fprintf(stderr, "Error: Failed to insert %d\n", array[i]);
return (NULL);
}
}
return (tree);
}
/**
* bst_search - finds location of where the value should be inserted
* @tree: tree to sort in binary search order
* @value: value to find where should be inserted
*
* Return: pointer to location to insert value at
*/
bst_t *bst_search(const bst_t *tree, int value)
{
if (tree == NULL)
return (NULL);
if (tree->n == value)
return ((bst_t *)tree);
if (value < tree->n)
return (bst_search(tree->left, value));
return (bst_search(tree->right, value));
}