-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtree_insert.c
executable file
·41 lines (38 loc) · 1.37 KB
/
btree_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* btree_insert.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akharrou <akharrou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/21 21:05:04 by akharrou #+# #+# */
/* Updated: 2019/03/04 13:17:20 by akharrou ### ########.fr */
/* */
/* ************************************************************************** */
#include "../Includes/btree.h"
void btree_insert(t_btree **root, void *item,
int (*cmpf)(void *, void *))
{
t_btree *probe;
if (!root || !(*root))
{
if (root)
(*root) = btree_newnode(item);
return ;
}
probe = (*root);
if ((*cmpf)(item, probe->item) < 0)
{
if (probe->left)
btree_insert(&(probe->left), item, cmpf);
else
probe->left = btree_newnode(item);
}
else
{
if (probe->right)
btree_insert(&(probe->right), item, cmpf);
else
probe->right = btree_newnode(item);
}
}