Question
Please help me out with this assignment. The code is provided below: Please do not change anything except those 4 problems mentioned in the description.
Please help me out with this assignment.
The code is provided below:
Please do not change anything except those 4 problems mentioned in the description.
----------------------------------------------------------------------
import java.io.*; import java.util.*; public class Lab1 { /** * Problem 1: Determine if two given binary search trees store the same numbers. Iterative Solution */ private static boolean problem1Iterative(Node t1, Node t2) { // Implement me! return false; } /** * Problem 1: Determine if two given binary search trees store the same numbers. Recursive Solution */ private static boolean problem1Recursive(Node t1, Node t2) { // Implement me! return false; } /** * Problem 2: Determine the sum of all node's keys between min and max (inclusive). Iterative Solution */ private static int problem2Iterative(Node root, int min, int max) { // Implement me! return 0; } /** * Problem 2: Determine the sum of all node's keys between min and max (inclusive). Recursive Solution */ private static int problem2Recursive(Node root, int min, int max) { // Implement me! return 0; }
// --------------------------------------------------------------------- // Do not change any of the code below! private static class Node { public Node left = null; public Node right = null; public int key; public Node(int key) { this.key = key; } } private static void insert(Node root, int key) { if (root == null) { root = new Node(key); return; } for (Node node = root;;) { if (key node.key) { if (node.right == null) { node.right = new Node(key); } node = node.right; } else // key = node.key { // Nothing to do, because no value to update. break; } } } private static boolean find(Node root, int key){ if(root == null) { return false; }
if(root.key == key) { return true; } if(key stack = new Stack
{ if (node == null) { if (stack.empty()) break; node = stack.pop(); node = node.right; } else { orderList.add(node.key); stack.push(node); node = node.left; } } int[] order = new int[orderList.size()]; for (int i = 0; i stack = new Stack
{ stack.push(node.right); stackCtr.push(0); } } else // ctr >= 2 { // Third visit. // Right subtree done. stack.pop(); orderList.add(node.key); } } int[] order = new int[orderList.size()]; for (int i = 0; i
{ answer = problem1Iterative(root1, root2); }else{ answer = problem1Recursive(root1, root2); } return solution == answer; } private static boolean testProblem2(int[][] testCase, int style) { int[] tree = testCase[0]; int[] range = testCase[1]; int solution = testCase[2][0]; Node root = new Node(tree[0]); for (int i = 1; i
try { switch (prob) { case 1: testCase = createProblem1(i); passed = testProblem1(testCase, style); break; case 2: testCase = createProblem2(i); passed = testProblem2(testCase, style); break; } } catch (Exception ex) { passed = false; exce = true; } if (!passed) { System.out.println("Test " + i + " failed!" + (exce ? " (Exception)" : "")); if (prob == 1) { System.out.println(" tree 1: " + Arrays.toString(testCase[0])); System.out.println(" tree 2: " + Arrays.toString(testCase[1])); } else { System.out.println(" tree: " + Arrays.toString(testCase[0])); System.out.println(" range: " + Arrays.toString(testCase[1])); } passedAll = false; break; } } if (passedAll) { System.out.println("All test passed."); } } private static void shuffle(int[] arr) { for (int i = 0; i
int tmp = arr[i]; arr[i] = arr[rndInd]; arr[rndInd] = tmp; } } private static int[] getRandomNumbers(int size) { int maxSize = size * 10; int[] integers = new int[maxSize]; for (int i = 0; i
{ int maxSize = max = minKey && tree[i] Problem 1 Your are given the root nodes of two binary search trees. Determine if both trees store the same numbers. Note that the trees do not need to be equivalent in structure; the question is only if they store the same numbers. Implement an iterative and recursive solution. Problem 2 Your are given the root node of a binary search tree T and two integers min and max. Note that min and max are not necessarily stored in the tree. Determine the sum of all keys stored in T that are larger than or equal to min, and smaller than or equal to max. Implement an iterative and recursive solution. Implementation You are given a file Lab1.java (which you can download from canvas). The file contains a class Lab1 with the four functions: problem1Iterative, problem1Recursive, problem2Iterative and problem2Recursive. Implement your solutions in the corresponding functions. You may use the helper functions which are already implemented in the file given to you. Do not make any changes outside of these four functions (e. g. by adding helper functions); such changes will be undone. Do not output anything to the terminal. The program already implemented in the file Lab1.java randomly generates test cases. This file contains a small number of test cases. The seed of the random number generator is set to ensure the same test cases whenever to program is executed. Note that the purpose of the tests is for you to avoid major mistakes. Passing all given tests does not imply that your algorithm is correct, especially that is has the expected runtime
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started