-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path111-bst_insert.c
More file actions
59 lines (52 loc) · 942 Bytes
/
111-bst_insert.c
File metadata and controls
59 lines (52 loc) · 942 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
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "binary_trees.h"
/**
* bst_in - checks if node is inserted
* @tree: tree root
* @value: node value
*
* Return: pointer to the new node
*/
bst_t *bst_in(bst_t **tree, int value)
{
if (value < (*tree)->n)
{
if ((*tree)->left == NULL)
{
(*tree)->left = binary_tree_node(*tree, value);
return ((*tree)->left);
}
else
{
return (bst_in(&((*tree)->left), value));
}
}
if (value > (*tree)->n)
{
if ((*tree)->right == NULL)
{
(*tree)->right = binary_tree_node(*tree, value);
return ((*tree)->right);
}
else
{
return (bst_in(&((*tree)->right), value));
}
}
return (NULL);
}
/**
* bst_insert - inserts a value in a Binary Search Tree
* @tree: tree root
* @value: node value
*
* Return: pointer to the new node
*/
bst_t *bst_insert(bst_t **tree, int value)
{
if (*tree == NULL)
{
*tree = binary_tree_node(NULL, value);
return (*tree);
}
return (bst_in(tree, value));
}