Advertisement
Guest User

Dictionary

a guest
Apr 30th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5. import java.io.BufferedReader;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8.  
  9.  
  10. public class Dictionary {
  11.    BinarySearchTree treeObject = new BinarySearchTree<>() ;
  12.    public static int numberOfLines ;
  13.    public static int DictSize = 0;
  14.  
  15.    public void LookWord(){
  16.         Scanner scanner = new Scanner(System.in);
  17.         String word = scanner. nextLine();
  18.         if (treeObject.contains(word) == true ){
  19.             System.out.println("YES");
  20.         }else {
  21.             System.out.println("NO");
  22.         }
  23.         return ;
  24.     }
  25.    
  26.    public void RemoveWord(){
  27.             Scanner scanner = new Scanner(System.in);
  28.             String word = scanner. nextLine();
  29.             treeObject.remove(word) ;
  30.             DictSize--;
  31.             printSize();
  32.             printHeight();
  33.         }
  34.  
  35.    public void BatchLook() throws IOException{
  36.        int wordsFound = 0 ;
  37.         String path = "C:/test/queries.txt" ;
  38.         String [] words = OpenFile(path) ;
  39.        for ( int i=0; i < numberOfLines ; i++ ){
  40.        if (treeObject.contains(words[i]) == true ){
  41.            System.out.println("YES : " + words[i]);
  42.            wordsFound ++ ;
  43.         }else {
  44.             System.out.println("NO : " + words[i]);
  45.         }
  46.        
  47.        }
  48.        System.out.println(" FOUND  " + wordsFound + "  words");
  49.        
  50.    }
  51.    
  52.    public void BatchDeletions() throws IOException{
  53.         String path = "C:/test/deletions.txt" ;
  54.         String [] words = OpenFile(path) ;
  55.        for ( int i=0; i < numberOfLines ; i++ ){
  56.            treeObject.remove(words[i]) ;
  57.            DictSize--;
  58.            printHeight();
  59.        }
  60.  }
  61.  
  62.  
  63.              /*Note: For validation purposes, you are required to print
  64.                    both the size of the dictionary and the height of your AVL
  65.                         tree after each insertion or deletion. When performing
  66.                                  batch insertions/deletions, you may print the height only
  67.                                                            once after the performed operation. */
  68.  
  69.  
  70.                           //////////////// READ FILE METHODS \\\\\\\\\\\\\\\\\
  71. private String []  OpenFile(String path) throws IOException{
  72.           FileReader fr = new FileReader(path) ;
  73.           BufferedReader textReader = new BufferedReader(fr) ;
  74.          
  75.            numberOfLines = readLines(path); //readLines();
  76.             String[ ] textData = new String[numberOfLines];
  77.          
  78.          
  79.           for (int i=0; i < numberOfLines ; i++) {
  80.              
  81.               textData[ i ] = textReader.readLine();
  82.               DictSize++;
  83.           }
  84.        
  85.           textReader.close( );
  86.           return textData ;
  87.          
  88.       }
  89.  
  90.      
  91. int readLines (String path) throws IOException {
  92.          
  93.           FileReader file_to_read = new FileReader(path) ;
  94.           BufferedReader bf  = new BufferedReader(file_to_read) ;
  95.          
  96.           String aLine ;
  97.           int numberOfLines = 0 ;
  98.          
  99.           while ((aLine = bf.readLine()) != null ){
  100.             //  aLine.substring(1, 3) ;
  101.               numberOfLines ++ ;
  102.           }
  103.           bf.close();
  104.           return numberOfLines ;
  105.       }
  106.  
  107.                                   ////////////////\\\\\\\\\\\\\\\\\
  108.  
  109. public void printSize(){
  110.     System.out.println( "Dictionary Size = " + DictSize );
  111. }
  112.  
  113. private void printHeight(){
  114.     System.out.println( "Tree Height = " + treeObject.findHeight(treeObject.root) );
  115. }
  116.  
  117. public void loadDictionary() throws IOException{
  118.     String path = "C:/test/dictionary.txt" ;
  119.     String [] words = OpenFile(path) ;
  120.    for ( int i=0; i < numberOfLines ; i++ ){
  121.        treeObject.insert(words[i],treeObject.root) ;  
  122.    }
  123. }
  124.  
  125. public void insertWord(){
  126.     Scanner scanner = new Scanner(System.in);
  127.     String word = scanner. nextLine();
  128.     treeObject.insert(word,treeObject.root) ;
  129.     DictSize++;
  130.     printSize();
  131.     printHeight();
  132. }
  133.  
  134.  
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement