SaumyaGupta

Untitled

Nov 30th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. package tries;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.Statement;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. public class TrieDB {
  11.     public static void main(String[] args) {
  12.         String url = "jdbc:mysql://localhost:3306/sakila";
  13.         String user = "root";
  14.         String password = "secret";
  15.         final List<String> setOfStrings = new ArrayList<>();
  16.  
  17.  
  18.         try {
  19.             Class.forName("com.mysql.jdbc.Driver");
  20.             Connection conn = DriverManager.getConnection(url, user, password);
  21.  
  22.             String sql = "select first_name from actor ";
  23.             Statement stmt = conn.createStatement();
  24.             ResultSet rs = stmt.executeQuery(sql);
  25.             while(rs.next())
  26.             {
  27.                 String firstName = rs.getString("first_name");
  28.                 setOfStrings.add(firstName);}
  29.             }catch (Exception ex) {
  30.             ex.printStackTrace();
  31.             }
  32.         System.out.println(setOfStrings);
  33.         final Trie trie = new Trie();
  34. //        setOfStrings.forEach(trie::insert);
  35.  
  36.         for(String s : setOfStrings) {
  37.             System.out.println(trie.query(s));
  38.         }
  39.     }
  40. }
  41.  
  42. class Trie {
  43.     final TrieNode root;
  44.  
  45.     public Trie() {
  46.         this.root = new TrieNode();
  47.     }
  48.  
  49.     public int query(final String s) {
  50.         TrieNode current = root;
  51.         for (int i = 0; i < s.length(); i++) {
  52.             if(current==null || current.next(s.charAt(i))==null) {
  53.                 return 0;
  54.             }
  55.             else
  56.                 current=current.next(s.charAt(i));
  57.         }
  58.         return current.terminating;
  59.     }
  60.  
  61.     public void insert(final String s) {
  62.         TrieNode current = root;
  63.         char c;
  64.         for (int i = 0; i < s.length(); i++) {
  65.             if(s.charAt(i)<=90) {
  66.                if (current.trieNodes[s.charAt(i) - 'A'] == null) {
  67.                 current.trieNodes[s.charAt(i) - 'A'] = new TrieNode();
  68.                }
  69.             }
  70.                else {  
  71.                 if (current.trieNodes[s.charAt(i) - 'a'] == null) {
  72.                     current.trieNodes[s.charAt(i) - 'a'] = new TrieNode();
  73.                 }
  74.             }
  75.             current = current.next(s.charAt(i));
  76.         }
  77.         current.terminating++;
  78.     }
  79. }
  80.  
  81. class TrieNode {
  82.     int terminating;
  83.     final TrieNode[] trieNodes = new TrieNode[58];
  84.  
  85.     public TrieNode next(final char c) {
  86.         return trieNodes[c - 'a'];
  87.     }
  88. }
Add Comment
Please, Sign In to add comment