CS 3723 Programming Languages
Writing and Reading Binary Trees
Using Separate Programs

This example consists of two separate C programs:

First the two programs are executed separately. Then the output of the first is piped into the second as input. This example shows how a program (search_tree.c above) can create a binary tree, "flatten" it and output it as a stream of characters. This output can then be piped into a second program which decodes the binary tree, that is, "unflattens" it. Finally an inorder traversal is carried out on this reconstructed binary tree.

Part of the point of this example is to illustrate how much better it would be if one could pipe a whold data structure from one process to another.


Here is the first program: search_tree.c:


Here is the second program: traverse_tree.c:


Output of sample runs:


Here the programs have been redone so that no blanks are output and blanks are not skipped over on input. This will handle most input characters, including '0', ' ', '(', and ')', but not '#'. (It could also be fixed to not require '#' at the end.) The input routine in the second program is just next = getchar(); and the second program has dropped the isalpha(next) requirement. The output routine in the first program is now:

Here are the new results of test runs: