Question: Can you try to fix this error for me BinarySearchTree.java:152: error: bad operand types for binary operator ' node.getWord()) node.right = insertRec(node.right, word); //else

Can you try to fix this error for me "BinarySearchTree.java:152: error: bad operand types for binary operator '<' if (w < node.getWord()) ^ first type: String second type: String 1 error"

You're allowed to make any neccessary changes and IF you do comment on the code why you did those adjustments.

Bear in mind that the code is not complete.

import java.util.Scanner;

public class BinarySearchTree{

private Node root; private String word; public static void main (String [] args){ Scanner scan = new Scanner(System.in); BinarySearchTree tree = new BinarySearchTree(); String inputFromUser = " "; tree.inputUser(); for (int i = 0; i < 5; i++ ){ word = scan.nextLine(); tree.insert(word); } tree.printPreOrder(); System.out.println(); tree.printInorder(); System.out.println(); tree.printPostorder(); }//main private class Node{ private Node right; private Node left; private String data; private int count; public Node (String item){ data = item; right = null; left = null; count = 0; }// constructor public Node(){ right = null; left = null; data = null; count = 1; } public void getWord(String word){ data = word; } public void setLeft(Node left){ this.left = left; } public void setRight(Node right){ this.right = right; } public void setCount(int count){ this.count = count; } public Node getLeft(){ return left; } public Node getRight(){ return right; } public String getWord(){ return word; } public int getcount(){ return count; } } public void inputUser(){ System.out.println("Welcome to Binary Search Tree application."); System.out.println("You will be asked to type the names of the file containing strictly words only."); System.out.println("The application file will not accept non-letter words and all will be lowercases. "); System.out.println("Please type in the words... "); }//User Instructtion and his/her input to the program // This method mainly calls insertRec() public void insert(String key){ root = insertRec(root, key); } //A recursive function to insert a new key in BST. public Node insertRec(Node node, String word){ int count = 0; /* If the tree is empty, return a new node */ if (node == null) { node = new Node(word); } else { /* Otherwise, recur down the tree */ if (word < node.getWord()) node.left = insertRec(node.left, word); else if (word > node.getWord()) node.right = insertRec(node.right, word); //else node.setCount(node.getCount() + 1); } /* return the (unchanged) node pointer */ return node; }// insertRec Method which takes two parameters of Root and Key. public void insertInorderRec(Node node) { if (node == null) { return; } //recur on the left side of the tree. insertInorderRec(node.left); //recur on the right side of the tree. insertInorderRec(node.right); }// printInorder method. public void printInorder(){ insertInorderRec(root); System.out.print(root.data + " "); } public void insertPreOderder(Node node){ if (node == null) return; //recur on the left subtree. insertPreOderder(node.left); //then recur on the right subtree. insertPreOderder(node.right); }// ptintPreOderder method. public void printPreOrder(){ insertPreOderder(root); System.out.print(root.data + " "); } public void insertPostorder(Node node){ if (node == null) return; // First recur on left subtree. insertPostorder(node.left); //Then recur on right subtree. insertPostorder(node.right); }// printPostorder method. public void printPostorder(){ insertPostorder(root); System.out.print(root.data + " "); }

}

Here is the basic instruction of how the code shoud behave.

2. The words are sorted in the BST by keying off every node's word: words that come alphabetically before the node's word will be placed in the left subtree, while words that are alphabetically later will be placed in the right subtree. This algorithm is applied recursively

3. In addition, a node will keep track of how many times that particular word appeared by having an additional node field, for a total of four fields: left subtree, right subtree, the word for that node (a String), and a count for that word (an int) The tree will insert words, but will not delete any

4. The tree will insert words, but will not delete any

5. Insertion always starts at the root, and new words always become a new leaf. Words already in the tree will have their count incremented rather than a new node created and inserted. This gives two base cases for the insertion traversal: 1) the word is found; increment the count, return, 2) the traversal reaches the point where it would step down again but can't because the alphabetically-correct subtree is empty, create a new node with the word and a count of one, return.

6. Write three methods to print the tree: pre-order, in-order, and post-order where printing the node is the "visit." Note that in-order should print the words in alphabetical order -check your output. Output goes to a text file. The program will have a tree class containing main with a private,

7. embedded tree node class. Write a node constructor that receives its word as a parameter. The node class will also have a print method for the data in that one node. The tree class will have the recursive print methoo for the whole tree.

8. The user will input the name of the file containing the words. The file wll not have any non-letter words, and they will all be lowercase. They are not, however, all on separate lines.

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!