diff options
-rw-r--r-- | trees.c | 32 | ||||
-rw-r--r-- | trees.h | 5 |
2 files changed, 30 insertions, 7 deletions
@@ -28,14 +28,36 @@ struct tnode *bintree_insert(struct tnode *p, char *t) return p; } -/* in-order tree print */ -void treeprint_inorder(struct tnode *p) +/* CLR: pre-order / prefix tree print */ +void treeprint_prefix(struct tnode *p) { if (p != NULL) { - treeprint_inorder(p->left); printf("%4d %s\n", p->count, p->text); - treeprint_inorder(p->right); + treeprint_prefix(p->left); + treeprint_prefix(p->right); + } +} + +/* LCR: in-order / infix tree print */ +void treeprint_infix(struct tnode *p) +{ + if (p != NULL) + { + treeprint_infix(p->left); + printf("%4d %s\n", p->count, p->text); + treeprint_infix(p->right); + } +} + +/* LRC: post-order / postfix tree print */ +void treeprint_postfix(struct tnode *p) +{ + if (p != NULL) + { + treeprint_postfix(p->left); + treeprint_postfix(p->right); + printf("%4d %s\n", p->count, p->text); } } @@ -51,7 +73,7 @@ int main(int argc, char **argv) for (i = 0; i < sizeof(tokens) / sizeof(char *); i++) root = bintree_insert(root, tokens[i]); - treeprint_inorder(root); + treeprint_infix(root); return 0; } @@ -16,7 +16,8 @@ struct tnode /* function prototypes */ struct tnode *bintree_insert(struct tnode *, char *); -void treeprint_inorder(struct tnode *); - +void treeprint_prefix(struct tnode *); +void treeprint_infix(struct tnode *); +void treeprint_postfix(struct tnode *); #endif |