Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Finish the DepthCalculatorVisitor and the HeightCalculatorVisitor. I do rate the

ID: 3823061 • Letter: F

Question

Finish the DepthCalculatorVisitor and the HeightCalculatorVisitor.

I do rate the answer so please do it seriously don't just copy and paste some code which is totally NOT RELEATED to this question.

Here is the code the needed for this question.

---------------------------------------------

AVLEntry

package edu.buffalo.cse116;

public class AVLEntry<E> {
/** Tree's element which is stored within this Node. */
private E element;

/** Left child of the current Node. */
private AVLEntry<E> left;
/** Right child of the current Node. */
private AVLEntry<E> right;
/** Parent in the binary tree for the current Node. */
private AVLEntry<E> parent;
/** Largest possible number of descendants until a leaf is reached. */
private int height;
/** Number of ancestors between node and tree's root. */
private int depth;

/**
* Initializes this Entry object. This default constructor is defined for future expansion purposes.
*/
public AVLEntry() {}

/** Return the value stored as the node's height. */
public int getHeight() {
return height;
}

/** Sets the height of this node. */
public void setHeight(int height) {
this.height = height;
}

/** Returns the value stored as the node's depth. */
public int getDepth() {
return depth;
}

/** Sets the depth for this node. */
public void setDepth(int depth) {
this.depth = depth;
}

/**
* Initializes this Entry object from element and parent.
*/
public AVLEntry(E element, AVLEntry<E> parent) {
this.element = element;
this.parent = parent;
}

/** Return the element stored in this node. */
public E getElement() {
return element;
}

/** Specify a new element to be stored at this node. */
public void setElement(E element) {
this.element = element;
}

/** Get the node's left child. */
public AVLEntry<E> getLeft() {
return left;
}

/** Specify a node to be the left child of the current node. */
public void setLeft(AVLEntry<E> left) {
this.left = left;
}

/** Get the node's right child. */
public AVLEntry<E> getRight() {
return right;
}

/** Specify a node to be the right child of the current node. */
public void setRight(AVLEntry<E> right) {
this.right = right;
}

/** Get the node's parent in the tree. This is null if the node is a root. */
public AVLEntry<E> getParent() {
return parent;
}

/** Specify a node to be the parent of the current node. */

public void setParent(AVLEntry<E> parent) {
this.parent = parent;
}

/**
* Implement the node's portion of the Visitor pattern. This allows the TreeVisitor to work and return the result of
* the traversal.
*
* @param visitor TreeVisitor instance doing the work.
* @param data Data being passed along as part of this visiting traversal.
* @return The result returned by the TreeVisitor.
*/
public int apply(TreeVisitor<E> visitor, int data) {
if ((left == null) && (right == null)) {
return visitor.visitLeaf(this, data);
} else {
return visitor.visitInterior(this, data);
}
}

}

TreeVisitor

package edu.buffalo.cse116;

/**
* Interface implemented by Visitor classes. Each class defines the visitor for a generic binary tree. Classes will need
* to implement this to define the specific functionality they will define.
*
* @author Matthew Hertz
* @param <E> Type of data held in each node in the binary tree.
*/
public interface TreeVisitor<E> {
public int visitLeaf(AVLEntry<E> leaf, int message);

public int visitInterior(AVLEntry<E> node, int message);
}

DepthCalcultorVisitor

package edu.buffalo.cse116;

/**
* When correctly implemented, this class will complete the Visitor pattern and create a system which calculates and
* sets node's depths. It is important that each of the methods only calculate and set the depth in the AVLEntry
* provided as a parameter.
*
* @author Matthew Hertz
*/
public class DepthCalculatorVisitor<E> implements TreeVisitor<E> {

/**
* Sets the depth of the leaf node to the value that had been calculated.
*/
@Override
public int visitLeaf(AVLEntry<E> leaf, int depth) {

}

@Override
public int visitInterior(AVLEntry<E> node, int depth) {

}

}

HeightCalcultorVisitor

package edu.buffalo.cse116;

/**
* When correctly implemented, this class will complete the Visitor pattern and create a system which calculates and
* sets node's heights. It is important that each of the methods only calculate and set the height in the AVLEntry
* provided as a parameter.
*
* @author Matthew Hertz
*/
public class HeightCalculatorVisitor<E> implements TreeVisitor<E> {

/**
* Sets the height of the leaf node and then returns this height. By definition, leaves have a height of 0.
*/
@Override
public int visitLeaf(AVLEntry<E> leaf, int data) {

}

@Override
public int visitInterior(AVLEntry<E> node, int data) {

}

}

The test use to check

package edu.buffalo.cse116;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;

import org.junit.Before;
import org.junit.Test;

public class TreeVisitorsTest {

private AVLEntry<Character>[] nodes;
private TreeVisitor<Character> depthCalc;
private TreeVisitor<Character> heightCalc;

@SuppressWarnings("unchecked")
@Before
public void setUp() {
nodes = new AVLEntry[11];
for (int i = 0; i < nodes.length; i++ ) {
nodes[i] = new AVLEntry<>();
nodes[i].setElement((char) ('A' + i));
}

// Setup the left and right children in the tree
nodes[5].setLeft(nodes[3]);
nodes[5].setRight(nodes[4]);

nodes[4].setLeft(nodes[10]);

nodes[2].setLeft(nodes[0]);
nodes[2].setRight(nodes[1]);

nodes[6].setLeft(nodes[2]);
nodes[6].setRight(nodes[5]);

nodes[8].setRight(nodes[9]);

nodes[7].setLeft(nodes[6]);
nodes[7].setRight(nodes[8]);

// Setup the parents in the tree
nodes[6].setParent(nodes[7]);
nodes[8].setParent(nodes[7]);
nodes[9].setParent(nodes[8]);
nodes[5].setParent(nodes[6]);
nodes[2].setParent(nodes[6]);
nodes[0].setParent(nodes[2]);
nodes[1].setParent(nodes[2]);
nodes[10].setParent(nodes[4]);
nodes[3].setParent(nodes[5]);
nodes[4].setParent(nodes[5]);

depthCalc = new DepthCalculatorVisitor<>();
heightCalc = new HeightCalculatorVisitor<>();
}

@Test
public final void testDepthLeaf() {
depthCalc.visitLeaf(nodes[10], 4);
assertNull("DepthCalculatorVisitor.visitLeaf() should not update leaf's left child", nodes[10].getLeft());
assertNull("DepthCalculatorVisitor.visitLeaf() should not update leaf's right child", nodes[10].getRight());
assertSame("DepthCalculatorVisitor.visitLeaf() should not change leaf's parent", nodes[4], nodes[10].getParent());
assertEquals("DepthCalculatorVisitor.visitLeaf() should call setDepth() on leaf", 4, nodes[10].getDepth());
assertEquals("DepthCalculatorVisitor.visitLeaf() should NOT call any methods with leaf.getParent()", 0,
nodes[4].getDepth());
depthCalc.visitLeaf(nodes[9], 2);
assertNull("DepthCalculatorVisitor.visitLeaf() should not update leaf's left child", nodes[9].getLeft());
assertNull("DepthCalculatorVisitor.visitLeaf() should not update leaf's right child", nodes[9].getRight());
assertSame("DepthCalculatorVisitor.visitLeaf() should not change leaf's parent", nodes[8], nodes[9].getParent());
assertEquals("DepthCalculatorVisitor.visitLeaf() should call setDepth() on leaf", 2, nodes[9].getDepth());
}

@Test
public final void testHeightLeaf() {
int actual = heightCalc.visitLeaf(nodes[10], 2);
assertNull("HeightCalculatorVisitor.visitLeaf() should not update leaf's left child", nodes[10].getLeft());
assertNull("HeightCalculatorVisitor.visitLeaf() should not update leaf's right child", nodes[10].getRight());
assertSame("HeightCalculatorVisitor.visitLeaf() should not change leaf's parent", nodes[4], nodes[10].getParent());
assertEquals("HeightCalculatorVisitor.visitLeaf() should call setHeight() on leaf", 0, nodes[10].getHeight());
assertEquals("HeightCalculatorVisitor.visitLeaf() should NOT call any methods with leaf.getParent()", 0,
nodes[4].getHeight());
assertEquals("HeightCalculatorVisitor.visitLeaf() should return the height of leaf", 0, actual);
}

@Test
public final void testDepthInteriorLeftChild() {
try {
depthCalc.visitInterior(nodes[4], 3);
assertEquals("DepthCalculatorVisitor.visitInterior() should call setDepth() on node", 3, nodes[4].getDepth());
assertSame("DepthCalculatorVisitor.visitInterior() should not call node.setLeft()", nodes[10],
nodes[4].getLeft());
assertNull("DepthCalculatorVisitor.visitInterior() should not call node.setRight()", nodes[4].getRight());
assertEquals("DepthCalculatorVisitor.visitInterior() should increase depth by 1 and then call apply() on all non-null children ",
4, nodes[10].getDepth());
assertSame("DepthCalculatorVisitor.visitInterior() should not call node.setParent()", nodes[5],
nodes[4].getParent());
} catch (NullPointerException npe) {
fail("DepthCalculatorVisitor.visitInterior() should only call apply() with non-null children.");
}
}

@Test
public final void testHeightLeftChild() {
try {
int actual = heightCalc.visitInterior(nodes[4], 2);
assertSame("HeightCalculatorVisitor.visitInterior() should not call node.setLeft()", nodes[10],
nodes[4].getLeft());
assertNull("HeightCalculatorVisitor.visitInterior() should not call node.setRight()", nodes[4].getRight());
assertSame("HeightCalculatorVisitor.visitInterior() should not call node.setParent()", nodes[5],
nodes[4].getParent());
assertEquals("HeightCalculatorVisitor.visitInterior() should call apply() on any non-null children", 0,
nodes[10].getHeight());
assertEquals("HeightCalculatorVisitor.visitInterior() should setHeight() with 1 + height of the taller child", 1,
nodes[4].getHeight());
assertEquals("HeightCalculatorVisitor.visitInterior() should NOT call any methods with node.getParent()", 0,
nodes[5].getHeight());
assertEquals("HeightCalculatorVisitor.visitInterior() should return the height of node", 1, actual);
} catch (NullPointerException npe) {
fail("HeightCalculatorVisitor.visitInterior() should only call apply() with non-null children.");
}
}

@Test
public final void testDepthInteriorRightChild() {
try {
depthCalc.visitInterior(nodes[8], 1);
assertEquals("DepthCalculatorVisitor.visitInterior() should call setDepth() on node", 1, nodes[8].getDepth());
assertNull("DepthCalculatorVisitor.visitInterior() should not call node.setLeft()", nodes[8].getLeft());
assertSame("DepthCalculatorVisitor.visitInterior() should not call node.setRight()", nodes[9],
nodes[8].getRight());
assertEquals("DepthCalculatorVisitor.visitInterior() should increase depth by 1 and then call apply() on all non-null children ",
2, nodes[9].getDepth());
assertSame("DepthCalculatorVisitor.visitInterior() should not call node.setParent()", nodes[7],
nodes[8].getParent());
} catch (NullPointerException npe) {
fail("DepthCalculatorVisitor.visitInterior() should only call apply() with non-null children.");
}
}

@Test
public final void testHeightRightChild() {
try {
int actual = heightCalc.visitInterior(nodes[8], 2);
assertNull("HeightCalculatorVisitor.visitInterior() should not call node.setLeft()", nodes[8].getLeft());
assertSame("HeightCalculatorVisitor.visitInterior() should not call node.setRight()", nodes[9],
nodes[8].getRight());
assertSame("HeightCalculatorVisitor.visitInterior() should not call node.setParent()", nodes[7],
nodes[8].getParent());
assertEquals("HeightCalculatorVisitor.visitInterior() should call apply() on any non-null children", 0,
nodes[9].getHeight());
assertEquals("HeightCalculatorVisitor.visitInterior() should setHeight() with 1 + height of the taller child", 1,
nodes[8].getHeight());
assertEquals("HeightCalculatorVisitor.visitInterior() should NOT call any methods with node.getParent()", 0,
nodes[7].getHeight());
assertEquals("HeightCalculatorVisitor.visitInterior() should return the height of node", 1, actual);
} catch (NullPointerException npe) {
fail("HeightCalculatorVisitor.visitInterior() should only call apply() with non-null children.");
}
}

@Test
public final void testDepthInteriorBothChildren() {
try {
depthCalc.visitInterior(nodes[2], 2);
assertEquals("DepthCalculatorVisitor.visitInterior() should call setDepth() on node", 2, nodes[2].getDepth());
assertSame("DepthCalculatorVisitor.visitInterior() should not call node.setLeft()", nodes[0], nodes[2].getLeft());
assertSame("DepthCalculatorVisitor.visitInterior() should not call node.setRight()", nodes[1],
nodes[2].getRight());
assertEquals("DepthCalculatorVisitor.visitInterior() should increase depth by 1 and then call apply() on all non-null children ",
3, nodes[0].getDepth());
assertEquals("DepthCalculatorVisitor.visitInterior() should increase depth by 1 and then call apply() on all non-null children ",
3, nodes[1].getDepth());
assertSame("DepthCalculatorVisitor.visitInterior() should not call node.setParent()", nodes[6],
nodes[2].getParent());
} catch (NullPointerException npe) {
fail("DepthCalculatorVisitor.visitInterior() should call apply() and not call visitLeaf() or visitInterior() directly.");
}
}

@Test
public final void testHeightBothChildren() {
try {
int actual = heightCalc.visitInterior(nodes[2], 2);
assertSame("HeightCalculatorVisitor.visitInterior() should not call node.setLeft()", nodes[0],
nodes[2].getLeft());
assertSame("HeightCalculatorVisitor.visitInterior() should not call node.setRight()", nodes[1],
nodes[2].getRight());
assertSame("HeightCalculatorVisitor.visitInterior() should not call node.setParent()", nodes[6],
nodes[2].getParent());
assertEquals("HeightCalculatorVisitor.visitInterior() should call apply() on any non-null children", 0,
nodes[0].getHeight());
assertEquals("HeightCalculatorVisitor.visitInterior() should call apply() on any non-null children", 0,
nodes[1].getHeight());
assertEquals("HeightCalculatorVisitor.visitInterior() should setHeight() with 1 + height of the taller child", 1,
nodes[2].getHeight());
assertEquals("HeightCalculatorVisitor.visitInterior() should NOT call any methods with node.getParent()", 0,
nodes[6].getHeight());
assertEquals("HeightCalculatorVisitor.visitInterior() should return the height of node", 1, actual);
} catch (NullPointerException npe) {
fail("HeightCalculatorVisitor.visitInterior() should only call apply() with non-null children.");
}
}

@Test
public final void testDepthFull() {
try {
depthCalc.visitInterior(nodes[7], 0);
assertEquals("DepthCalculatorVisitor.visitInterior() should call setDepth() on node and then call apply() on non-null children",
1, nodes[8].getDepth());
assertEquals("DepthCalculatorVisitor.visitInterior() should call setDepth() on node and then call apply() on non-null children",
1, nodes[6].getDepth());
assertEquals("DepthCalculatorVisitor.visitInterior() should call setDepth() on node and then call apply() on non-null children",
2, nodes[2].getDepth());
assertEquals("DepthCalculatorVisitor.visitInterior() should call setDepth() on node and then call apply() on non-null children",
2, nodes[5].getDepth());
assertEquals("DepthCalculatorVisitor.visitInterior() should call setDepth() on node and then call apply() on non-null children",
2, nodes[9].getDepth());
assertEquals("DepthCalculatorVisitor.visitInterior() should call setDepth() on node and then call apply() on non-null children",
3, nodes[0].getDepth());
assertEquals("DepthCalculatorVisitor.visitInterior() should call setDepth() on node and then call apply() on non-null children",
3, nodes[1].getDepth());
assertEquals("DepthCalculatorVisitor.visitInterior() should call setDepth() on node and then call apply() on non-null children",
3, nodes[3].getDepth());
assertEquals("DepthCalculatorVisitor.visitInterior() should call setDepth() on node and then call apply() on non-null children",
3, nodes[4].getDepth());
assertEquals("DepthCalculatorVisitor.visitInterior() should call setDepth() on node and then call apply() on non-null children",
4, nodes[10].getDepth());
} catch (NullPointerException npe) {
fail("DepthCalculatorVisitor.visitInterior() should only call apply() with non-null children.");
}
}

@Test
public final void testHeightFull() {
try {
heightCalc.visitInterior(nodes[7], 0);
assertEquals("HeightCalculatorVisitor.visitInterior() should call setHeight() on node and then call apply() on non-null children",
1, nodes[8].getHeight());
assertEquals("HeightCalculatorVisitor.visitInterior() should call setHeight() on node and then call apply() on non-null children",
3, nodes[6].getHeight());
assertEquals("HeightCalculatorVisitor.visitInterior() should call setHeight() on node and then call apply() on non-null children",
1, nodes[2].getHeight());
assertEquals("HeightCalculatorVisitor.visitInterior() should call setHeight() on node and then call apply() on non-null children",
2, nodes[5].getHeight());
assertEquals("HeightCalculatorVisitor.visitInterior() should call setHeight() on node and then call apply() on non-null children",
0, nodes[9].getHeight());
assertEquals("HeightCalculatorVisitor.visitInterior() should call setHeight() on node and then call apply() on non-null children",
0, nodes[0].getHeight());
assertEquals("HeightCalculatorVisitor.visitInterior() should call setHeight() on node and then call apply() on non-null children",
0, nodes[1].getHeight());
assertEquals("HeightCalculatorVisitor.visitInterior() should call setHeight() on node and then call apply() on non-null children",
0, nodes[3].getHeight());
assertEquals("HeightCalculatorVisitor.visitInterior() should call setHeight() on node and then call apply() on non-null children",
1, nodes[4].getHeight());
assertEquals("HeightCalculatorVisitor.visitInterior() should call setHeight() on node and then call apply() on non-null children",
0, nodes[10].getHeight());
} catch (NullPointerException npe) {
fail("HeightCalculatorVisitor.visitInterior() should only call apply() with non-null children.");
}
}
}

Explanation / Answer

Screenshot of the code:

AVLEntry.java:

public class AVLEntry<E> {

/** Tree's element which is stored within this Node. */

private char element;

/** Left child of the current Node. */

private AVLEntry<E> left;

/** Right child of the current Node. */

private AVLEntry<E> right;

/** Parent in the binary tree for the current Node. */

private AVLEntry<E> parent;

/** Largest possible number of descendants until a leaf is reached. */

private int height;

/** Number of ancestors between node and tree's root. */

private int depth;

/**

* Initializes this Entry object. This default constructor is defined for future expansion purposes.

*/

public AVLEntry()

{

}

/** Return the value stored as the node's height. */

public int getHeight() {

return height;

}

/** Sets the height of this node. */

public void setHeight(int height) {

this.height = height;

}

/** Returns the value stored as the node's depth. */

public int getDepth() {

return depth;

}

/** Sets the depth for this node. */

public void setDepth(int depth) {

this.depth = depth;

}

/**

* Initializes this Entry object from element and parent.

*/

public AVLEntry(E element, AVLEntry<E> parent) {

this.element = (char) element;

this.parent = parent;

}

/** Return the element stored in this node. */

public char getElement() {

return element;

}

/** Specify a new element to be stored at this node. */

public void setElement(char c) {

this.element = c;

}

/** Get the node's left child. */

public AVLEntry<E> getLeft() {

return left;

}

/** Specify a node to be the left child of the current node. */

public void setLeft(AVLEntry<E> left) {

this.left = left;

}

/** Get the node's right child. */

public AVLEntry<E> getRight() {

return right;

}

/** Specify a node to be the right child of the current node. */

public void setRight(AVLEntry<E> right) {

this.right = right;

}

/** Get the node's parent in the tree. This is null if the node is a root. */

public AVLEntry<E> getParent() {

return parent;

}

/** Specify a node to be the parent of the current node. */

public void setParent(AVLEntry<E> parent) {

this.parent = parent;

}

/**

* Implement the node's portion of the Visitor pattern.

//This allows the TreeVisitor to work and return the result of

* the traversal.

*

* @param visitor TreeVisitor instance doing the work.

* @param data Data being passed along as part of this visiting traversal.

* @return The result returned by the TreeVisitor.

*/

public int apply(TreeVisitor<E> visitor, int data)

{

if ((left == null) && (right == null))

{

return visitor.visitLeaf(this, data);

}

else

{

return visitor.visitInterior(this, data);

}

}

}

TreeVisitor.java:

/**

* Interface implemented by Visitor classes. Each class defines the

* visitor for a generic binary tree. Classes will need

* to implement this to define the specific functionality they will define.

*

* @author Matthew Hertz

* @param <E> Type of data held in each node in the binary tree.

*/

public interface TreeVisitor<E>

{

public int visitLeaf(AVLEntry<E> leaf, int message);

public int visitInterior(AVLEntry<E> node, int message);

}

DepthCalculatorVisitor.java:

/**

* When correctly implemented, this class will complete the Visitor

//pattern and create a system which calculates and

* sets node's depths. It is important that each of the methods

//only calculate and set the depth in the AVLEntry

* provided as a parameter.

*

* @author Matthew Hertz

*/

public class DepthCalculatorVisitor<E> implements TreeVisitor<E>

{

TreeVisitorsTest test;

/**

* Sets the depth of the leaf node to the value that had been calculated.

*/

@Override

public int visitLeaf(AVLEntry<E> leaf, int depth)

{

leaf.setDepth(depth);

return depth;

}

@Override

public int visitInterior(AVLEntry<E> node, int depth)

{

int depth1 = 0;

while(node.getParent()!=null)

{

node=node.getParent();

depth1++;

}

node.setDepth(depth1);

return depth1;

}

}

HeightCalculatorVisitor.java:

/**

* When correctly implemented, this class will complete the Visitor

* pattern and create a system which calculates and

* sets node's heights. It is important that each of the methods

* only calculate and set the height in the AVLEntry

* provided as a parameter.

*

* @author Matthew Hertz

*/

public class HeightCalculatorVisitor<E> implements TreeVisitor<E>

{

/**

* Sets the height of the leaf node and then returns this

* height. By definition, leaves have a height of 0.

*/

@Override

public int visitLeaf(AVLEntry<E> leaf, int data)

{

return leaf.getHeight();

}

@Override

public int visitInterior(AVLEntry<E> node, int data)

{

if (node==null)

return 0;

else

{

/* compute the depth of each subtree */

int lDepth = 1+visitInterior(node.getLeft(),data);

int rDepth = 1+visitInterior(node.getRight(),data);

/* use the larger one */

if (lDepth > rDepth)

return(lDepth);

else

return(rDepth);

}

}

}

TreeVisitorTest.java:

import static org.junit.Assert.assertEquals;

import static org.junit.Assert.assertNull;

import static org.junit.Assert.assertSame;

import static org.junit.Assert.fail;

public class TreeVisitorsTest<E>

{

public void setUp(AVLEntry<E>[] nodes)

{

for (int i = 0; i < nodes.length; i++ )

{

nodes[i] = new AVLEntry<>();

nodes[i].setElement((char) ('A' + i));

}

}

//test cases

public final void testDepthLeaf(TreeVisitor<Character> depthCalc, AVLEntry<Character>[] nodes)

{

depthCalc.visitLeaf(nodes[10], 4);

assertNull("DepthCalculatorVisitor.visitLeaf() should not update leaf's left child",

nodes[10].getLeft());

assertNull("DepthCalculatorVisitor.visitLeaf() should not update leaf's right child",

nodes[10].getRight());

assertSame("DepthCalculatorVisitor.visitLeaf() should not change leaf's parent",

nodes[4], nodes[10].getParent());

assertEquals("DepthCalculatorVisitor.visitLeaf() should call setDepth() on leaf", 4,

nodes[10].getDepth());

assertEquals("DepthCalculatorVisitor.visitLeaf() should NOT call any methods with leaf.getParent()",

0,nodes[4].getDepth());

depthCalc.visitLeaf(nodes[9], 2);

assertNull("DepthCalculatorVisitor.visitLeaf() should not update leaf's left child",

nodes[9].getLeft());

assertNull("DepthCalculatorVisitor.visitLeaf() should not update leaf's right child",

nodes[9].getRight());

assertSame("DepthCalculatorVisitor.visitLeaf() should not change leaf's parent",

nodes[8], nodes[9].getParent());

assertEquals("DepthCalculatorVisitor.visitLeaf() should call setDepth() on leaf",

2, nodes[9].getDepth());

}

public final void testHeightLeaf(TreeVisitor<Character> heightCalc, AVLEntry<Character>[] nodes) {

int actual = heightCalc.visitLeaf(nodes[10], 2);

assertNull("HeightCalculatorVisitor.visitLeaf() should not update leaf's left child",

nodes[10].getLeft());

assertNull("HeightCalculatorVisitor.visitLeaf() should not update leaf's right child",

nodes[10].getRight());

assertSame("HeightCalculatorVisitor.visitLeaf() should not change leaf's parent",

nodes[4], nodes[10].getParent());

assertEquals("HeightCalculatorVisitor.visitLeaf() should call setHeight() on leaf",

0, nodes[10].getHeight());

assertEquals("HeightCalculatorVisitor.visitLeaf() should NOT call any methods with leaf.getParent()", 0,

nodes[4].getHeight());

assertEquals("HeightCalculatorVisitor.visitLeaf() should return the height of leaf",

0, actual);

}

public final void testDepthInteriorLeftChild(TreeVisitor<E> depthCalc, AVLEntry<E>[] nodes) {

try {

int depth=depthCalc.visitInterior(nodes[4], 3);

nodes[4].setDepth(depth);

assertEquals("DepthCalculatorVisitor.visitInterior() should call setDepth() on node",

3, nodes[4].getDepth());

assertSame("DepthCalculatorVisitor.visitInterior() should not call node.setLeft()",

nodes[10],nodes[4].getLeft());

assertNull("DepthCalculatorVisitor.visitInterior() should not call node.setRight()",

nodes[4].getRight());

assertEquals("DepthCalculatorVisitor.visitInterior() should increase depth by 1 "

+"and then call apply() on all non-null children ",

4, nodes[10].getDepth());

assertSame("DepthCalculatorVisitor.visitInterior() should not call node.setParent()",

nodes[5],nodes[4].getParent());

} catch (NullPointerException npe) {

fail("DepthCalculatorVisitor.visitInterior() should only call apply() with non-null children.");

}

}

public final void testHeightLeftChild(TreeVisitor<Character> heightCalc, AVLEntry<Character>[] nodes) {

try {

int actual = heightCalc.visitInterior(nodes[4], 2)-1;

nodes[4].setHeight(actual);

assertSame("HeightCalculatorVisitor.visitInterior() should not call node.setLeft()",

nodes[10],nodes[4].getLeft());

assertNull("HeightCalculatorVisitor.visitInterior() should not call node.setRight()",

nodes[4].getRight());

assertSame("HeightCalculatorVisitor.visitInterior() should not call node.setParent()",

nodes[5],nodes[4].getParent());

assertEquals("HeightCalculatorVisitor.visitInterior() should call apply() on "

+"any non-null children", 0, nodes[10].getHeight());

assertEquals("HeightCalculatorVisitor.visitInterior() should setHeight()"+

" with 1 + height of the taller child", 1, nodes[4].getHeight());

assertEquals("HeightCalculatorVisitor.visitInterior() should "

+"NOT call any methods with node.getParent()", 0,nodes[5].getHeight());

assertEquals("HeightCalculatorVisitor.visitInterior() should return the height of node", 1, actual);

}

catch(NullPointerException npe1)

{

fail("HeightCalculatorVisitor.visitInterior() should only call apply() with non-null children.");

}

}

public final void testDepthInteriorRightChild(TreeVisitor<Character> depthCalc, AVLEntry<Character>[] nodes) {

try {

int depth=depthCalc.visitInterior(nodes[8], 1);

nodes[8].setDepth(depth);

assertEquals("DepthCalculatorVisitor.visitInterior() should call setDepth() on node",

1, nodes[8].getDepth());

assertNull("DepthCalculatorVisitor.visitInterior() should not call node.setLeft()",

nodes[8].getLeft());

assertSame("DepthCalculatorVisitor.visitInterior() should not call node.setRight()",

nodes[9],nodes[8].getRight());

assertEquals("DepthCalculatorVisitor.visitInterior() should increase depth by 1 and "

+"then call apply() on all non-null children ",2, nodes[9].getDepth());

assertSame("DepthCalculatorVisitor.visitInterior() should not call node.setParent()", nodes[7],

nodes[8].getParent());

} catch (NullPointerException npe) {

fail("DepthCalculatorVisitor.visitInterior() should only call apply() with non-null children.");

}

}

public final void testHeightRightChild(TreeVisitor<Character> heightCalc, AVLEntry<Character>[] nodes) {

try {

int actual = heightCalc.visitInterior(nodes[8], 2)-1;

nodes[8].setHeight(actual);

assertNull("HeightCalculatorVisitor.visitInterior() should not call node.setLeft()",

nodes[8].getLeft());

assertSame("HeightCalculatorVisitor.visitInterior() should not call node.setRight()",

nodes[9], nodes[8].getRight());

assertSame("HeightCalculatorVisitor.visitInterior() should not call node.setParent()",

nodes[7],nodes[8].getParent());

assertEquals("HeightCalculatorVisitor.visitInterior() should call apply() on any non-null children", 0,

nodes[9].getHeight());

assertEquals("HeightCalculatorVisitor.visitInterior() should setHeight() "

+"with 1 + height of the taller child", 1, nodes[8].getHeight());

assertEquals("HeightCalculatorVisitor.visitInterior() should NOT call any methods "

+"with node.getParent()", 0, nodes[7].getHeight());

assertEquals("HeightCalculatorVisitor.visitInterior() should return the height of node", 1, actual);

} catch (NullPointerException npe) {

fail("HeightCalculatorVisitor.visitInterior() should only call apply() with non-null children.");

}

}

public final void testDepthInteriorBothChildren(TreeVisitor<Character> depthCalc, AVLEntry<Character>[] nodes) {

try {

int depth=depthCalc.visitInterior(nodes[2], 2);

nodes[2].setDepth(depth);

assertEquals("DepthCalculatorVisitor.visitInterior() should call setDepth() on node",

2, nodes[2].getDepth());

assertSame("DepthCalculatorVisitor.visitInterior() should not call node.setLeft()",

nodes[0], nodes[2].getLeft());

assertSame("DepthCalculatorVisitor.visitInterior() should not call node.setRight()",

nodes[1], nodes[2].getRight());

assertEquals("DepthCalculatorVisitor.visitInterior() should increase depth"+

" by 1 and then call apply() on all non-null children ",3, nodes[0].getDepth());

assertEquals("DepthCalculatorVisitor.visitInterior() should increase depth by"

+" 1 and then call apply() on all non-null children ", 3, nodes[1].getDepth());

assertSame("DepthCalculatorVisitor.visitInterior() should not call node.setParent()", nodes[6],

nodes[2].getParent());

} catch (NullPointerException npe) {

fail("DepthCalculatorVisitor.visitInterior() should call apply() and not call"

+" visitLeaf() or visitInterior() directly.");

}

}

@SuppressWarnings("unchecked")

public static <E> void main(String[] args)

{

AVLEntry<E>[] nodes = new AVLEntry[11];

TreeVisitor<Character> depthCalc;

TreeVisitor<Character> heightCalc;

depthCalc = new DepthCalculatorVisitor<>();

heightCalc = new HeightCalculatorVisitor<>();

TreeVisitorsTest TV = new TreeVisitorsTest();

TV.setUp(nodes);

// Setup the left and right children in the tree

nodes[5].setLeft(nodes[3]);

nodes[5].setRight(nodes[4]);

nodes[4].setLeft(nodes[10]);

nodes[2].setLeft(nodes[0]);

nodes[2].setRight(nodes[1]);

nodes[6].setLeft(nodes[2]);

nodes[6].setRight(nodes[5]);

nodes[8].setRight(nodes[9]);

nodes[7].setLeft(nodes[6]);

nodes[7].setRight(nodes[8]);

// Setup the parents in the tree

nodes[6].setParent(nodes[7]);

nodes[8].setParent(nodes[7]);

nodes[9].setParent(nodes[8]);

nodes[5].setParent(nodes[6]);

nodes[2].setParent(nodes[6]);

nodes[0].setParent(nodes[2]);

nodes[1].setParent(nodes[2]);

nodes[10].setParent(nodes[4]);

nodes[3].setParent(nodes[5]);

nodes[4].setParent(nodes[5]);

//set depth

nodes[0].setDepth(3);

nodes[1].setDepth(3);

TV.testDepthLeaf(depthCalc,nodes);

TV.testHeightLeaf(heightCalc,nodes);

TV.testDepthInteriorLeftChild(depthCalc,nodes);

TV.testHeightLeftChild(heightCalc,nodes);

TV.testDepthInteriorRightChild(depthCalc,nodes);

TV.testHeightRightChild(heightCalc,nodes);

TV.testDepthInteriorBothChildren(depthCalc,nodes);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote