Question
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
Make the TreeInsert and the Accesses methods Template methods. You will need to remember this syntax.
public static
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
/** * 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
public void setLeft(BinaryTreeNode
public BinaryTreeNode
public void setRight(BinaryTreeNode
}
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
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