Question: Help in Java: 1. Make the TreeInsert and the Accesses methods Template methods. You will need to remember this syntax public static void TreeInsert(BinaryTreeNode curr,

Help in Java:

1. Make the TreeInsert and the Accesses methods Template methods. You will need to remember this syntax

public static void TreeInsert(BinaryTreeNode curr, T num)

2. 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)

3. 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).

TreeInsert method

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(); } } } }

Accesses Methods: public static int Accesses(BinaryTreeNode root,int element){ if (root==null) return 0; if (root.getElement()==element) return 1; if (root.getElement()>element) return 1+Accesses(root.left,element); else return 1+Accesses(root.right,element); }

public static int Accesses(BinaryTreeNode s,String str){ int counts = 1; while(s !=null){ String currString = (String)s.getValue(); if(str.compareTo(currString)<0){ counts++; s = s.getLeft(); } else if(str.compareTo(currString)>0){ counts++; s = s.getRight(); } else{ break;

}

}

return counts;

}

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!