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
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]+" ");
}
*** Test Array Tree ADT *** Pass: al.isEmpty() Pass: a1.root() Pass: a1.parent() Pass: a1.child() Pass: al.size() Pass: al.isEmpty() Pass: al.get() Pass: al.getChild() Small Tree: [ArrayTree: order=2, count=21, size=25, array={1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Pass: a2.isEmpty() Pass: a2.root() Pass: a2.parent() }] Fail: a2.child(), expected 75, got -1 Pass: a2.size() Pass: a2.isEmpty() Pass: a2.get() Pass: a2.getChild() Large Tree: [ArrayTree: order=5, count=51, size=55, array={22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 --- }]
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