-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path111-bst_insert.c
49 lines (47 loc) · 912 Bytes
/
111-bst_insert.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
#include "binary_trees.h"
/**
* bst_insert - insert nodes in order to create a BST tree
* @tree: tree to create with type BST
* @value: value of node to insert
* Return: BST tree
*/
bst_t *bst_insert(bst_t **tree, int value)
{
bst_t *new, *temp;
binary_tree_t *aux;
if (tree == NULL)
return (NULL);
if (*tree == NULL)
{
aux = binary_tree_node((binary_tree_t *)(*tree), value);
new = (bst_t *)aux;
*tree = new;
}
else
{
temp = *tree;
if (value < temp->n)
{
if (temp->left)
new = bst_insert(&temp->left, value);
else
{
aux = binary_tree_node((binary_tree_t *)temp, value);
new = temp->left = (bst_t *)aux;
}
}
else if (value > temp->n)
{
if (temp->right)
new = bst_insert(&temp->right, value);
else
{
aux = binary_tree_node((binary_tree_t *)temp, value);
new = temp->right = aux;
}
}
else
return (NULL);
}
return (new);
}