Question: HELP IN JAVA: Make the TreeInsert and the Accesses methods Template methods. You will need to remember this syntax. public static void TreeInsert(BinaryTreeNode curr, T

HELP IN JAVA: Make the TreeInsert and the Accesses methods Template methods. You will need to remember this syntax.

public static void TreeInsert(BinaryTreeNode curr, T num)

Add a (template) method to calculate the depth of a node (note the depth of a node is defined as the number of edges from the root down to that node, so if there are k assesses, the depth is k-1). So you don't have to reinvent the wheel here - you can just call a method you have already written)

Here is the BinaryTreeNode class:

public class BinaryTreeNode { private T element; private BinaryTreeNode left, right;

/** * Creates a new tree node with the specified data. * * @param obj the element that will become a part of the new tree node */ BinaryTreeNode (T obj) { element = obj; left = null; right = null; }

public T getValue(){ return element; }

public T getElement() { return element; }

public void setElement(T element) { this.element = element; }

public BinaryTreeNode getLeft() { return left; }

public void setLeft(BinaryTreeNode left) { this.left = left; }

public BinaryTreeNode getRight() { return right; }

public void setRight(BinaryTreeNode right) { this.right = right; }

}

These are int methods I need to change them into templates that can be used with any type:

public static void TreeInsert(BinaryTreeNode curr, Integer num) { BinaryTreeNode b=new BinaryTreeNode(num); while (curr != null) { int currValue= (int)curr.getValue(); if(num

} else { if (curr.getRight() == null) { curr.setRight(b); break; } else { curr = curr.getRight(); } } } }

public static int Accesses(BinaryTreeNode curr, Integer num) { int currValue= (int)curr.getValue(); if( currValue == 0){ return 0; } else if(currValue == num){ return 1; } else if(currValue > num){ return 1 + Accesses(curr.getLeft(),num); } else{ return 1+ Accesses(curr.getRight(), num); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!