Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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> implements TreeInterface {

// Data fields public TreeNode rootTreeNode = null; // Store the number of Nodes in this class variables public int size = 0; // Store the number of Non Leaf nodes in this class variables public int nonleaves;

public ArrayList inOrderTraversal = new ArrayList<>(); public ArrayList preOrderTraversal = new ArrayList<>(); public ArrayList postOrderTraversal = new ArrayList<>(); public ArrayList bstTraversal = new 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 parent = null; TreeNode current = rootTreeNode; while (current != null) { if (e.compareTo(current.element) < 0) { parent = current; current = current.left; } else if (e.compareTo(current.element) > 0) { parent = current; current = current.right; } else return false; // Duplicate node not inserted } // Create the new node and attach it to the parent if (e.compareTo(parent.element) < 0) parent.left = createNewNode(e); else parent.right = createNewNode(e); }

size++; return true; // Element inserted successfully } protected TreeNode createNewNode(E e) { return new TreeNode<>(e); } public boolean delete(E e) { //Locate the node to be deleted and also locate its parent node TreeNode parent = null; TreeNode current = rootTreeNode; while (current != null) { if (e.compareTo(current.element) < 0) { parent = current; current = current.left; } else if (e.compareTo(current.element) > 0) { parent = current; current = current.right; } else break; // Element is in the tree pointed at by current }

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 parentOfRightMost = current; TreeNode rightMost = current.left;

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 node) { if(node == null) { return 0; } else if((node.left == null) && (node.right == null)) { return 0; } else { return 1 + getNumberofNonLeaves(node.left) + getNumberofNonLeaves(node.right); } }

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

/** Return postorder traversal using stack i.e. without using recursion */ public ArrayList postorderNoRecursion(); }// end interface Tree

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Marketing Database Analytics

Authors: Andrew D. Banasiewicz

1st Edition

0415657881, 978-0415657884

More Books

Students also viewed these Databases questions

Question

9. Describe the characteristics of power.

Answered: 1 week ago

Question

10. Describe the relationship between communication and power.

Answered: 1 week ago