Write a recursive function named binsearch that implements the above algorithm. Your function header is:
Person *binsearch(string key, vector<Person *> &students, int low, int high);
The four parameters are defined as follows:
Your binsearch procedure returns a pointer to the student object containing the search key or NULL if the search key is not in the vector. A student object is declared as follows:
class Person {
public:
string name;
double gpa;
string hometown;
};
You will be comparing the search key against the name field of
student objects in the vector.
Constraints:
Test Input: Here is some sample input. We have written a test driver that reads this input, creates the vector of Person objects, and then calls your binsearch procedure with search keys:
smiley 3.6 Knoxville nels 4.0 San_Jose josie 4.0 Nashville tom 2.75 Chattanooga
A binary expression tree is one in which the interior nodes are operators, such as + or -, and the leaves are numbers. For example:
+
/ \
- +
/ \ / \
- 4 8 10
/ \
16 5
This expression tree represents the arithmetic expression:
((16-5) - 4) + (8+10)
which evaluates to 25. An expression tree can be evaluated
by evaluating the left subtree, then the right subtree, and finally
performing the indicated operation on the values returned by evaluating
the left and right subtrees. A leaf evaluates to its number. For example,
in the above expression tree, the + node with the leaves 8 and
10 evaluates to 18 since its left subtree evaluates to 8 and its right
subtree evaluates to 10. Similarly the root + node evaluates to
25 because its left subtree evaluates to 7 and its right subtree to 18.
You are given the following class declaration for an expression tree node:
class ExpNode {
protected:
double value;
char operation;
bool leaf;
ExpNode *left;
ExpNode *right;
public:
ExpNode(double v) : value(v), leaf(true) {}
ExpNode(char opn, ExpNode *leftChild, ExpNode *rightChild)
: operation(opn), left(leftChild), right(rightChild), leaf(false) {}
double evaluate();
};
Your task is to write the method named evaluate. It should
evaluate the subtree rooted at that ExpNode and return the value of
computed expression as a double.
If the node is a leaf, then the leaf variable will be true and
the value field will be set to a number. If the node is an operation node,
then leaf will be false and the operation field will contain the
node's operator, which may be one of '+', '-', '*', and '/'.
If the node is a leaf, evaluate will return the number associated with the leaf. Otherwise evaluatewill evaluate the left and right subtrees, perform the operation indicated by the operation field on the results returned by the left and right subtrees, and return this value.
We have provided a testdriver in main that reads in a prefix arithmetic expression, constructs an expression tree from it, and calls the root node's evaluate method. Some sample inputs would be:
5 // evaluate should return 5
* 3 4.2 // evaluate should return 12.6
- 4 9 // evaluate should return -5
* + 3 6 4 // evaluate should return 36. The input represents the
// expression (3 + 6) * 4
/ * 8 5 - 4 2 // evaluate should return 20. The input represents the
// expression (8 * 5) / (4 - 2)