Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

family tree is a text file that is read in and converted to a binary tree select

ID: 3704102 • Letter: F

Question

family tree is a text file that is read in and converted to a binary tree

selectIterative Takes a Predicate p and returns the list of all elements
    for which p.check is true. The elements must be returned in
    "in order" traversal order.

df-Adobe Reader View Window Help 51/6| | 109% | Tools Fill & Sign to write test cases for both selectRecursive and selectiterative to make sure the code really works. Your methods will be graded on several additional hidden tests and on the quality of your tests for both methods HINT 3: The provided tests include only one Predicate implementation, IsEven. The hidden tests will use other Predicates, so test your code with other Predicates that you define. To write a new test case, we recommend that you copy/paste an existing one and change the name of the test method. Then draw your test tree on paper so you know what order to insert nodes using insertNode and what your assertions' expected values are. Part 5: Queries that use Select In FamilyRecordQuery.java uncomment the code required for Part 5 in the main method and the classes. As you noticed, the family has a lot of people named Robert and a lot of engineers. Use your selectIterative and selectRecursive to find these people in the family tree. To find all the Roberts, fix the SelectName class and search for the exact string "Robert" in the name field of the FamilyRecord. This must use selectIterative. To find all the Engineers, fix the Select3ob class and search for any text containing "Engineer" in the job field of the FamilyRecord. This must use selectRecursive. This query will output a list of many engineer types (people born in 1920 couldn't have been Software Engineers). Feel free to test our different names or jobs with these queries. To check your work, look at the displayed tree or the CVS file to see that the output is correct Part 6: One more query Uncomment the code in the main method of FamilyRecordQuery.java. Write the code that will return and print out all the people in the family tree that are under the age of 50. Follow the same structure of

Explanation / Answer

import java.util.Stack;

/* Class to create Node */
class Node {
String name;
Node left, right;

public Node(String data) {
name = data;
left = right = null;
}


// Check method - can be changed to check any variable of node
public void check(String p) {
if (name.contains(p)) {
System.out.print(name + ", ");
}
}
}

// Inorder traversal of tree
class BinaryTree {
Node root;

void selectIterative(String p) {
// Stack holds nodes that are to be visited
Stack<Node> stack = new Stack<Node>();
Node node = root;

// Since in-order, leftmost node will be visited first
while (node != null) {
stack.push(node);
node = node.left;
}

// traverse the tree
while (stack.size() > 0) {
node = stack.pop(); // visit the top node

node.check(p); // call the check method
  
if (node.right != null) {
node = node.right;

// the next node to be visited is the leftmost
while (node != null) {
stack.push(node);
node = node.left;
}
}
}
}

public static void main(String args[]) {

/* This creates binary tree manually. If you already have the tree
created, pass the root of the tree. */
BinaryTree tree = new BinaryTree();
tree.root = new Node("Emily Adams");
tree.root.left = new Node("Robert Tyler");
tree.root.right = new Node("Jane Tyler");
tree.root.left.left = new Node("Robert Jordan");
tree.root.left.right = new Node("Shawn Jordan");
tree.selectIterative("Robert");
}
}

// OUTPUT - Robert Jordan, Robert Tyler,