-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-binary_tree_insert_left.c
More file actions
38 lines (35 loc) · 961 Bytes
/
1-binary_tree_insert_left.c
File metadata and controls
38 lines (35 loc) · 961 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
#include "binary_trees.h"
/**
* binary_tree_insert_left - inserts a node as the left-child of another node
* @parent: pointer to the node to insert the left-child in
* @value: the value to store in the new node
* Return: pointer to the created node, or NULL on failure or if parent is NULL
*/
#include "binary_trees.h"
/**
* binary_tree_insert_left - add a node in the left of the parent
* if it exists it move down one level and add the new node first
* @parent: parent of the specified node
* @value: value of the node
* Return: NULL if it fails or the new node
*/
binary_tree_t *binary_tree_insert_left(binary_tree_t *parent, int value)
{
binary_tree_t *new_node;
if (parent == NULL)
{
return (NULL);
}
new_node = binary_tree_node(parent, value);
if (new_node == NULL)
{
return (NULL);
}
if (parent->left != NULL)
{
new_node->left = parent->left;
parent->left->parent = new_node;
}
parent->left = new_node;
return (new_node);
}