Special bonus features (at no additional cost!): Here is an elaboration of the answer to question 2 below: A program to read binary trees as a sequence of characters, using the syntax for binary trees given in question 2. The program then converts the tree to internal tree format. Finally, the program writes the tree as the same sequence of characters using the same format. (The lower-case letters at the nodes are converted to upper-case, just to prove that something is happening.)
Here is the C program elaborated into two programs. The first reads lowercase letters and uses them to build a binary search tree. This tree is written out using the above format. The second program reads in the binary search tree, and does an inorder traversal to print the letters in alphabetic order. Finally, the listing illustrates using a Unix pipe between the two processes, where a binary tree is fed from the first process (standard output) into the second process (standard input):
P ---> E '#' E ---> E '+' T | E '-' T | T T ---> T '*' S | T '/' S | S S ---> F '^' S | F F ---> 'a' | 'b' | 'c' | '(' E ')'
P +----+-----+ E | | | T | +------------------------+----+ | T | | | | | | | S | | | | | | | F | | | +---------+-------------------+ | | | | E | | | | | +----+---------+ | | | | | E | T | | | | | | | | | | | | | T | S | | | | | | | +----+----+ | | | | | S | | | S | | S | | | | | | | | | | | | F | F | F | | F | | | | | | | | | | | ( a + b ^ c ) * a #
(a (b (d 0 0) 0) (c (e 0 0) (f 0 0))) # a / \ / \ / \ b c / \ / \ / 0 / \ d e f / \ / \ / \ 0 0 0 0 0 0
bintree ---> tree '#' tree ---> '0' | '(' letter tree tree ')' letter ---> 'a' | 'b' | 'c' | ...
void bintree(void) { scan(); tree(); if (next != '#') error(1); else printf("Successful parse\n"); } void tree(void) { if (next == '0') scan(); else { if (next == '(') scan(); else error(2); if (isalpha(next)) scan(); else error(3); tree(); tree(); if (next == ')') scan(); else error(4); } } void scan(void) { while (isspace(next = getchar())) ; } void error(int n) { printf("\n*** ERROR: %i\n", n); exit(1); }
I ---> '[' E '?' { S } ':' { S } ']'
# Start of code for an if-then-else # INSERT EXTRA LABEL(S) AND/OR INSTRUCTION(S) HERE # Start of if-then-else (no MIPS code needed here) # Start of code to evaluate an expression E ... # Result left in a temporary memory location 144 bytes past # the start of the memory array M. # INSERT EXTRA LABEL(S) AND/OR INSTRUCTION(S) HERE lw $t1, 172($s1) beq $t1, $zero, ThenEnd4 # Start of code to evaluate the first portion { S } ... # INSERT EXTRA LABEL(S) AND/OR INSTRUCTION(S) HERE j ElseEnd4 ThenEnd4: # Start of code to evaluate the second portion {S} ... # INSERT EXTRA LABEL(S) AND/OR INSTRUCTION(S) HERE ElseEnd4:(Note that there must be a unique sequence number, like the 4 above, that will distinguish this if-then-else from any other.)
s[0][0], s[0][1], s[0][2], s[0][3], s[1][0], s[1][1], s[1][2], s[1][3], s[2][0], s[2][1], s[2][2], s[2][3], s[3][0], s[3][1], s[3][2], s[3][3], s[4][0], s[4][1], s[4][2], s[4][3].