(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
- reverseString(string &s): s is the string being reversed
- reverseStringHelper(string &s, int startChar, int endChar): s
is the string being reversed. startChar and endChar
are the indices of the first and last character to be reversed.
Constraints:
- The string could be empty so make sure you properly handle an empty string
- You must modify the string passed in as a parameter. You may not
make a copy of the string.
- 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"
(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
- 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.
- 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:
- insert key: inserts a key into the tree
- 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