Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.93 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3.  
  4. public class Trie {
  5.  
  6.     private TrieNode rootNode = null;
  7.     static private final char ROOT_NODE_CHAR = '*'; //A dummy char to represent the Root Node
  8.  
  9.     private class TrieNode{ //This is the class that represents a node in the trie
  10.         private char value; // the character contained at this node
  11.         private TrieNode[] subnodes; //the subtrees that stem from this node
  12.         private boolean isValidEnd = false; //We need to know if this node represents the end point of a a valid word
  13.  
  14.         public TrieNode(char c){
  15.             value = c;
  16.             isValidEnd = false;
  17.             subnodes = new TrieNode[26];
  18.             for(int i =0; i < 26; i++){ //initialise each node to null. 26 nodes for 26 letters
  19.                 subnodes[i] = null;
  20.             }  
  21.         }
  22.  
  23.         public void addWord(String s){
  24.             int positionOfNextNode = ((int)s.codePointAt(0)) - 97; //97 is 'a' in ASCII //Where is this character based in the array           
  25.             if(subnodes[positionOfNextNode] == null){ //add a new node for this value
  26.                 subnodes[positionOfNextNode] = new TrieNode(s.charAt(0));
  27.             }
  28.             if(s.length()== 1) //if this is the last character, and we don't need to add a node, then set the end point to be valid
  29.                 subnodes[positionOfNextNode].isValidEnd = true;
  30.             else
  31.                 subnodes[positionOfNextNode].addWord(s.substring(1)); //add the substring from 1 on to that node
  32.         }
  33.  
  34.         public TrieNode deleteWord(String s){
  35.             //similar to deleting a linked list, we rebuild the Trie as we return.
  36.             if(s.length() == 0){ //this is the last char
  37.                 isValidEnd = false;
  38.             }else{
  39.                 int positionOfNextNode = ((int)s.codePointAt(0)) - 97; //97 is 'a' in ASCII
  40.                 if(subnodes[positionOfNextNode] == null){
  41.                     return this; //we don't have the word at all
  42.                 }else{ //there are still more characters
  43.                     subnodes[positionOfNextNode] =  subnodes[positionOfNextNode].deleteWord(s.substring(1));
  44.                 }
  45.             }
  46.             //As a final step. can we delete the nodes
  47.             //remember. we can only delete a node at this point if it is not a valid end point and it has no subtrees
  48.             //otherwise we need to leave it alone.  
  49.             if(!isValidEnd){
  50.                 boolean canDeleteNode = true;
  51.                 for (int i = 0; i < subnodes.length; i++){
  52.                     if (subnodes[i] != null)
  53.                         canDeleteNode = false;
  54.                 }
  55.  
  56.                 //if we can remove this node then return false
  57.                 if(canDeleteNode)
  58.                     return null;
  59.                 else
  60.                     return this;
  61.             }
  62.             return this;
  63.         }
  64.  
  65.         public boolean containsWord(String s){
  66.             int positionOfNextNode = ((int)s.codePointAt(0)) - 97; //97 is 'a' in ASCII
  67.             if(subnodes[positionOfNextNode] == null){
  68.                 return false; //we don't have the word
  69.             }else{ //there are still more characters
  70.                 if(s.length()== 1){
  71.                     return subnodes[positionOfNextNode].isValidEnd;
  72.                 }else{             
  73.                     return subnodes[positionOfNextNode].containsWord(s.substring(1));
  74.                 }
  75.             }
  76.         }
  77.  
  78.  
  79.  
  80.         public ArrayList returnAllStrings(){
  81.             ArrayList al = new ArrayList();
  82.             //if this is the root node, then we don't want to add that character on
  83.             String prefixString = "";
  84.             if(value == ROOT_NODE_CHAR){
  85.                 prefixString ="";
  86.             }else{
  87.                 prefixString = ""+value;
  88.             }
  89.             if(isValidEnd){ //if this is a valid end point we need to add the char we store as a string
  90.                 al.add(prefixString);
  91.             }
  92.  
  93.             //Find all the substrings and add them on
  94.             for (int i = 0; i < subnodes.length; i++){
  95.                 if (subnodes[i] != null){
  96.                     //there be substrings
  97.                     ArrayList tempAL = subnodes[i].returnAllStrings();
  98.                     Iterator it = tempAL.iterator();
  99.                     while (it.hasNext()){
  100.                         al.add(prefixString+it.next()); //add our prefix onto each  suffix
  101.                     }
  102.                 }
  103.             }
  104.             return al;
  105.         }
  106.  
  107.         public int count(TrieNode trie) {
  108.             if(trie == null) {
  109.                 return 0;
  110.             }
  111.             int i = 0;
  112.             if(trie.isValidEnd) {
  113.                 i++;
  114.             }
  115.             for(TrieNode node : trie.subnodes) {
  116.                 i += count(node);
  117.             }
  118.  
  119.             return i;
  120.  
  121.         }
  122.  
  123.     }
  124.    
  125.     //Prints all of the words in the Trie to the console
  126.     public void printAllWords(){
  127.         if(rootNode == null){
  128.             return;
  129.         }else{
  130.             ArrayList al =  rootNode.returnAllStrings();
  131.  
  132.             Iterator it = al.iterator();
  133.             while(it.hasNext()){
  134.                 System.out.println(it.next());         
  135.             }
  136.         }
  137.     }
  138.  
  139.     // inserts
  140.     public void insertString(String s){
  141.         if(rootNode == null){
  142.             rootNode = new TrieNode(ROOT_NODE_CHAR);
  143.         }
  144.         rootNode.addWord(s.toLowerCase());
  145.     }
  146.  
  147.     public boolean containsString(String s){
  148.         return rootNode.containsWord(s.toLowerCase());
  149.     }
  150.  
  151.     public void deleteString(String s){
  152.         rootNode.deleteWord(s.toLowerCase());
  153.     }
  154.  
  155.     /*
  156.      * Q1:  complete implementation
  157.      */
  158.     public int countAllWords(){
  159.  
  160.         return ;
  161.     }
  162.    
  163.     /*
  164.      * Optional: complete implementation
  165.      */
  166.     public boolean areWordsWithPrefix(String str){
  167.         return false;
  168.     }
  169.    
  170.  
  171.     public static void main (String[] args){
  172.         Trie t = new Trie();
  173.         t.insertString("hello");
  174.         t.insertString("hell");
  175.         t.insertString("zebra");
  176.         t.printAllWords();
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement