summaryrefslogtreecommitdiffstats
path: root/trees.c
diff options
context:
space:
mode:
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;
}