summaryrefslogtreecommitdiffstats
path: root/trees.c
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2010-12-11 18:50:00 -0600
committerKamil Kaminski <kamilkss@gmail.com>2010-12-11 18:50:00 -0600
commit7e7ddb63cf68d8733a134bfbd22d309c7e152f26 (patch)
tree4f6693194a5ba3d86e34f782cd0db02a6f6a2565 /trees.c
parent010cad880b213dbdd1b69bfbaf0c3af1f44e474f (diff)
downloadtrees-7e7ddb63cf68d8733a134bfbd22d309c7e152f26.tar.gz
trees-7e7ddb63cf68d8733a134bfbd22d309c7e152f26.tar.bz2
trees-7e7ddb63cf68d8733a134bfbd22d309c7e152f26.zip
implement prefix, infix, and postfix binary tree print
Diffstat (limited to 'trees.c')
-rw-r--r--trees.c32
1 files changed, 27 insertions, 5 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;
}