Repository navigation
Expand file tree
/
Copy pathtree_insert.c
More file actions
45 lines (42 loc) · 1.52 KB
/
Copy pathtree_insert.c
File metadata and controls
45 lines (42 loc) · 1.52 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tree_insert.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mihykim <mihykim@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/08 01:17:22 by mihykim #+# #+# */
/* Updated: 2020/04/09 00:22:50 by mihykim ### ########.fr */
/* */
/* ************************************************************************** */
#include "tree.h"
int tree_insert(t_tree *tree, void *data)
{
t_node *curr;
t_node *new;
t_node *parent;
if (tree == NULL || tree->cmp == NULL || (new = create_elem(data)) == NULL)
return (FAIL);
curr = tree->root;
parent = NULL;
while (curr)
{
if (tree->cmp(curr->data, data) == EQUAL)
return (FAIL);
parent = curr;
if (tree->cmp(curr->data, data) > 0)
curr = curr->left;
else
curr = curr->right;
}
if (parent == NULL)
tree->root = new;
else if (tree->cmp(parent->data, data) > 0)
parent->left = new;
else if (tree->cmp(parent->data, data) < 0)
parent->right = new;
else
tree->root = new;
tree->size++;
return (SUCCESS);
}