www.delorie.com/gnu/docs/avl/libavl_80.html   search  
Buy GNU books!

GNU libavl 2.0.1

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ] Displaying BST Structures

The print_tree_structure() function below can be useful for debugging, but it is not used very much by the testing code. It prints out the structure of a tree, with the root first, then its children in parentheses separated by a comma, and their children in inner parentheses, and so on. This format is easy to print but difficult to visualize, so it's a good idea to have a notebook on hand to sketch out the shape of the tree. Alternatively, this output is in the right format to feed directly into the texitree program used to draw the tree diagrams in this book, which can produce output in plain text or PostScript form.

/* Prints the structure of node, 
which is level levels from the top of the tree. */ static void
print_tree_structure (const struct bst_node *node, int level)
{ /* You can set the maximum level as high as you like. Most of the time, you'll want to debug code using small trees, so that a large level indicates a ``loop'', which is a bug. */ if (level > 16)
{ printf ("[...]"); return; } if (node == NULL) return; printf ("%d", *(int *) node-&#62;bst_data); if (node-&#62;bst_link[0] != NULL || node-&#62;bst_link[1] != NULL)
{ putchar ('('); print_tree_structure (node-&#62;bst_link[0], level + 1); if (node-&#62;bst_link[1] != NULL)
{ putchar (','); print_tree_structure (node-&#62;bst_link[1], level + 1); } putchar (')'); } }
See also @refalso{120 This code is included in @refalso{98

A function print_whole_tree() is also provided as a convenient wrapper for printing an entire BST's structure.

/* Prints the entire structure of tree with the given title. */
print_whole_tree (const struct bst_table *tree, const char *title)
{ printf ("%s:", title); print_tree_structure (tree-&#62;bst_root, 0); putchar ('\n'); }

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003