Mathematics 1201: Programming in C Reading and Homework Assignment #10 Prof. Wickerhauser Due 3 November 1999 You are encouraged to collaborate on homework, and to work additional exercises from the indicated problem sections, although the homework grade will be based only on the exercises listed below. Please return your solutions to me by the end of class. LATE HOMEWORK WILL NOT BE ACCEPTED. Read Chapter 11 of the textbook. Do Exercises 3(a) and 12 of Chapter 11, p. 325. Extra Problem 1. What will the following program print? #include void PrintLastChar(const char *Str) { if( *Str ) { PrintLastChar(Str+1); printf("%c", *Str); } return; } int main(void) { PrintLastChar("Able was I, ere I saw Elba"); printf("\n"); return 0; } Extra Problem 2. What will the following program do? (Hint: Draw a labeled picture of the binary tree data structure that it generates.) #include #include struct Node { struct Node *Left; struct Node *Right; int Content; }; typedef struct Node *Tree; Tree MakeTree(int Depth) { Tree Root; if(Depth>0) { Root = (Tree)malloc(sizeof(struct Node)); Root->Left = MakeTree(Depth-1); Root->Right = MakeTree(Depth-1); Root->Content = 0; } else Root = 0; return Root; } int NumberTree(Tree Root, int NextNumber) { if(Root) { NextNumber = NumberTree ( Root->Left, NextNumber ); NextNumber = NumberTree ( Root->Right, NextNumber ); Root->Content = NextNumber; ++NextNumber; } return NextNumber; } void SwapTree(Tree Root) { Tree temp; if(Root) { SwapTree ( Root->Left ); SwapTree ( Root->Right ); temp = Root->Left; Root->Left = Root->Right; Root->Right = temp; } return; } void PrintContents(Tree Root) { if(Root) { PrintContents(Root->Left); PrintContents(Root->Right); printf("%d ", Root->Content); } else printf("*"); /* ==> Next is a `leaf' node */ return; } int main(void) { Tree Root; Root = MakeTree(3); NumberTree(Root, 0); PrintContents(Root); putchar('\n'); SwapTree(Root); PrintContents(Root); putchar('\n'); return 0; }