btree print is cool now
This commit is contained in:
parent
5a6159c224
commit
29674a9869
4 changed files with 61 additions and 20 deletions
|
|
@ -40,6 +40,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));
|
void btree_print(t_btree *tree, char *(*printer)(void *));
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -95,6 +95,7 @@ void ft_putchar_fd(char c, int fd);
|
||||||
void ft_putstr_fd(char const *s, int fd);
|
void ft_putstr_fd(char const *s, int fd);
|
||||||
void ft_putendl_fd(char const *s, int fd);
|
void ft_putendl_fd(char const *s, int fd);
|
||||||
void ft_putnbr_fd(int n, int fd);
|
void ft_putnbr_fd(int n, int fd);
|
||||||
|
void ft_putaddr(void *a);
|
||||||
|
|
||||||
char *ft_strrev(char *str);
|
char *ft_strrev(char *str);
|
||||||
char **ft_strsplit(char const *s, char c);
|
char **ft_strsplit(char const *s, char c);
|
||||||
|
|
|
||||||
|
|
@ -11,32 +11,66 @@
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "btree.h"
|
#include "btree.h"
|
||||||
|
int _print_t(t_btree *tree, int is_left, int offset, int depth, char s[20][255], char *(*printer)(void *))
|
||||||
static void print_offset(int offset)
|
|
||||||
{
|
{
|
||||||
int i;
|
char b[20];
|
||||||
|
int width = 5;
|
||||||
|
|
||||||
i = 0;
|
if (!tree)
|
||||||
while (i < offset)
|
return 0;
|
||||||
|
/* ft_printf("new tree elem: %s\n", printer(tree)); */
|
||||||
|
sprintf(b, "%5s", printer(tree->item));
|
||||||
|
int left = _print_t(tree->left, 1, offset, depth + 1, s, printer);
|
||||||
|
int right = _print_t(tree->right, 0, offset + left + width, depth + 1, s, printer);
|
||||||
|
#ifdef COMPACT
|
||||||
|
for (int i = 0; i < width; i++)
|
||||||
|
s[depth][offset + left + i] = b[i];
|
||||||
|
if (depth && is_left)
|
||||||
{
|
{
|
||||||
ft_putstr(" ");
|
for (int i = 0; i < width + right; i++)
|
||||||
i++;
|
s[depth - 1][offset + left + width/2 + i] = '-';
|
||||||
|
s[depth - 1][offset + left + width/2] = '.';
|
||||||
}
|
}
|
||||||
|
else if (depth && !is_left)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < left + width; i++)
|
||||||
|
s[depth - 1][offset - width/2 + i] = '-';
|
||||||
|
s[depth - 1][offset + left + width/2] = '.';
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
for (int i = 0; i < width; i++)
|
||||||
|
s[2 * depth][offset + left + i] = b[i];
|
||||||
|
if (depth && is_left)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < width + right; i++)
|
||||||
|
s[2 * depth - 1][offset + left + width/2 + i] = '-';
|
||||||
|
s[2 * depth - 1][offset + left + width/2] = '+';
|
||||||
|
s[2 * depth - 1][offset + left + width + right + width/2] = '+';
|
||||||
|
}
|
||||||
|
else if (depth && !is_left) {
|
||||||
|
for (int i = 0; i < left + width; i++)
|
||||||
|
s[2 * depth - 1][offset - width/2 + i] = '-';
|
||||||
|
s[2 * depth - 1][offset + left + width/2] = '+';
|
||||||
|
s[2 * depth - 1][offset - width/2 - 1] = '+';
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return (left + width + right);
|
||||||
}
|
}
|
||||||
|
|
||||||
void btree_print(t_btree *tree, void (*printer)(t_btree *tree))
|
void btree_print(t_btree *tree, char *(*printer)(void *))
|
||||||
{
|
{
|
||||||
static int offset = 0;
|
char s[20][255];
|
||||||
|
char empty[255];
|
||||||
print_offset(offset);
|
/* for (int i = 0; i < 20; i++) */
|
||||||
if (tree == NULL)
|
/* s[i][1] = 0; */
|
||||||
|
for (int i = 0; i < 20; i++)
|
||||||
|
sprintf(s[i], "%80s", " ");
|
||||||
|
sprintf(empty, "%80s", " ");
|
||||||
|
_print_t(tree, 0, 0, 0, s, printer);
|
||||||
|
for (int i = 0; i < 20; i++)
|
||||||
{
|
{
|
||||||
ft_putendl("-");
|
if (ft_strcmp(s[i], empty) == 0)
|
||||||
return ;
|
break ;
|
||||||
|
printf("%s\n", s[i]);
|
||||||
}
|
}
|
||||||
(*printer)(tree);
|
|
||||||
offset += 3;
|
|
||||||
btree_print(tree->right, printer);
|
|
||||||
btree_print(tree->left, printer);
|
|
||||||
offset -= 3;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
6
libftasm/src/printing/ft_putaddr.c
Normal file
6
libftasm/src/printing/ft_putaddr.c
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_putaddr(void *a)
|
||||||
|
{
|
||||||
|
ft_printf("%p", a);
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue