Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Java Trie

611760May 14 2008 — edited May 14 2008
This post was inspired by a question here.

This is an implementation of a java trie. It is a tree structure representing words. Each node in the tree represents a single character, as shown below;
Trie with the words car, cat and dog added.
               root
              /    \
             c      d
            /         \
           a           o
          /  \          \
         r    t          g
The trie can be searched by prefix, returning a list of words which begin with the prefix. There are two classes, Trie and TrieNode. I've done a moderate amount of testing, but any improvements welcome. If anybody has any very large lists of words I'd be interested in doing some performance testing.
import java.util.ArrayList;
import java.util.List;

public class Trie
{
   private TrieNode root;
   
   /**
    * Constructor
    */
   public Trie()
   {
      root = new TrieNode();
   }
   
   /**
    * Adds a word to the Trie
    * @param word
    */
   public void addWord(String word)
   {
      root.addWord(word.toLowerCase());
   }
   
   /**
    * Get the words in the Trie with the given
    * prefix
    * @param prefix
    * @return a List containing String objects containing the words in
    *         the Trie with the given prefix.
    */
   public List getWords(String prefix)
   {
      //Find the node which represents the last letter of the prefix
      TrieNode lastNode = root;
      for (int i=0; i<prefix.length(); i++)
      {
	 lastNode = lastNode.getNode(prefix.charAt(i));
	 
	 //If no node matches, then no words exist, return empty list
	 if (lastNode == null) return new ArrayList();	 
      }
      
      //Return the words which eminate from the last node
      return lastNode.getWords();
   }
}
import java.util.ArrayList;
import java.util.List;

public class TrieNode
{
   private TrieNode parent;
   private TrieNode[] children;
   private boolean isLeaf;	//Quick way to check if any children exist
   private boolean isWord;	//Does this node represent the last character of a word
   private char character;	//The character this node represents

   /**
    * Constructor for top level root node.
    */
   public TrieNode()
   {
      children = new TrieNode[26];
      isLeaf = true;
      isWord = false;
   }

   /**
    * Constructor for child node.
    */
   public TrieNode(char character)
   {
      this();
      this.character = character;
   }
   
   /**
    * Adds a word to this node. This method is called recursively and
    * adds child nodes for each successive letter in the word, therefore
    * recursive calls will be made with partial words.
    * @param word the word to add
    */
   protected void addWord(String word)
   {
      isLeaf = false;
      int charPos = word.charAt(0) - 'a';
      
      if (children[charPos] == null)
      {
	 children[charPos] = new TrieNode(word.charAt(0));
	 children[charPos].parent = this;
      }
      
      if (word.length() > 1)
      {
	 children[charPos].addWord(word.substring(1));
      }
      else
      {
	 children[charPos].isWord = true;
      }
   }
   
   /**
    * Returns the child TrieNode representing the given char,
    * or null if no node exists.
    * @param c
    * @return
    */
   protected TrieNode getNode(char c)
   {
      return children[c - 'a'];
   }
   
   /**
    * Returns a List of String objects which are lower in the
    * hierarchy that this node.
    * @return
    */
   protected List getWords()
   {
      //Create a list to return
      List list = new ArrayList();
      
      //If this node represents a word, add it
      if (isWord)
      {
	 list.add(toString());
      }
      
      //If any children
      if (!isLeaf)
      {
	 //Add any words belonging to any children
	 for (int i=0; i<children.length; i++)
	 {
	    if (children[i] != null)
	    {
	       list.addAll(children.getWords());
}
}
}
return list;
}

/**
* Gets the String that this node represents.
* For example, if this node represents the character t, whose parent
* represents the charater a, whose parent represents the character
* c, then the String would be "cat".
* @return
*/
public String toString()
{
if (parent == null)
{
return "";
}
else
{
return parent.toString() + new String(new char[] {character});
}
}
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

Comments

mkeith
Hi Francis,

It appears that my first reply was lost, so will re-post.

When using JMS the reconnect policy should reconnect to the topic, but only if the should-remove-connection-on-error setting in sessions.xml is false. Alternatively you can set it directly on the CacheSynchronizationManager by invoking setShouldRemoveConnectionOnError(false).

Note that for RMIJNDI cache sync the RMI Registry is not used. It may be that whatever type of object you are using does not support transparent failover so you may need to reacquire a stub (e.g. for vanilla RMI objects).

HTH,
-Mike
413051
I am already setting should-remove-connection-on-error to false. Should this make cache synchronization continue to work after the OC4J instance running the JMS server has been restarted? Here is the relevant section of my sessions.xml (for JMS):
<cache-synchronization-manager>
<clustering-service>oracle.toplink.remote.jms.JMSClusteringService</clustering-service>
<should-remove-connection-on-error>false</should-remove-connection-on-error>
<jms-topic-connection-factory-name>jms/TopicConnectionFactory</jms-topic-connection-factory-name>
<jms-topic-name>jms/TopLinkCacheSyncTopic</jms-topic-name>
</cache-synchronization-manager>

I don't really understand your last paragraph... What do I have to do to make my objects support transparent failover? All of the objects that I am using TopLink to persist are simple Serializable JavaBeans.

Francis
mkeith
Hi Francis,

It appears as if by default the reconnection policy does not reconnect, so you will have to subclass the JMSDistributedSessionReconnectPolicy clas and set it on the clustering service (e.g. clusteringService.setReconnectPolicy(new MyReconnectPolicy())). You will want to override the reconnect(RemoteConnection) method to call clusteringService.createRemoteConnection(). Note that you wil have to set the clustering service on your policy instance when you set the policy so that you can access the service from within the reconnect callback. I would also recommend setting the logging to log-debug so that you can see what is happening.

For the RMI, it looks like from your stack trace an RMI object is broken. If you are using JavaBeans then it is is not a TopLink-managed one but it won't be affected or fixed by cache sync.

-Mike
1 - 3
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jun 11 2008
Added on May 14 2008
1 comment
66,586 views