Advertisement
Bladtman

Untitled

Jun 26th, 2012
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.06 KB | None | 0 0
  1. //The code in this file is based on the code from http://algs4.cs.princeton.edu/52trie/TST.java.html
  2. package View;
  3.  
  4. import java.util.Queue;
  5. import java.util.LinkedList;
  6. import java.util.Scanner;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.io.FileNotFoundException;
  10.  
  11. public class TernaryTrie {
  12.  
  13.     Node root;
  14.    
  15.     /**
  16.      * Build a trie from the standard path
  17.      */
  18.     public TernaryTrie() {
  19.         this(new File("TrieData.txt"));
  20.     };
  21.  
  22.     /**
  23.      * Build the trie from the file denoted by the given string path
  24.      * @param String path to file
  25.      */
  26.     public TernaryTrie(String fp) {
  27.         this(new File(fp));
  28.     }
  29.  
  30.     /**
  31.      * build the trie from a file
  32.      * @param String the file to build the trie from
  33.      */
  34.     public TernaryTrie(File f) {
  35.         Scanner fileScan = null;
  36.         try {
  37.             fileScan = new Scanner(f, "UTF-8");
  38.             while (fileScan.hasNext()) {
  39.                 String[] name = fileScan.nextLine().split(";");
  40.                 put(name[0], name[1]);
  41.             }
  42.         } catch (FileNotFoundException e) {
  43.             throw new RuntimeException(e);
  44.         } finally {
  45.             fileScan.close();
  46.         }
  47.     }  
  48.  
  49.     /**
  50.      * Retrieve the value correspoding to a given string.
  51.      * if the given string can not be found, null is returned
  52.      * @param String string to search for. (exact match).
  53.      * @return String
  54.      */
  55.     public String get(String s) {
  56.         Node n = get(root, s, 0);
  57.         if (n== null) return null;
  58.         return n.val;
  59.     }
  60.  
  61.     private Node get(Node node, String s, int d) {
  62.         if (node == null) return null;
  63.         char c = s.charAt(d);
  64.         if  (c < node.c) return get(node.l, s, d);
  65.         else if (c > node.c) return get(node.r, s, d);
  66.         else if (d < s.length() -1) return get(node.m, s, d+1);
  67.         else return node;
  68.     }
  69.  
  70.     /**
  71.      * insert a key-value pair into the trie
  72.      * @param String key
  73.      * @param String value
  74.      */
  75.     public void put(String s, String v) {
  76.         root = put(root, s.toLowerCase(), v, 0 );
  77.     }
  78.  
  79.     private Node put(Node node, String s, String v, int d) {
  80.         char c = s.charAt(d);
  81.         if (node == null) node = new Node(c);
  82.         if  (c < node.c) node.l = put(node.l, s, v, d);
  83.         else if (c > node.c) node.r = put(node.r, s, v, d);
  84.         else if (d < s.length() -1) node.m = put(node.m, s, v, d+1);
  85.         else node.val = v;
  86.  
  87.         return node;
  88.     }
  89.  
  90.     /**
  91.      * Get the keys that are prefixed with a given String
  92.      * @param String prefix
  93.      * @return String
  94.      */
  95.     public Iterable<String> startsWith(String pre) {
  96.         if (pre.length()==0) return null;
  97.         Queue<String> q = new LinkedList<String>();
  98.         Node n = get(root, pre, 0);
  99.         if (n == null) return q;
  100.         if (n.val != null) q.offer(pre);
  101.         collect(n.m , pre, q);
  102.         return q;
  103.     }
  104.  
  105.     private void collect(Node node, String pre, Queue<String> q){
  106.         if (node == null) return;
  107.         collect(node.l, pre, q);    //swapped with next line to arange output lexiographically (well, sort of)
  108.         if (node.val != null) q.offer(pre + node.c);
  109.         collect(node.m, pre + node.c, q);
  110.         collect(node.r, pre, q);
  111.     }
  112.  
  113.     /**
  114.      * Prints a 60 character wide chart of characters from ascii code 0 through 600
  115.      */
  116.     public void testChars() {
  117.         for (int i=0; i<=600; i++){
  118.             if(i%60==0) System.out.println();
  119.             System.out.print((char)i + " ");
  120.         }      
  121.     }
  122.  
  123.  
  124.     //helper
  125.     private String cut(String s) {
  126.         return s.substring(0, s.length()-1);
  127.     }
  128.  
  129.     //private helper class
  130.     private class Node{
  131.         char c;
  132.         String val;
  133.         Node l, m, r;
  134.  
  135.         public Node(char c) {
  136.             this.c = c;
  137.         }
  138.  
  139.         public Node() {}
  140.     }  
  141.    
  142.     /**
  143.      * Mainly for testing purposes
  144.      */
  145.     public static void main (String[] args) {
  146.         if (args.length < 1) {
  147.             System.out.println("yeah, giving me no parameters is the way to go!\n" +
  148.                     "What am I, a freaking guessing machine?");
  149.                 return;
  150.         }
  151.  
  152.         System.out.println("Loading " + args[0] + ", please wait.");
  153.         TernaryTrie tt = new TernaryTrie(args[0]);
  154.         Scanner inputScan = new Scanner(System.in);
  155.         while (true) {
  156.             System.out.println("Waiting for a search-prefix...");
  157.             String q = inputScan.nextLine().toLowerCase();
  158.  
  159.             if (q.substring(0, 2).equals("g:")) {
  160.                 q=q.substring(3);
  161.                 System.out.println("Search result for " + q + ":");
  162.                 System.out.println(tt.get(q));
  163.             } else {
  164.                 System.out.println("Entries beginning with " + q + ":");
  165.                 for (String r : tt.startsWith(q)) {
  166.                     System.out.println(r);
  167.                 }
  168.             }
  169.             System.out.println();
  170.         }
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement