diff options
| author | Thomas Schmucker <ts@its1.de> | 2020-07-22 17:30:45 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2020-07-22 17:30:45 +0200 |
| commit | ac55496d881e0a17b3eff85f1faae5aafbc53b50 (patch) | |
| tree | 65b954e994d7bbfc76bc959876e28f7ea9fa708c /treeutil.h | |
| download | data-structures-ac55496d881e0a17b3eff85f1faae5aafbc53b50.tar.gz data-structures-ac55496d881e0a17b3eff85f1faae5aafbc53b50.tar.bz2 data-structures-ac55496d881e0a17b3eff85f1faae5aafbc53b50.zip | |
erster Commit
Diffstat (limited to 'treeutil.h')
| -rw-r--r-- | treeutil.h | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/treeutil.h b/treeutil.h new file mode 100644 index 0000000..302a6e3 --- /dev/null +++ b/treeutil.h | |||
| @@ -0,0 +1,45 @@ | |||
| 1 | // aux display and verification routines, helpful but not essential | ||
| 2 | struct trunk { | ||
| 3 | struct trunk *prev; | ||
| 4 | char * str; | ||
| 5 | }; | ||
| 6 | |||
| 7 | static void show_trunks(struct trunk *p) | ||
| 8 | { | ||
| 9 | if (!p) return; | ||
| 10 | show_trunks(p->prev); | ||
| 11 | printf("%s", p->str); | ||
| 12 | } | ||
| 13 | |||
| 14 | // this is very haphazzard | ||
| 15 | static void show_tree(struct tree_node *root, struct trunk *prev, int is_left) | ||
| 16 | { | ||
| 17 | if (root == NULL) return; | ||
| 18 | |||
| 19 | struct trunk this_disp = { prev, " " }; | ||
| 20 | char *prev_str = this_disp.str; | ||
| 21 | show_tree(root->right, &this_disp, 1); | ||
| 22 | |||
| 23 | if (!prev) | ||
| 24 | this_disp.str = "---"; | ||
| 25 | else if (is_left) { | ||
| 26 | this_disp.str = ".--"; | ||
| 27 | prev_str = " |"; | ||
| 28 | } else { | ||
| 29 | this_disp.str = "`--"; | ||
| 30 | prev->str = prev_str; | ||
| 31 | } | ||
| 32 | |||
| 33 | show_trunks(&this_disp); | ||
| 34 | if ( root->key >= 'A' && root->key <= 'Z' ) | ||
| 35 | printf("%c\n", root->key); | ||
| 36 | else | ||
| 37 | printf("%d\n", root->key); | ||
| 38 | |||
| 39 | if (prev) prev->str = prev_str; | ||
| 40 | this_disp.str = " |"; | ||
| 41 | |||
| 42 | show_tree(root->left, &this_disp, 0); | ||
| 43 | if (!prev) puts(""); | ||
| 44 | } | ||
| 45 | |||
