I need the help in 2,4,5 how can i implemented with java code ?
Show transcribed image textWrite each of the following methods inside the BST Class (i) public boolean isFull() A method (non-recursive) that returns true if the tree is full. So you can make use of your getCount() and getHeight() methods from Lab08. You can also use Math.pow() method to computer 2^h. (ii) protected boolean is Decision Tree BSTNode p) A recursive method that returns true if the tree referenced by p is is a decision tree. (iii) protected boolean isBalanced (BSTNode P) A recursive method that returns true if the tree referenced by p is balanced. (a) The difference between the heights of its left and right sub-trees is less than 2. (b) Both its left and right sub-trees are balanced. (iv) protected int getPathLength(T el, BSTNode p) A recursive method that returns the length of the path from the root of the tree referenced by p to the node containing el. (v) Public void printPath (T el, BSTNode p) A recursive method that prints all the nodes in the path from the root of the tree referenced by p to the node containing el.
Expert Answer
public class BSTNode<E extends Comparable<E>> {
/** Tree’s element which is stored within this Node. */
protected E element;
/** Left child of the current Node. */
protected BSTNode<E> left;
/** Right child of the current Node. */
protected BSTNode<E> right;
/** Parent in the binary tree for the current Node. */
protected BSTNode<E> parent;
/**
* Creates a new BSTNode object with the specified element and parent
* BSTNode.
*
* @param element
* Data to be held in this BSTNode.
* @param parent
* Parent for this entry within the binary tree.
*/
public BSTNode(E element, BSTNode<E> parent) {
this.element = element;
this.parent = parent;
}
/**
* Counts the number of nodes in this subtree. Note that, like many tree
* properties, this is defined recursively.
*/
public int subtreeSize() {
// Setup default values for when there is no subtree on the left & right
// (e.g., those children are null)
int leftChildSize = 0;
int rightChildSize = 0;
// If we have a left subtree
if (left != null) {
// Calculate the subtreeSize recursively.
leftChildSize = left.subtreeSize();
}
// Check if we have a right subtree
if (right != null) {
// Calculate the right’s subtreeSize recursively.
rightChildSize = right.subtreeSize();
}
// The total size of this subtree is 1 (for the current node) plus the
// results from each child.
return leftChildSize + rightChildSize + 1;
}
protected boolean isDecisionTree(BSTNode<E> p) {
// check current node
if (p == null || (p.left == null && p.right == null)) {
return true;
}
// check the childs
return isDecisionTree(p.left) && isDecisionTree(p.right);
}
private int height(BSTNode<E> node) {
if (node == null)
return 0;
/*
* If tree is not empty then height = 1 + max of left height and right
* heights
*/
return 1 + Math.max(height(node.left), height(node.right));
}
protected boolean isBalanced(BSTNode<E> p) {
if (p == null)
return true;
int leftHeight = height(p.left);
int rightHeight = height(p.right);
if ((Math.abs(leftHeight – rightHeight) <= 1) && (isBalanced(p.left))
&& (isBalanced(p.right))) {
return true;
}
return false;
}
protected int getPathLength(E data, BSTNode<E> p) {
if (p == null)
return -1;
if (p.element.compareTo(data) < 0) {
int len = getPathLength(data, p.right);
if (len == -1) {
return len;
} else {
return 1 + len;
}
}
if (p.element.compareTo(data) > 0) {
int len = getPathLength(data, p.left);
if (len == -1) {
return len;
} else {
return 1 + len;
}
} else {
return 0;
}
}
public void printPath(E data, BSTNode<E> p) {
if(getPathLength(data, p) == -1) {
// means there is no node as data
System.out.println(“No element present as: ” + data);
} else {
BSTNode<E> start = p;
while(!start.element.equals(data)) {
System.out.println(start.element + ” -> “);
if (start.element.compareTo(data) < 0) {
start = start.right;
} else {
start = start.left;
}
}
System.out.println(start.element);
}
}
}