Question: HELP IN JAVA: Make the TreeInsert and the Accesses methods Template methods. Add a (template) method to calculate the depth of a node (note the

HELP IN JAVA: Make the TreeInsert and the Accesses methods Template methods. 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). Add a (template) method to calculate the height of a tree.This is defined as the maximum depth. An empty tree is defined as having height -1, a tree with just a root has height 0 (because the root has depth 0). I cannot think of an easy non-recursive way to do this because you have to look at the depth of each node, and if you don't know what is in the tree that's difficult (you will need to modify a traversal, which is recursive anyway). Perhaps the best way to think about this IS recursively. If the tree is empty, return 0. Otherwise consider the tree rooted at the Left of the root, and the one rooted at the right. If you know the heights of these, choose the biggest height and add 1 (for the root).

public static void TreeInsert(BinaryTreeNode curr, T 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 root, int element) { //return 0 if (root == null) { return 0; }

// return 1 if (root.getElement() == element) { return 1; }

//if element is less than go to left and add 1 if (root.getElement() > element) { return 1 + Accesses(root.left, element); }

//if element is greater then go to right and add 1 return 1 + Accesses(root.right, element); } }

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!