Question
Can I please get an iterative solution to this problem? Problem You are given two non-empty binary search tree T1 and T2. T1 and T2
Can I please get an iterative solution to this problem?
Problem You are given two non-empty binary search tree T1 and T2. T1 and T2 store the same keys. The structure of both trees, however, is different. Implement an algorithm that uses rotations on T1 to make it equivalent to T2. That is, both trees should have identical structure. Note that you are only allowed to use rotations and only on T1; you are not allowed to modify the trees in any other way. The implementation must be iterative. Implementation You are given two files (which you can download from canvas): Lab2.java and BST.java. The file Lab2.java generates test cases, performs the tests, and outputs the results. The file BST.java partially implements a binary search tree and contains the function problem; implement your solution in that function. Do not make any changes outside of that function; such changes will be undone. Do not output anything to the terminal. The class BST also contains the functions rotateL, rotateR, find, as well as functions for in- and pre-order. Feel free to use these functions in your implementation. The program already implemented in the file Lab2java randomly generates 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.
BST.java
/**
* Problem: Perform rotations on tree1 to make it equivalent to tree2.
*/
public static void problem(BST tree1, BST tree2)
{
//if root is null then we've reached the end of a branch (base case)
if(tree1.root == null || tree2.root == null)
{
return;
}
//finding the node we need to rotate to tree1's root to match 2nd tree
Node match = tree1.find(tree2.getRootKey());
//loop to rotate matched node until it becomes tree1's new root or parent of the root
while(match != tree1.root && match != tree1.root.parent)
{
//parent node of matched node we need to get to new position
Node matchParent = match.parent;
//move matched node we want up the levels of tree1
//if matched node key is more than its parent then rotate parent left (which moves matched node up a level)
//otherwise rotate the parent right (still moves matched node up a level)
if(match.key > matchParent.key)
{
tree1.rotateL(matchParent);
}
else
{
tree1.rotateR(matchParent);
}
}
//if the matched node becomes the parent of tree1's root, in the loop above, then we need to
//change it so that the matched node is the root to line up with tree2
if(match == tree1.root.parent)
{
tree1.root = match;
}
// recursively go through left tree
// new BSTs to signify left subtrees
BST leftTree1 = new BST();
BST leftTree2 = new BST();
leftTree1.root = tree1.root.left;
leftTree2.root = tree2.root.left;
problem(leftTree1, leftTree2);
// recursively go through right tree
// new BSTs to signify right subtrees
BST rightTree1 = new BST();
BST rightTree2 = new BST();
rightTree1.root = tree1.root.right;
rightTree2.root = tree2.root.right;
problem(rightTree1, rightTree2);
}
// --------------------------------------------------------------------- // Do not change any of the code below!
private static final int LabNo = 2; private static final Random rng = new Random(190718);
private static boolean testProblem(int[][] testCase) { int[] arr1 = testCase[0]; int[] arr2 = testCase[1];
// --- Build tree ---
BST tree1 = new BST(); BST tree2 = new BST();
for (int i = 0; i < arr1.length; i++) { tree1.insert(arr1[i]); tree2.insert(arr2[i]); }
int[] pre2 = tree2.getPreOrder();
BST.problem(tree1, tree2);
// --- Verify tree. ---
int[] pre1 = tree1.getPreOrder(); int[] in1 = tree1.getInOrder();
if (in1.length != arr1.length) return false;
for (int i = 0; i < in1.length; i++) { if (in1[i] != i) return false; if (pre1[i] != pre2[i]) return false; }
return true; }
public static void main(String args[]) { System.out.println("CS 302 -- Lab " + LabNo); testProblems(1); }
private static void testProblems(int prob) { int noOfLines = 100000;
System.out.println("-- -- -- -- --"); System.out.println(noOfLines + " test cases for problem " + prob + ".");
boolean passedAll = true; long start = System.currentTimeMillis(); for (int i = 1; i <= noOfLines; i++) { boolean passed = false; boolean exce = false;
try { int[][] testCase = createProblem(i); passed = testProblem(testCase); } catch (Exception ex) { passed = false; exce = true; }
if (!passed) { System.out.println("Test " + i + " failed!" + (exce ? " (Exception)" : "")); passedAll = false; break; } } System.out.println((System.currentTimeMillis() - start) + " ms"); if (passedAll) { System.out.println("All test passed."); }
}
private static void shuffle(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { int rndInd = rng.nextInt(arr.length - i) + i; int tmp = arr[i]; arr[i] = arr[rndInd]; arr[rndInd] = tmp; } }
private static int[][] createProblem(int testNo) { int size = rng.nextInt(Math.min(200, testNo)) + 1;
int[] numbers1 = new int[size]; int[] numbers2 = new int[size];
for (int i = 0; i < size; i++) { numbers1[i] = i; numbers2[i] = i; }
shuffle(numbers1); shuffle(numbers2);
return new int[][] { numbers1, numbers2 }; } }
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