Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Scanner;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- public class Dictionary {
- BinarySearchTree treeObject = new BinarySearchTree<>() ;
- public static int numberOfLines ;
- public static int DictSize = 0;
- public void LookWord(){
- Scanner scanner = new Scanner(System.in);
- String word = scanner. nextLine();
- if (treeObject.contains(word) == true ){
- System.out.println("YES");
- }else {
- System.out.println("NO");
- }
- return ;
- }
- public void RemoveWord(){
- Scanner scanner = new Scanner(System.in);
- String word = scanner. nextLine();
- treeObject.remove(word) ;
- DictSize--;
- printSize();
- printHeight();
- }
- public void BatchLook() throws IOException{
- int wordsFound = 0 ;
- String path = "C:/test/queries.txt" ;
- String [] words = OpenFile(path) ;
- for ( int i=0; i < numberOfLines ; i++ ){
- if (treeObject.contains(words[i]) == true ){
- System.out.println("YES : " + words[i]);
- wordsFound ++ ;
- }else {
- System.out.println("NO : " + words[i]);
- }
- }
- System.out.println(" FOUND " + wordsFound + " words");
- }
- public void BatchDeletions() throws IOException{
- String path = "C:/test/deletions.txt" ;
- String [] words = OpenFile(path) ;
- for ( int i=0; i < numberOfLines ; i++ ){
- treeObject.remove(words[i]) ;
- DictSize--;
- printHeight();
- }
- }
- /*Note: For validation purposes, you are required to print
- both the size of the dictionary and the height of your AVL
- tree after each insertion or deletion. When performing
- batch insertions/deletions, you may print the height only
- once after the performed operation. */
- //////////////// READ FILE METHODS \\\\\\\\\\\\\\\\\
- private String [] OpenFile(String path) throws IOException{
- FileReader fr = new FileReader(path) ;
- BufferedReader textReader = new BufferedReader(fr) ;
- numberOfLines = readLines(path); //readLines();
- String[ ] textData = new String[numberOfLines];
- for (int i=0; i < numberOfLines ; i++) {
- textData[ i ] = textReader.readLine();
- DictSize++;
- }
- textReader.close( );
- return textData ;
- }
- int readLines (String path) throws IOException {
- FileReader file_to_read = new FileReader(path) ;
- BufferedReader bf = new BufferedReader(file_to_read) ;
- String aLine ;
- int numberOfLines = 0 ;
- while ((aLine = bf.readLine()) != null ){
- // aLine.substring(1, 3) ;
- numberOfLines ++ ;
- }
- bf.close();
- return numberOfLines ;
- }
- ////////////////\\\\\\\\\\\\\\\\\
- public void printSize(){
- System.out.println( "Dictionary Size = " + DictSize );
- }
- private void printHeight(){
- System.out.println( "Tree Height = " + treeObject.findHeight(treeObject.root) );
- }
- public void loadDictionary() throws IOException{
- String path = "C:/test/dictionary.txt" ;
- String [] words = OpenFile(path) ;
- for ( int i=0; i < numberOfLines ; i++ ){
- treeObject.insert(words[i],treeObject.root) ;
- }
- }
- public void insertWord(){
- Scanner scanner = new Scanner(System.in);
- String word = scanner. nextLine();
- treeObject.insert(word,treeObject.root) ;
- DictSize++;
- printSize();
- printHeight();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement