From 7e7ddb63cf68d8733a134bfbd22d309c7e152f26 Mon Sep 17 00:00:00 2001 From: Kyle K Date: Sat, 11 Dec 2010 18:50:00 -0600 Subject: implement prefix, infix, and postfix binary tree print --- trees.c | 32 +++++++++++++++++++++++++++----- trees.h | 5 +++-- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/trees.c b/trees.c index dd38e0c..60de45a 100644 --- a/trees.c +++ b/trees.c @@ -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; } diff --git a/trees.h b/trees.h index 93f63d9..95a1c0a 100644 --- a/trees.h +++ b/trees.h @@ -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 -- cgit v1.2.3