diff --git a/bp_tree.c b/bp_tree.c index 9c94ef736a6b72ac68e68432606cf376c0cf9470..f7a3ab800ba13700808c0791e0a2f90555ebaafb 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 93a16b98cd9f5f7d92bf34a596856fa2f105ba82..95cce5de883c810675d5983a790d846e30388034 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 adff56c8f3f23e553867f80832b2e95a54e7137d..5ac9fee2b50eee95699dec9c100a08fb94b7fbfe 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