Advertisement
Guest User

Untitled

a guest
May 4th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.31 KB | None | 0 0
  1. import java.io.FileReader;
  2. import java.util.*;
  3.  
  4.  
  5. public class Main {
  6.     public static void main(String[] args) throws Exception {
  7.        
  8.         Drzewo tree = new Drzewo();
  9.         ArrayList<Elementy> lista = stworzListe();
  10.         for (Elementy node: lista) {
  11.             tree.insertBST(node);
  12.         }
  13.      
  14.        // tree.wyswietl(tree._root);
  15.         tree.wyswietl(tree._root);
  16. tree.queueBFS(tree._root);
  17.        
  18.     }
  19.  
  20.     public static String sameSlowa(String s) {
  21.         String str = "";
  22.         boolean kontrola = false;
  23.         for (int i=0; i<s.length(); i++)
  24.             if (Character.isLetter(s.charAt(i))) {
  25.                 kontrola = true;
  26.                 break;
  27.             }
  28.         if (kontrola) {
  29.             for (int i = 0; i < s.length(); i++)
  30.                 if (Character.isLetter(s.charAt(i)))
  31.                     str += s.charAt(i);
  32.             return str;
  33.         }
  34.         else return null;
  35.     }
  36.  
  37.     public static ArrayList<Elementy> stworzListe () throws Exception {
  38.         ArrayList<Elementy> elementy = new ArrayList<>();
  39.  
  40.         int wiersz = 1;
  41.         Scanner sc = new Scanner(new FileReader("test.txt"));
  42.         while (sc.hasNextLine()) {
  43.             Scanner s2 = new Scanner(sc.nextLine());
  44.             while (s2.hasNext()) {
  45.                 String s = s2.next();
  46.                 if(sameSlowa(s) != null) {
  47.                     String str = sameSlowa(s);
  48.  
  49.                     Elementy node = new Elementy();
  50.                     node.data = str;
  51.                     if (elementy.contains(node)) {
  52.                         int ind = elementy.indexOf(node);
  53.                         Elementy n = elementy.get(ind);
  54.                         n.lista.add(wiersz);
  55.                     }
  56.                    
  57.                    
  58.                     else {
  59.                         node.lista.add(wiersz);
  60.                         elementy.add(node);
  61.                     }
  62.                 }
  63.             }
  64.             wiersz++;
  65.         }
  66.         return elementy;
  67.     }
  68.  
  69.     public static void wypiszListe(ArrayList<Elementy> lista) {
  70.        
  71.        
  72.         for(Elementy n: lista) {
  73.             System.out.print(n.data +" ");
  74.             for (Integer i: n.lista)
  75.                 System.out.print(i + ", ");
  76.  
  77.             System.out.print("\n");
  78.         }
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement