Rewrite this program: does not have to major changes. Just one or two changes th
ID: 656170 • Letter: R
Question
Rewrite this program: does not have to major changes. Just one or two changes that give the same result.
int nbre = 200; // one can change this value
for(int i = 0; i < nbre; i++)
{
for(int j = 0; j < i; j++)
{
try
{
// check
if (! check(i, j, false))
{
System.out.println();
System.out.println("fail with check("+i+", "+j+")");
check(i, j, true);
}
}
catch(Exception e)
{
System.out.println();
System.err.println("exception with check("+i+", "+j+")");
e.printStackTrace();
}
}
}
public static boolean check(int nbre, int indexToRemove, boolean print)
{
// create avl tree
AVLTree<Integer> avlTree = new AVLTree<Integer>();
// insert value
for(int i = 0; i < nbre; i++)
avlTree.insert(i);
// remove index
avlTree.remove(indexToRemove);
// check if index is deleted
boolean result = true;
int count = 0;
for(int i = 0; i < nbre; i++)
{
if (!avlTree.contains(i))
{
count++;
result &= (indexToRemove == i);
if (print)
System.out.println(i + " not present");
}
}
return count == 1 && result;
}
Explanation / Answer
Write the following method in AVLTree class:
public void remove(int indexToRemove)
{
AVLNode<E> node = new AVLNode(indexToRemove);
remove(node.getElement(), rootAbove);
}
// File: AVLTreeTest.java
public class AVLTreeTest
{
public static void main(String[] args)
{
int nbre = 200; // one can change this value
for(int i = 0; i < nbre; i++)
{
for(int j = 0; j < i; j++)
{
try
{
// check
if(!check(i, j, false))
{
System.out.println();
System.out.println("fail with check(" + i + ", " + j + ")");
check(i, j, true);
}
}
catch(Exception e)
{
System.out.println();
System.err.println("exception with check(" + i + ", " + j + ")");
e.printStackTrace();
}
}
}
}
public static boolean check(int nbre, int indexToRemove, boolean print)
{
// create avl tree
AVLTree<Integer> avlTree = new AVLTree<Integer>();
// insert value
for(int i = 0; i < nbre; i++)
avlTree.insert(i);
// remove index
avlTree.remove(indexToRemove);
// check if index is deleted
boolean result = true;
int count = 0;
for(int i = 0; i < nbre; i++)
{
if(!avlTree.contains(i))
{
count++;
result &= (indexToRemove == i);
if(print)
System.out.println(i + " not present");
}
}
return count == 1 && result;
}
}
----------------------------------------------------------------------
OR
Write the following method in AVLTree class:
public AVLNode<E> getRoot()
{
return rootAbove;
}
// File: AVLTreeTest.java
public class AVLTreeTest
{
public static void main(String[] args)
{
int nbre = 200; // one can change this value
for(int i = 0; i < nbre; i++)
{
for(int j = 0; j < i; j++)
{
try
{
// check
if(!check(i, j, false))
{
System.out.println();
System.out.println("fail with check(" + i + ", " + j + ")");
check(i, j, true);
}
}
catch(Exception e)
{
System.out.println();
System.err.println("exception with check(" + i + ", " + j + ")");
e.printStackTrace();
}
}
}
}
public static boolean check(int nbre, int indexToRemove, boolean print)
{
// create avl tree
AVLTree<Integer> avlTree = new AVLTree<Integer>();
// insert value
for(int i = 0; i < nbre; i++)
avlTree.insert(i);
AVLNode<Integer> node = new AVLNode<Integer>(indexToRemove);
avlTree.remove(node.getElement(), avlTree.getRoot());
// check if index is deleted
boolean result = true;
int count = 0;
for(int i = 0; i < nbre; i++)
{
if(!avlTree.contains(i))
{
count++;
result &= (indexToRemove == i);
if(print)
System.out.println(i + " not present");
}
}
return count == 1 && result;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.