Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java BST words case from Linux or terminal command ( NO ECLIPSE PACKS OR GUI) Ne

ID: 662258 • Letter: J

Question

Java BST words case from Linux or terminal command ( NO ECLIPSE PACKS OR GUI)

Need a program that reads words from txt file and sort it in BST as it prints every added and removed words. as well as the bst collection words at the end.

it should:

-lowercase all the words

-use insert, find and remove methods.

-ignore spaces

example in numbers(reads them as strings):

when the txt file content is : 1 2 3 4 5 6 7 8 9 1 2 3 4 3

output is :

1 add

1 2 add

1 2 3 add

1 2 3 4 add

1 2 3 4 5 add

1 2 3 4 5 6 add

1 2 3 4 5 6 7 add

1 2 3 4 5 6 7 8 add

1 2 3 4 5 6 7 8 9 add

1 rem

2 rem

3 rem

4 rem

bst collection :

etc...

Explanation / Answer

Program:

//java program for BST

import java.io.*;

import java.util.*;

//Class that implements BinarySearchTree

class BSearchTree

{

   BSTNode1 root;

     public BSearchTree()

{

      root = null;

   }

/*** Insertion ****/

     public void insert(String str)

{

      root = insert(str, root);

   }

private static BSTNode1 insert(String string, BSTNode temp) {

          if (temp == null)

return new BSTNode1(string);

          int compare = string.compareTo(temp.word);

          if (compare < 0)

temp.left = insert(string, temp.left);

          else if (compare > 0)

temp.right = insert(string, temp.right);

          return temp;

   }

/**** Deletion *****/

public void BSTDelete(String str)

{

root=deleteBSTNode(root,str);

}

public static BSTNode1 deleteBSTNode(BSTNode root, String str)

{

if(temp==null)

return temp;

else if(compare(str,temp.word)<0)

temp.left=deleteNode(temp.left,str);

else if(compare(str,temp.word)>0)

temp.right=deleteNode(temp.right,str);

else

{

if(temp.left==null)

return temp.right;

else if(temp.right==null)

return temp.left;

else

{

     Temp.word=getValue(temp.left);

     Temp.left=deleteBSTNode(temp.left,temp.word);

}

}

}

Private BSTNode1 getValue(BSTNode1 temp)

{

     While(temp1.right!=null)

          Temp1=temp1.right;

     Return temp1.data;

}

/***** Search ******/

public boolean BSTSearch(String str)

{

return search1(root, str);

}

private static boolean search1(BSTNode1 node, String str)

{

if(node==null)

return false;

else if(compare(str,node.word)==0)

return true;

else if(compare(str,node.word)<0)

return search1(node.left,str);

else

return search1(node.right,str);

}

/***** Display *****/

   public void showBSearchTree()

{

               System.out.println("Bst collection");

              showTree(root);

              System.out.print();

   }

   private static void showTree(BSTNode1 node)

{

          if (node == null) return;

              showTree(node.left);

          System.out.print(node.word + " ");

              showTree(node.right);

   }

}//class BSearchTree ends

/**** Node description for Binary Search Tree ****/

class BSTNode1

{

   String word;

   BSTNode1 left, right;

//constructor

   public BSTNode1(String word1)

{

          this.word = word1;

          left = null;

          right = null;

   }

}

/**** main class for BST implementation ****/

public class BSTImplementation

{

//main method

public static void main(String[] args)

{

Scanner scan=new scanner(System.in);

BSerachTree bst=new BsearchTree();

String filename;

System.out.println("Enter the file name");

filename=scan.nextLine();

Scanner fileread=new Scanner(new Filereader(filename));

System.out.println("Menu");

System.out.println(" 1.Insertion 2.deletion 3.Search ");

System.out.println("Enter ur choice");

int ch=scan.nextInt();

switch(ch)

{

//insertion

case 1:

while(fileread.hasNextLine())

{

String line=fileRead.readLine();

boolean found=bst.BSTSearch(line);

if(found)

System.out.println(string+"rem");

else

{

bst.insert(line);

bst.showBSearchTree();

}

}

break;

//deletion

case 2:

System.out.println("which element do u want to delete");

String word=scan.nextLine();

bst.BSTDelete(word);

bst.showBSearchTree();

break;

//searching an element

case 3:

System.out.println(" enter the search element");

String word=scan.nextLine();

boolean elementSearch=bst.BSTSearch(str);

if(elementSearch)

System.out.println("Element found");

else

System.out.println("Element not found in BST");

break;

default:

System.out.println("Wrong choice");

}

}//main method

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote