-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path111-clone.c
More file actions
37 lines (33 loc) · 1.05 KB
/
Copy path111-clone.c
File metadata and controls
37 lines (33 loc) · 1.05 KB
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
#include "binary_trees.h"
/**
* bst_insert_helper - a function that inserts a value in a binary search tree
* @tree: a double pointer to the roo node of the BST to insert the value
* @value: value to store in the node to be inserted
* @parent: parent node
* Return: pointer to the created node otherwise NULL
*/
bst_t *bst_insert_helper(bst_t **tree, bst_t *parent, int value)
{
if (!tree)
return (NULL);
if (*tree == NULL)
{
*tree = binary_tree_node(parent, value);
return (*tree);
}
if (value < (*tree)->n)
(*tree)->left = bst_insert_helper(&((*tree)->left), *tree, value);
else if (value > (*tree)->n)
(*tree)->right = bst_insert_helper(&((*tree)->right), *tree, value);
return (*tree);
}
/**
* bst_insert - a function that inserts a value in a binary search tree
* @tree: a double pointer to the roo node of the BST to insert the value
* @value: value to store in the node to be inserted
* Return: pointer to the created node otherwise NULL
*/
bst_t *bst_insert(bst_t **tree, int value)
{
return (bst_insert_helper(tree, NULL, value));
}