blob: 12c0cde98a489f13e70bb33aed8b78f913389057 (
plain)
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
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_btsorted_insert.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/26 19:10:36 by charles #+# #+# */
/* Updated: 2020/04/26 19:19:24 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft_bt.h"
/*
** \brief Insert a data in a binary search tree so that it stays sorted
** \param tree Tree where data is inserted
** \param data Data to insert
** \param cmp Comparison function
** \return The new root or NULL if a new node couldn't be allocated
*/
t_ftbt *ft_btsorted_insert(t_ftbt *tree, void *data, int (*cmp)(void*, void*))
{
if (tree == NULL)
return (ft_btnew(data));
if (cmp(data, tree->data) < 0)
{
if (ft_btsorted_insert(tree->left, data, cmp) == NULL)
return (NULL);
}
else
{
if (ft_btsorted_insert(tree->right, data, cmp) == NULL)
return (NULL);
}
return (tree);
}
|