Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

help with my recursive trie search

Thread view: 
chuck - 01 Oct 2007 07:15 GMT
Hello,
I wrote a simple Trie class that creates a Trie structure correctly
from character array. It will correctly recurisivley print the trie
correctly too, albeit not sorted.

The trie structue is a linked lists structure.  Each node has two
pointers, next(sibling) and link(children).

I adapted the same structure that my print trie method uses to search
the trie for a particular integer index and want to return the trie
node if the index is found.  The search seems to stop the recursion
when it finds the node, but returns null.  The only node it correctly
find is the root.

public TrieNode findNodeIndex(TrieNode currentNode, int i){
// if(currentNode.index == i)      return currentNode;
if(currentNode.link != null)   findNodeIndex(currentNode.link, i);
//System.out.println( currentNode);
if(currentNode.index == i)      return currentNode;
if(currentNode.next != null)    findNodeIndex(currentNode.next, i);
//if(currentNode.index == i)      return currentNode;
return null;
}

I have tried putting the test in various places without success, any
ideas why this fails?

If it helps i put Trie.java here
http://pastebin.ca/721376

and the test file, trietest.java here
http://pastebin.ca/721378

any help is appreciated
Chuck
Behrang - 01 Oct 2007 09:51 GMT
> Hello,
>  I wrote a simple Trie class that creates a Trie structure correctly
[quoted text clipped - 30 lines]
> any help is appreciated
> Chuck

Hi Chuck,

You are returning from findNodeIndex too early. I have posted the
corrected findNodeIndex here:

  http://pastebin.ca/721497

Also, in English, it is spelled "Tree", not "Trie" ;-)

-Behrang
Matt Humphrey - 01 Oct 2007 12:08 GMT
| > Hello,
| >  I wrote a simple Trie class that creates a Trie structure correctly
[quoted text clipped - 39 lines]
|
| Also, in English, it is spelled "Tree", not "Trie" ;-)

There is a data structure called a "trie"
http://en.wikipedia.org/wiki/Trie.

Matt Humphrey http://www.iviz.com/
Behrang - 01 Oct 2007 13:23 GMT
Jeez! I have studied CS for more than 6 years and this is the first
time I have seen this term :-))

> | > Hello,
> | >  I wrote a simple Trie class that creates a Trie structure correctly
[quoted text clipped - 44 lines]
>
> Matt Humphrey http://www.iviz.com/
Joshua Cranmer - 01 Oct 2007 22:04 GMT
> Jeez! I have studied CS for more than 6 years and this is the first
> time I have seen this term :-))

Tries are discussed in Knuth's The Art of Computer Programming (volume
1, I think). The only major usage I have ever heard of them was in TeX's
hyphenation algorithm. It is, to say the least, not a very popular data
structure.

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Matt Humphrey - 01 Oct 2007 22:55 GMT
| > Jeez! I have studied CS for more than 6 years and this is the first
| > time I have seen this term :-))
[quoted text clipped - 3 lines]
| hyphenation algorithm. It is, to say the least, not a very popular data
| structure.

I thought they were used just for dictionaries.  When I learned how to do
them (long ago) one of their advantages was that they could be traversed
while in an dense binary format--instead of storing keys and offsets only
certain bit counts are stored. Wikipedia says they're part of the fastest
technique for sorting strings.

Matt Humphrey http://www.iviz.com/
Joshua Cranmer - 01 Oct 2007 23:03 GMT
> | > Jeez! I have studied CS for more than 6 years and this is the first
> | > time I have seen this term :-))
[quoted text clipped - 9 lines]
> certain bit counts are stored. Wikipedia says they're part of the fastest
> technique for sorting strings.

s/very popular/well-known/

I too have known some semantics for a while, but I didn't know them
under the name `trie'.

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Christian - 01 Oct 2007 23:33 GMT
Joshua Cranmer schrieb:
>> Jeez! I have studied CS for more than 6 years and this is the first
>> time I have seen this term :-))
[quoted text clipped - 3 lines]
> hyphenation algorithm. It is, to say the least, not a very popular data
> structure.

your os probably uses these tries to index your disc storage for faster
searching.. (suffixtrees are also tries)

Also some state of the art p2p systems use a distributed trie instead of
a normal dht.
chuck - 01 Oct 2007 23:44 GMT
> | You are returning from findNodeIndex too early. I have posted the
> | corrected findNodeIndex here:
[quoted text clipped - 7 lines]
>
> Matt Humphrey http://www.iviz.com/

Thanks Behrang.

Yes, I too thought Trie was a misprint, however it is a real data
structure.  In my case, I am using a trie for Lempel-Ziv
encoding/decoding.

Chuck


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.