Question
Please help me fix this Java Error: type TreeNode does not take parameters BST.java: import java.util.ArrayList; import java.util.Stack; public class BST implements TreeInterface { //
Please help me fix this Java Error: type TreeNode does not take parameters
BST.java:
import java.util.ArrayList; import java.util.Stack;
public class BST
// Data fields public TreeNode
public ArrayList
// empty constructor public BST() { }
// Looks for an item in the tree public boolean search(E e) { TreeNode current = rootTreeNode; // Start from the root while (current != null) { if (e.compareTo(current.element) < 0) { current = current.left; // Go left } else if (e.compareTo(current.element) > 0) { current = current.right; // Go right } else // Element matches current.element return true; // Element is found } return false; } public boolean insert(E e) { if (rootTreeNode == null) rootTreeNode = createNewNode(e); // Create a new root else { // Locate the parent node TreeNode
size++; return true; // Element inserted successfully } protected TreeNode
if (current == null) return false; // Element is not in the tree
// Case 1: current has no left child if (current.left == null) { // Connect the parent with the right child of the current node if (parent == null) { rootTreeNode = current.right; } else { if (e.compareTo(parent.element) < 0) parent.left = current.right; else parent.right = current.right; } } else { // Case 2: The current node has a left child. // Locate the rightmost node in the left subtree of // the current node an also its parent. TreeNode
while (rightMost.right != null) { parentOfRightMost = rightMost; rightMost = rightMost.right; // Keep going to the right }
// Replace the element in current by the element in rightMost current.element = rightMost.element;
// Eliminate rightmost node if (parentOfRightMost.right == rightMost) parentOfRightMost.right = rightMost.left; else // Special case: parentOfRightMost == current parentOfRightMost.left = rightMost.left; }
size--; return true; // Element deleted successfully }
// get the Number of non-leaves. public int getNumberofNonLeaves() { return getNumberofNonLeaves(rootTreeNode); } public int getNumberofNonLeaves(TreeNode
TreeNode.java:
class TreeNode { Object element; TreeNode left; TreeNode right;
public TreeNode() {
}
public Object getElement() { return element; }
public void setElement(Object element) { this.element = element; }
public TreeNode getLeft() { return left; }
public void setLeft(TreeNode left) { this.left = left; }
public TreeNode getRight() { return right; }
public void setRight(TreeNode right) { this.right = right; }
// end TreeNode
}// end class TreeNode
TreeInterface.java:
import java.util.ArrayList;
public interface TreeInterface
/** Return true if the element is in the tree */
public void insert(E e);
/** * Delete the specified element from the tree Return true if the element is * deleted successfully */ public boolean delete(E e);
/** Return the Number of non-leaves. */ public int getNumberofNonLeaves();
/** Return inorder traversal using stack i.e. without using recursion */ public ArrayList
/** Return postorder traversal using stack i.e. without using recursion */ public ArrayList
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