btree stuff
This commit is contained in:
parent
ef60398100
commit
dd2e4b8dde
16 changed files with 127 additions and 29 deletions
|
|
@ -18,6 +18,7 @@ t_btree *btree_create_node(void const *item, size_t content_size);
|
||||||
void btree_insert_data(
|
void btree_insert_data(
|
||||||
t_btree **root,
|
t_btree **root,
|
||||||
void *item,
|
void *item,
|
||||||
|
size_t content_size,
|
||||||
int (*cmpf)(void *, void *));
|
int (*cmpf)(void *, void *));
|
||||||
|
|
||||||
void *btree_search_item(t_btree *root,
|
void *btree_search_item(t_btree *root,
|
||||||
|
|
@ -27,5 +28,6 @@ int btree_level_count(t_btree *root);
|
||||||
void btree_apply_prefix(t_btree *root, void (*applyf)(void *));
|
void btree_apply_prefix(t_btree *root, void (*applyf)(void *));
|
||||||
void btree_apply_infix(t_btree *root, void (*applyf)(void *));
|
void btree_apply_infix(t_btree *root, void (*applyf)(void *));
|
||||||
void btree_apply_suffix(t_btree *root, void (*applyf)(void *));
|
void btree_apply_suffix(t_btree *root, void (*applyf)(void *));
|
||||||
|
void btree_print(t_btree* tree, void (*printer)(t_btree *tree));
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */
|
/* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/11/11 17:47:12 by jhalford ### ########.fr */
|
/* Updated: 2016/11/14 16:31:04 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -114,6 +114,7 @@ size_t ft_ilen_base(int n, int base);
|
||||||
size_t ft_uilen(unsigned int n);
|
size_t ft_uilen(unsigned int n);
|
||||||
size_t ft_lllen(long long n);
|
size_t ft_lllen(long long n);
|
||||||
size_t ft_lllen_base(long long n, int base);
|
size_t ft_lllen_base(long long n, int base);
|
||||||
|
int ft_addrcmp(void *a, void *b);
|
||||||
|
|
||||||
char **ft_sstradd(char **list, char *new);
|
char **ft_sstradd(char **list, char *new);
|
||||||
void ft_sstrsort(char **list, int size, int (*cmp)());
|
void ft_sstrsort(char **list, int size, int (*cmp)());
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */
|
/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/08/18 21:10:05 by jhalford ### ########.fr */
|
/* Updated: 2016/11/14 11:58:47 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */
|
/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/08/18 21:11:19 by jhalford ### ########.fr */
|
/* Updated: 2016/11/14 16:08:23 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */
|
/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/08/19 23:32:10 by jhalford ### ########.fr */
|
/* Updated: 2016/11/14 16:11:49 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -14,14 +14,13 @@
|
||||||
|
|
||||||
t_btree *btree_create_node(void const *item, size_t content_size)
|
t_btree *btree_create_node(void const *item, size_t content_size)
|
||||||
{
|
{
|
||||||
t_btree *node;
|
t_btree *new;
|
||||||
|
|
||||||
if (!(node = (t_btree *)malloc(sizeof(t_btree))))
|
if (!(new = (t_btree *)malloc(sizeof(t_btree))))
|
||||||
return (NULL);
|
return (NULL);
|
||||||
node->left = 0;
|
new->left = 0;
|
||||||
node->right = 0;
|
new->right = 0;
|
||||||
return (node);
|
if (!item)
|
||||||
if (!content)
|
|
||||||
{
|
{
|
||||||
new->content_size = 0;
|
new->content_size = 0;
|
||||||
new->item = NULL;
|
new->item = NULL;
|
||||||
|
|
@ -32,4 +31,5 @@ t_btree *btree_create_node(void const *item, size_t content_size)
|
||||||
new->item = ft_memalloc(content_size + 1);
|
new->item = ft_memalloc(content_size + 1);
|
||||||
ft_memcpy(new->item, item, content_size);
|
ft_memcpy(new->item, item, content_size);
|
||||||
}
|
}
|
||||||
|
return (new);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */
|
/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/08/19 14:12:59 by jhalford ### ########.fr */
|
/* Updated: 2016/11/14 16:12:47 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -15,28 +15,29 @@
|
||||||
void btree_insert_data(
|
void btree_insert_data(
|
||||||
t_btree **root,
|
t_btree **root,
|
||||||
void *item,
|
void *item,
|
||||||
|
size_t content_size,
|
||||||
int (*cmpf)(void *, void *))
|
int (*cmpf)(void *, void *))
|
||||||
{
|
{
|
||||||
t_btree *node;
|
t_btree *node;
|
||||||
|
|
||||||
if (!*root)
|
if (!*root)
|
||||||
{
|
{
|
||||||
*root = btree_create_node(item);
|
*root = btree_create_node(item, content_size);
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
node = *root;
|
node = *root;
|
||||||
if ((*cmpf)(item, node->item) < 0)
|
if ((*cmpf)(item, node->item) < 0)
|
||||||
{
|
{
|
||||||
if (node->left)
|
if (node->left)
|
||||||
btree_insert_data(&node->left, item, cmpf);
|
btree_insert_data(&node->left, item, content_size, cmpf);
|
||||||
else
|
else
|
||||||
node->left = btree_create_node(item);
|
node->left = btree_create_node(item, content_size);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (node->right)
|
if (node->right)
|
||||||
btree_insert_data(&node->right, item, cmpf);
|
btree_insert_data(&node->right, item, content_size, cmpf);
|
||||||
else
|
else
|
||||||
node->right = btree_create_node(item);
|
node->right = btree_create_node(item, content_size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
41
libft/src/btree/btree_print.c
Normal file
41
libft/src/btree/btree_print.c
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* btree_print.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2016/11/14 18:06:24 by jhalford #+# #+# */
|
||||||
|
/* Updated: 2016/11/14 18:26:32 by jhalford ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "btree.h"
|
||||||
|
|
||||||
|
static void print_offset(int offset)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < offset; ++i)
|
||||||
|
{
|
||||||
|
ft_putstr(" ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void btree_print(t_btree* tree, void (*printer)(t_btree *tree))
|
||||||
|
{
|
||||||
|
static int offset = 0;
|
||||||
|
|
||||||
|
print_offset(offset);
|
||||||
|
|
||||||
|
if (tree == NULL)
|
||||||
|
{
|
||||||
|
ft_putendl("-");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
(*printer)(tree);
|
||||||
|
|
||||||
|
offset += 3;
|
||||||
|
btree_print(tree->right, printer);
|
||||||
|
btree_print(tree->left, printer);
|
||||||
|
offset -= 3;
|
||||||
|
}
|
||||||
|
|
@ -1,11 +1,41 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_dlstdel.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2016/11/14 17:55:40 by jhalford #+# #+# */
|
||||||
|
/* Updated: 2016/11/14 17:57:46 by jhalford ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
|
static void ft_dlstdelback(t_dlist **alst, void (*del)(void *, size_t))
|
||||||
|
{
|
||||||
|
if (alst && *alst && del)
|
||||||
|
{
|
||||||
|
ft_dlstdelback(&(*alst)->prev, del);
|
||||||
|
ft_dlstdelone(alst, del);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ft_dlstdelfront(t_dlist **alst, void (*del)(void *, size_t))
|
||||||
|
{
|
||||||
|
if (alst && *alst && del)
|
||||||
|
{
|
||||||
|
ft_dlstdelfront(&(*alst)->next, del);
|
||||||
|
ft_dlstdelone(alst, del);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ft_dlstdel(t_dlist **alst, void (*del)(void *, size_t))
|
void ft_dlstdel(t_dlist **alst, void (*del)(void *, size_t))
|
||||||
{
|
{
|
||||||
if (alst && *alst)
|
if (alst && *alst && del)
|
||||||
{
|
{
|
||||||
ft_dlstdel(&(*alst)->next, del);
|
ft_dlstdelback(&(*alst)->prev, del);
|
||||||
ft_dlstdel(&(*alst)->prev, del);
|
ft_dlstdelfront(&(*alst)->next, del);
|
||||||
}
|
|
||||||
ft_dlstdelone(alst, del);
|
ft_dlstdelone(alst, del);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/07 13:27:13 by jhalford #+# #+# */
|
/* Created: 2016/11/07 13:27:13 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/11/07 13:27:13 by jhalford ### ########.fr */
|
/* Updated: 2016/11/14 17:52:58 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/07 13:27:29 by jhalford #+# #+# */
|
/* Created: 2016/11/07 13:27:29 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/11/07 13:31:08 by jhalford ### ########.fr */
|
/* Updated: 2016/11/14 16:13:24 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -20,7 +20,7 @@ char *ft_dlsttostr(t_dlist *list)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
while (list->prev)
|
while (list->prev)
|
||||||
list = list->prev;
|
list = list->prev;
|
||||||
str = (char *)ft_strnew(sizeof(char) * (ft_dlst_size(list) + 2));
|
str = (char *)ft_strnew(sizeof(char) * (ft_dlstsize(list) + 2));
|
||||||
while (list)
|
while (list)
|
||||||
{
|
{
|
||||||
ft_strcat(str, (char *)list->content);
|
ft_strcat(str, (char *)list->content);
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/04 11:09:12 by jhalford #+# #+# */
|
/* Created: 2016/11/04 11:09:12 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/11/08 15:00:24 by jhalford ### ########.fr */
|
/* Updated: 2016/11/14 16:55:27 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/03 14:57:15 by jhalford #+# #+# */
|
/* Created: 2016/11/03 14:57:15 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/11/08 13:45:13 by jhalford ### ########.fr */
|
/* Updated: 2016/11/14 15:50:00 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
18
libft/src/math/ft_addrcmp.c
Normal file
18
libft/src/math/ft_addrcmp.c
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_addrcmp.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2016/11/14 15:59:10 by jhalford #+# #+# */
|
||||||
|
/* Updated: 2016/11/14 15:59:39 by jhalford ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
int ft_addrcmp(void *a, void *b)
|
||||||
|
{
|
||||||
|
return (a - b);
|
||||||
|
}
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/03 18:03:58 by jhalford #+# #+# */
|
/* Created: 2016/11/03 18:03:58 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/11/10 12:15:23 by jhalford ### ########.fr */
|
/* Updated: 2016/11/14 16:31:02 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/08 17:01:24 by jhalford #+# #+# */
|
/* Created: 2016/11/08 17:01:24 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/11/10 12:11:20 by jhalford ### ########.fr */
|
/* Updated: 2016/11/14 11:08:19 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -17,9 +17,14 @@ void ft_sstrfree(char **sstr)
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
if (sstr)
|
||||||
|
{
|
||||||
while (sstr[i])
|
while (sstr[i])
|
||||||
{
|
{
|
||||||
ft_strdel(sstr + i);
|
ft_strdel(sstr + i);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
ft_strdel(sstr + i);
|
||||||
|
free(sstr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue