From 3fc3d0df969ae113d5cedf7da137b52d37651ba1 Mon Sep 17 00:00:00 2001 From: Alec <alec.schmidt@hesge.ch> Date: Mon, 30 May 2022 14:15:49 +0200 Subject: [PATCH] modified print func --- bp_tree.c | 14 ++++---- bp_tree.h | 2 +- main.c | 99 +++++++++++++++++++++---------------------------------- 3 files changed, 45 insertions(+), 70 deletions(-) diff --git a/bp_tree.c b/bp_tree.c index 9c94ef7..f7a3ab8 100644 --- a/bp_tree.c +++ b/bp_tree.c @@ -261,13 +261,14 @@ void bp_print_as_ll(node *root) return bp_print_as_ll(root->childs[0]); } + printf("|"); // print every values in the node until hitting 0, then we can get on the next node for (int i = 0; i < M; i++) { printf(" %d |", root->data[i]); if (root->data[i+1] == 0) { - printf("->"); + printf(" -> "); return bp_print_as_ll(root->next); } } @@ -288,23 +289,20 @@ void bp_print(node *root, int depth) if (root->data[i+1] == 0) break; } - printf("\n"); return; } for (int i = 0; i < M; i++) { - printf("\n"); bp_print(root->childs[i], depth + 1); - + printf("\n"); + for (int i = 0; i < depth; i++) printf(" "); - printf(" %d |", root->data[i]); + + printf(" %d |\n", root->data[i]); if (root->data[i+1] == 0) - { - printf("\n"); return bp_print(root->childs[i+1], depth + 1); - } } } \ No newline at end of file diff --git a/bp_tree.h b/bp_tree.h index 93a16b9..95cce5d 100644 --- a/bp_tree.h +++ b/bp_tree.h @@ -1,7 +1,7 @@ #ifndef _BP_TREE_C_ #define _BP_TREE_C_ -#define M 5 +#define M 4 #include <stdlib.h> typedef struct node{ diff --git a/main.c b/main.c index adff56c..5ac9fee 100644 --- a/main.c +++ b/main.c @@ -1,66 +1,43 @@ +#include "bp_tree.h" #include <stdio.h> #include <stdlib.h> -#include "bp_tree.h" - -int main(){ - - /* - node *test = bp_create_node(); - test->count = 1; - test->data[0] = 3; - for (int i = 0; i < 2; i++) - { - test->childs[i] = bp_create_node(); - test->childs[i]->count = 1; - test->childs[i]->data[0] = 3; - for (int y = 0; y < 2; y++) - { - test->childs[i]->childs[y] = bp_create_node(); - for (int z = 0; z < 2; z++) - { - test->childs[i]->childs[y]->data[z] = 3; - } - test->childs[i]->childs[y]->count = 2; - - } - - } - bp_print(test, 0); - bp_destroy(test);*/ - - node *tree = bp_create_node(); +int +main () +{ + node *tree = bp_create_node (); - tree = bp_insert_val(tree, 3); - bp_print(tree, 0); - printf("|||||||||\n"); - tree = bp_insert_val(tree, 6); - bp_print(tree, 0); - printf("|||||||||\n"); - tree = bp_insert_val(tree, 5); - bp_print(tree, 0); - printf("|||||||||\n"); - tree = bp_insert_val(tree, 4); - bp_print(tree, 0); - printf("|||||||||\n"); - tree = bp_insert_val(tree, 7); - bp_print(tree, 0); - printf("|||||||||\n"); - tree = bp_insert_val(tree, 8); - bp_print(tree, 0); - printf("|||||||||\n"); - tree = bp_insert_val(tree, 9); - bp_print(tree, 0); - printf("|||||||||\n"); - tree = bp_insert_val(tree, 10); - bp_print(tree, 0); - printf("|||||||||\n"); - tree = bp_insert_val(tree, 11); - bp_print(tree, 0); - printf("|||||||||\n"); - tree = bp_insert_val(tree, 12); - bp_print(tree, 0); - bp_print_as_ll(tree); - bp_destroy(tree); - return 0; + tree = bp_insert_val (tree, 3); + bp_print (tree, 0); + printf ("\n|||||||||\n"); + tree = bp_insert_val (tree, 6); + bp_print (tree, 0); + printf ("\n|||||||||\n"); + tree = bp_insert_val (tree, 5); + bp_print (tree, 0); + printf ("\n|||||||||\n"); + tree = bp_insert_val (tree, 4); + bp_print (tree, 0); + printf ("\n|||||||||\n"); + tree = bp_insert_val (tree, 7); + bp_print (tree, 0); + printf ("\n|||||||||\n"); + tree = bp_insert_val (tree, 8); + bp_print (tree, 0); + printf ("\n|||||||||\n"); + tree = bp_insert_val (tree, 9); + bp_print (tree, 0); + printf ("\n|||||||||\n"); + tree = bp_insert_val (tree, 10); + bp_print (tree, 0); + printf ("\n|||||||||\n"); + tree = bp_insert_val (tree, 11); + bp_print (tree, 0); + printf ("\n|||||||||\n"); + tree = bp_insert_val (tree, 12); + bp_print (tree, 0); + printf ("\n|||||||||\n"); + bp_print_as_ll (tree); + bp_destroy (tree); + return 0; } \ No newline at end of file -- GitLab