forked from Atountoun/binary_trees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
111-bst_insert.c
56 lines (51 loc) · 1.04 KB
/
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
50
51
52
53
54
55
56
#include <stdlib.h>
#include <stdio.h>
#include "binary_trees.h"
/**
* bst_insert - inserts a value in a BST
*
* if tree is NULL, the created node must become the root
* if the value is already present in the tree, it must be ignored
*
* @tree: double pointer to the root node of the BST to insert the value
* @value: value to store in the node to be inserted
*
* Return: pointer to the created node or NULL on failure
*/
bst_t *bst_insert(bst_t **tree, int value)
{
bst_t *node, *temp, *prev;
node = (bst_t *) malloc(sizeof(bst_t));
if (!node || !tree)
return (NULL);
node->left = NULL;
node->right = NULL;
node->n = value;
if (!(*tree))
{
node->parent = NULL;
*tree = node;
}
else
{
temp = *tree;
while (temp)
{
prev = temp;
if (value < temp->n)
temp = temp->left;
else if (value > temp->n)
temp = temp->right;
else
return (NULL);
}
node->parent = prev;
if (value < prev->n)
prev->left = node;
else if (value > prev->n)
prev->right = node;
else
free(node);
}
return (node);
}