diff options
Diffstat (limited to 'src/treeutil.h')
| -rw-r--r-- | src/treeutil.h | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/treeutil.h b/src/treeutil.h new file mode 100644 index 0000000..76627c0 --- /dev/null +++ b/src/treeutil.h | |||
| @@ -0,0 +1,51 @@ | |||
| 1 | // aux display and verification routines, helpful but not essential | ||
| 2 | struct trunk { | ||
| 3 | struct trunk *prev; | ||
| 4 | const char * str; | ||
| 5 | }; | ||
| 6 | |||
| 7 | static void | ||
| 8 | show_trunks(struct trunk *p) | ||
| 9 | { | ||
| 10 | if ( !p ) | ||
| 11 | return; | ||
| 12 | show_trunks(p->prev); | ||
| 13 | printf("%s", p->str); | ||
| 14 | } | ||
| 15 | |||
| 16 | // this is very haphazzard | ||
| 17 | static void | ||
| 18 | show_tree(struct tree_node *root, struct trunk *prev, int is_left) | ||
| 19 | { | ||
| 20 | if ( root == NULL ) | ||
| 21 | return; | ||
| 22 | |||
| 23 | struct trunk this_disp = { prev, " " }; | ||
| 24 | const char * prev_str = this_disp.str; | ||
| 25 | show_tree(root->right, &this_disp, 1); | ||
| 26 | |||
| 27 | if ( !prev ) | ||
| 28 | this_disp.str = "---"; | ||
| 29 | else if ( is_left ) { | ||
| 30 | this_disp.str = ".--"; | ||
| 31 | prev_str = " |"; | ||
| 32 | } | ||
| 33 | else { | ||
| 34 | this_disp.str = "`--"; | ||
| 35 | prev->str = prev_str; | ||
| 36 | } | ||
| 37 | |||
| 38 | show_trunks(&this_disp); | ||
| 39 | if ( root->key >= 'A' && root->key <= 'Z' ) | ||
| 40 | printf("%c\n", root->key); | ||
| 41 | else | ||
| 42 | printf("%d\n", root->key); | ||
| 43 | |||
| 44 | if ( prev ) | ||
| 45 | prev->str = prev_str; | ||
| 46 | this_disp.str = " |"; | ||
| 47 | |||
| 48 | show_tree(root->left, &this_disp, 0); | ||
| 49 | if ( !prev ) | ||
| 50 | puts(""); | ||
| 51 | } | ||
