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:
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"
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
Test Input: We have provided a main function that reads two types of commands from standard input:
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 leavesshould produce the output:
number of leaves = 2 number of leaves = 3 number of leaves = 5