CS140 Final

Fall 2018

Instructions

  1. Use the Code Assessor development website, cs102dev.eecs.utk.edu, to answer each of the coding questions.
  2. Leave this worksheet next to your computer for the next lab section.
  3. You may have a one page cheat sheet, with writing on front and back.
  4. The name of the coding question on Code Assessor is shown next to each question. Both questions are in the CS140 category.
  5. You may not be able to finish all the problems. If so, please do not worry. We will examine all answers (even correct ones) and award partial credit. Make sure you budget your time so that you have time to write a complete solution to both problems. You can still obtain almost 100% credit even if your program is not running, so if you think your program is mostly correct but it is not running, move on to the other problem.
  6. You have 2 hours for this exam. Once you finish you should start working on this week's lab assignment. Please do not discuss the exam with anyone from another lab section until after 5:30pm.
  7. We will look at coding style and efficiency in grading your problems. Even if your answer passes the test cases, you might lose points if your solution is overly inefficient, is hard to read, uses poor variable names, etc.
  8. Remember to pass all object parameters by reference!
  9. Good luck!

  1. (30 points--CS140Fa18-String-Reversal): Write a pair of functions named reverseString and reverseStringHelper that together perform an "in-place" reversal of a string. By "in-place" I mean reverse the original string, rather than making a copy of the original string. For example, if the original string is "smiley", then the altered string should be "yelims".

    You must use a recursive solution to solve this problem. You can recursively reverse a string by swapping the beginning and ending characters, then the first and next to last character, then the second and second to last character, and so on. reverseStringHelper will be your recursive function. reverseString will call reverseStringHelper and pass it both the string and the indices of the beginning and ending characters to be reversed (recall that in class we often used recursive helper functions because the recursion requires some "state" information that we do not want the caller of the original function to have to worry about). Your initial call to reverseStringHelper will be "reverseStringHelper(s, 0, s.size()-1)" and thereafter reverseStringHelper will swap characters and then recursively call itself by appropriately modifying the beginning and ending character indices.

    Function Prototypes

    Constraints:

    1. The string could be empty so make sure you properly handle an empty string
    2. You must modify the string passed in as a parameter. You may not make a copy of the string.
    3. You must use recursion. You will receive a 0 for not using recursion.

    Test Input: The input consists of a word or phrase. For example:

    "smiley"        reverseString should return "yelims"
    
    "brad"          reverseString should return "darb"
    
    "now"           reverseString should return "won"
    
    "a"             reverseString should return "a"
    
    ""              reverseString should return ""
    
    "I won the race!" reverseString should return "!ecar eht now I"
    
    
  2. (30 points--CS140Fa18-LeafCount): Write a recursive method named recursive_leafcount that takes a binary tree node as a parameter and returns the number of leaves in the binary search tree rooted at that node. recursive_leafcount should return 0 if the node is a sentinel node, 1 if the node is a leaf node, and otherwise should return the sum of the number of leaves in the left and right subtrees. For example, if the left subtree has 4 leaves and the right subtree has 10 leaves, then recursive_leafcount should return 14. If the node is a leaf node, then each of its children will be a sentinel node and hence both children will return 0. Thus if the number of leaves in a node's two subtrees is 0, then the node must be a leaf and you should return 1. For this problem, you are given the following abbreviated class declaration from the class notes:
    class BSTNode {
      public:
        BSTNode *left;
        BSTNode *right;
        string key;
    };
    
    class BSTree {
      public:
        BSTree();
        int Insert(string key);
        int LeafCount();
      protected:
        BSTNode *sentinel;
    
        int recursive_leafcount(BSTNode *n);
    };
    
    int BSTree::LeafCount() {
      return recursive_leafcount(sentinel->right);
    }
    
    LeafCount() passes the root node of the tree to recursive_leafcount. The constructor function initializes the tree and the insert function adds keys to the tree. We have provided the code for the constructor, LeafCount, and Insert functions for you. You only have to write recursive_leafcount.

    Constraints

    1. You will always be passed a pointer to the node of a well-formed binary search tree. That means that if a node does not have a left or right child, then the left or right pointer points to the sentinel node.

    2. The tree is allowed to be empty, in which case the root of the tree will be the sentinel node and recursive_leafcount should return 0.

    Test Input: We have provided a main function that reads two types of commands from standard input:

    1. insert key: inserts a key into the tree
    2. leaves: prints the number of leaves of the tree by calling the tree's LeafCount method and printing the return value. Note that recursive_leafcount should not print anything.

    For example, the input:

    insert joe
    insert mary
    insert brad
    leaves
    insert frank
    insert jessie
    insert norman
    insert jim
    insert ebby
    leaves
    insert al
    insert lucy
    leaves
    
    should produce the output:
    number of leaves = 2
    number of leaves = 3
    number of leaves = 5