I need the output to match the one provided below while not changing the TestTreeADT.java and only
Question:
I need the output to match the one provided below while not changing the TestTreeADT.java and only changing the ArrayTree.java file.
public class TestTreeADT {
public static void main(String[] args) {
ArrayTree
ArrayTree
int val = 1;
System.out.println("*** Test ArrayTree ADT ***\n");
testcase("a1.isEmpty()", a1.isEmpty() ? 1 : 0, 1);
int pos = a1.addRoot(val++);
for (int i = 0; i < 10; i++) {
a1.addChild(pos, 0, val++);
a1.addChild(pos, 1, val++);
pos++;
}
testcase("a1.root()", a1.root(), 0);
testcase("a1.parent()", a1.parent(7), 3);
testcase("a1.child()", a1.child(10, 1), 22);
testcase("a1.size()", a1.size(), 21);
testcase("a1.isEmpty()", a1.isEmpty() ? 1 : 0, 0);
testcase("a1.get()", a1.get(3), 4);
testcase("a1.getChild()", a1.getChild(6, 1), 15);
System.out.println("Small Tree: " + a1 +"\n");
testcase("a2.isEmpty()", a2.isEmpty() ? 1 : 0, 1);
pos = a2.addRoot(val++);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 5; j++) {
a2.addChild(pos, j, val++);
}
pos++;
}
testcase("a2.root()", a2.root(), 0);
testcase("a2.parent()", a2.parent(27), 5);
testcase("a2.child()", a2.child(14, 4), 75);
testcase("a2.size()", a2.size(), 51);
testcase("a2.isEmpty()", a2.isEmpty() ? 1 : 0, 0);
testcase("a2.get()", a2.get(18), 40);
testcase("a2.getChild()", a2.getChild(9, 0), 68);
System.out.println("Large Tree: " + a2);
}
private static void testcase(String description, int actual, int expected) {
if (actual == expected) {
System.out.println("Pass: " + description);
} else {
System.out.println("Fail: " + description + ", expected " +
expected + ", got " + actual);
}
}
}
class ArrayTree
public E[] a;
int count;
int size;
int order;
ArrayTree()
{
this.order=2;
this.size=1000;
this.count=0;
this.a =(E[]) new Object[1000];
}
ArrayTree(int o,int s)
{
this.order=o;
this.size=s;
this.count=0;
this.a =(E[]) new Object[size];
}
public int root()
{
return 0;
}
public int parent(int p)
{
int m=p%this.order;
int pos=(p-m)/this.order;
return pos;
}
public E child(E p,int c)
{
int i,pos=0;
for(i=0;i { if(a[i]==p) { break; } } if(i { pos=this.order*i+c+1; } return this.a[pos]; } public int size() { return this.count; } public boolean isEmpty() { if(this.count==0) { return true; } return false; } public int addRoot(E e) { this.a[0]=e; this.count++; return 0; } public E get(int pos) { return this.a[pos]; } public int addChild(int p,int c, E e) { int pos=order*p+c+1; this.a[pos]=e; this.count++; return pos; } { int pos=order*p+c+1; return a[pos]; } public void toString1() { System.out.print("[ArrayTree: order= "+this.order+", count= "+this.count+", size= "+this.size+", array={"); for(int i=0;i System.out.print("- - - - }]\n"); } }
public E getChild(int p,int c)
System.out.print(this.a[i]+" ");
}
Java An Introduction To Problem Solving And Programming
ISBN: 9780134462035
8th Edition
Authors: Walter Savitch