I've written code for displaying a B-tree in tree traversal order or 1 level of the B-tree at a time. However, I my code is not implemented as it should. It only works logically for a B-tree of level 2, since the nodes under the root are leaf nodes. but for larger depths, the B-tree would print children nodes from left branches 1st then going back up to the right branches. How should I modify the recursion, etc. to print nodes of a B-tree, from left to right before going to the next level?
Code:
void print_btree(short root){
BTPAGE page;
int i;
btread(root,&page);
//print pg #
printf("%d\t",root);
//print keys of page
for(i = 0; i < page.keycount; ++i)
printf("%s ",page.key[i]);
//add space padding
for(i = page.keycount; i < MAXKEYS; ++1)
printf(" ");
printf("\t");
//print child ptrs of page
for(i = 0; i <= page.keycount; ++i)
printf("%d ",page.child[i]);
printf("\n");
//print lower/next level of btree - RECURSIVE
for(i = 0; i <= page.keycount; ++i){
if(page.child[i] != NIL)
print_btree(page.child[i]);
}
return;
}