like the title
i wanna know how to output a B-tree like that:

the struct of B-tree is:
typedef struct node {
            datatype data;
             struct node *lchild, *rchild;
} BTnode,*BTREE
"*" means escape


Edited by mike_2000_17: Fixed formatting

10 Years
Discussion Span
Last Post by Narue

>i wanna know how to output a B-tree like that:
Please don't refer to binary trees as B-trees. A B-tree is a separate data structure with significant differences. As for how to print a binary tree like you want, you can do it a number of ways. However, that particular method is rather limiting as you'll have to deal with line wrapping on wider trees. I've found something like this to be more useful as well as simpler to implement:

static void jsw_splaydump_r ( jsw_splaynode_t *root, int level, FILE *out )
  int i;

  if ( root == NULL ) {
    for ( i = 0; i < level; i++ )
      fputc ( '\t', out );
    fputs ( "~\n", out );
  else {
    jsw_splaydump_r ( root->link[1], level + 1, out );

    for ( i = 0; i < level; i++ )
      fputc ( '\t', out );
    fprintf ( out, "%d\n", *(int *)root->data );

    jsw_splaydump_r ( root->link[0], level + 1, out );

void jsw_splaydump ( jsw_splay_t *tree, FILE *out )
  jsw_splaydump_r ( tree->root, 0, out );
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.