-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1-binary_tree_insert_left.c
More file actions
executable file
·48 lines (44 loc) · 1.29 KB
/
Copy path1-binary_tree_insert_left.c
File metadata and controls
executable file
·48 lines (44 loc) · 1.29 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
37
38
39
40
41
42
43
44
45
46
47
48
#include "binary_trees.h"
#include <stdlib.h>
/**
* binary_tree_node_1 - creates a binary tree node
* @parent: parnt of the node to create
* @value: value to store in new node
*
* Return: pointer to the new node
*/
binary_tree_t *binary_tree_node_1(binary_tree_t *parent, int value)
{
binary_tree_t *new_node;
new_node = malloc(sizeof(binary_tree_t));
if (new_node == NULL)
return (NULL);
new_node->parent = parent;
new_node->n = value;
new_node->left = new_node->right = NULL;
return (new_node);
}
/**
* 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: value to store in the new node
*
* Description: If parent already has a left-child, the new node must take its
* place, and the old left-child must be set as the left-child of the new node.
*
* Return: pointer to the created node, or NULL on failure
*/
binary_tree_t *binary_tree_insert_left(binary_tree_t *parent, int value)
{
binary_tree_t *left_child;
if (parent == NULL)
return (NULL);
left_child = binary_tree_node_1(parent, value);
if (left_child == NULL)
return (NULL);
left_child->left = parent->left;
if (left_child->left != NULL)
left_child->left->parent = left_child;
parent->left = left_child;
return (left_child);
}