Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below is my code. All my test cases are still not passed. Can someone help me modify my code, so it passes all the tests?

image text in transcribedBelow is my code. All my test cases are still not passed. Can someone help me modify my code, so it passes all the tests? The solution must be iterative.

BST.java

/** * Problem: Perform rotations on tree1 to make it equivalent to tree2. */ public static void problem(BST tree1, BST tree2) { //base if (tree1.root == null || tree2.root == null) { return; } Stack s1 = new Stack(); Stack s2 = new Stack(); //Initialize stacks with root nodes of both trees if (tree1.root.key == tree2.root.key) { s1.push(tree1.root); s2.push(tree2.root); } while (!s1.isEmpty() && !s2.isEmpty()) { Node n1 = s1.pop(); Node n2 = s2.pop(); //match left subtrees if (n1.left != null && n2.left != null) { s1.push(n1.left); s2.push(n2.left); } else if (n1.left != null && n2.left == null) { tree1.rotateL(n1); } else if (n1.left == null && n2.left != null) { tree1.rotateR(n1); } //match right subtrees if (n1.right != null && n2.right != null) { s1.push(n1.right); s2.push(n2.right); } else if (n1.right != null && n2.right == null) { tree1.rotateR(n1); } else if (n1.right == null && n2.right != null) { tree1.rotateL(n1); } } }

// --------------------------------------------------------------------- // Do not change any of the code below!

private class Node { public Node left = null; public Node right = null; public Node parent = null;

public int key;

public Node(int key) { this.key = key; } }

private Node root = null;

public int getRootKey() { return root.key; }

private Node find(int key) { for (Node cur = root; cur != null;) { if (key cur.key { cur = cur.right; } }

return null; }

// x y // / \ / \ // a y => x c // / \ / \ // b c a b private void rotateL(Node xNode) { Node xPar = xNode.parent; boolean isRoot = xPar == null; boolean isLChild = !isRoot && xPar.left == xNode;

Node yNode = xNode.right; Node beta = yNode.left;

if (isRoot) root = yNode; else if (isLChild) xPar.left = yNode; else xPar.right = yNode;

yNode.parent = xPar; yNode.left = xNode;

xNode.parent = yNode; xNode.right = beta;

if (beta != null) beta.parent = xNode; }

// y x // / \ / \ // x c => a y // / \ / \ // a b b c private void rotateR(Node yNode) { Node yPar = yNode.parent; boolean isRoot = yPar == null; boolean isLChild = !isRoot && yPar.left == yNode;

Node xNode = yNode.left; Node beta = xNode.right;

if (isRoot) root = xNode; else if (isLChild) yPar.left = xNode; else yPar.right = xNode;

xNode.parent = yPar; xNode.right = yNode;

yNode.parent = xNode; yNode.left = beta;

if (beta != null) beta.parent = yNode; }

public void insert(int key) { if (root == null) { root = new Node(key); return; }

Node par = null;

for (Node node = root; node != null;) { par = node;

if (key node.key) { node = node.right; } else // key == node.key { // Nothing to do, because no value to update. return; } }

// Create node and set pointers. Node newNode = new Node(key); newNode.parent = par;

if (key

public int[] getInOrder() { if (root == null) return new int[] { };

Stack stack = new Stack(); ArrayList orderList = new ArrayList();

for (Node node = root;;) { if (node == null) { if (stack.empty()) break;

node = stack.pop(); orderList.add(node.key); node = node.right; } else { stack.push(node); node = node.left; } }

int[] order = new int[orderList.size()]; for (int i = 0; i

return order; }

public int[] getPreOrder() { if (root == null) return new int[] { };

Stack stack = new Stack(); ArrayList orderList = new ArrayList();

for (Node node = root;;) { 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

return order; } }

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 Lab 2 java 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Moving Objects Databases

Authors: Ralf Hartmut Güting, Markus Schneider

1st Edition

0120887991, 978-0120887996

More Books

Students also viewed these Databases questions