All pastes #721378 Raw Edit

Unnamed

public text v1 · immutable
#721378 ·published 2007-10-01 05:52 UTC
rendered paste body
class trietest
{
	public static void main(String[] args)
	{	Trie trie;
		TrieNode rootNode, currentNode, nextNode, searchNode;
		trie = new Trie();		
		char chars[] = {'t', 'a', 'c', 'c', 'a', 'g', 't', 'a', 'c', 'c', 'a', 'g', 't', 'a', 'c', 'c', 'a', 'c', 't', 'a', '#'};
		//{'a', 'b', 'b', 'c', 'd', 'e'};
		int i = 0;

		
		rootNode = trie.head();
		currentNode = rootNode;
		
		while(i < chars.length )
		{	//System.out.println("Big Loop " + chars[i]);

			while(true) 
			{	//currentNode.printNode("loop");
				nextNode = trie.isChild(currentNode, chars[i]);
				if(nextNode == null) break;  // add chars[i] to currentNode as child
				else
				{	//nextNode.printNode("Great Success! found " + chars[i]);
					currentNode = nextNode;
					if( i + 1 < chars.length)	i++;
				}
			}	
		
			// so now chars[i] was not found in the current trie, so add it
			if(i < chars.length)
			{	trie.addNode(currentNode, chars[i]);
				//System.out.println("(" + currentNode.index + ", " + chars[i] + ")");	 // we put the encoding here
				currentNode = rootNode;
				i++;
			}
		} // end big while

		trie.printTrie();
		System.out.println();
		
		searchNode = trie.findNodeIndex(rootNode, 0);
		if(searchNode != null)	System.out.println("Great Success! Node 0 Found " + searchNode.letter);
		else System.out.println("Node 0 not found");
		
		searchNode = trie.findNodeIndex(rootNode, 1);
		if(searchNode != null)	System.out.println("Great Success! Node 1 Found " + searchNode.letter);
		else System.out.println("Node 1 not found");
		
		searchNode = trie.findNodeIndex(rootNode, 2);
		if(searchNode != null)	System.out.println("Great Success! Node 2 Found " + searchNode.letter);
		else System.out.println("Node 2 not found");
		
		searchNode = trie.findNodeIndex(rootNode, 3);
		if(searchNode != null)	System.out.println("Great Success! Node 3 Found " + searchNode.letter);
		else System.out.println("Node 3 not found");
		
		
	}// end main


}// end class test

/*  prints results like
	TRIE
	TrieNode@6a55fa 9 c  link=null next=TrieNode@32c41a 
	TrieNode@32c41a 11 #  link=null next=null 
	TrieNode@e89b94 6 a  link=TrieNode@6a55fa next=null 
	TrieNode@3e205f 1 t  link=TrieNode@e89b94 next=TrieNode@bf73fa 
	TrieNode@5740bb 8 g  link=null next=null 
	TrieNode@bf73fa 2 a  link=TrieNode@5740bb next=TrieNode@5ac072 
	TrieNode@109a4c 10 c  link=null next=null 
	TrieNode@201f9 4 a  link=TrieNode@109a4c next=TrieNode@cf8583 
	TrieNode@cf8583 7 c  link=null next=null 
	TrieNode@5ac072 3 c  link=TrieNode@201f9 next=TrieNode@4693c7 
	TrieNode@4693c7 5 g  link=null next=null 
	TrieNode@901887 0 -  link=TrieNode@3e205f next=null 

	Great Success! Node 0 Found -
	Node 1 not found
	Node 2 not found
	Node 3 not found
*/