2

I found a java implementation of a trie and would like to have a similar one in J2ME. Here is the code.

Node class

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

class Node {

private final char ch;

/**
 * Flag indicates that this node is the end of the string.
 */
private boolean end;

private LinkedList<Node> children;

public Node(char ch) {
    this.ch = ch;
}

public void addChild(Node node) {
    if (children == null) {
        children = new LinkedList<Node>();
    }
    children.add(node);
}

public Node getNode(char ch) {
    if (children == null) {
        return null;
    }
    for (Node child : children) {
        if (child.getChar() == ch) {
            return child;
        }
    }
    return null;
}

public char getChar() {
    return ch;
}

public List<Node> getChildren() {
    if (this.children == null) {
        return Collections.emptyList();
    }
    return children;
}

public boolean isEnd() {
    return end;
}

public void setEnd(boolean end) {
    this.end = end;
}

}

TrieNode Class

import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class WordTree {
Node root = new Node(' ');

public WordTree() {
}

/**
* Searches for a strings that match the prefix.
*
* @param prefix - prefix
* @return - list of strings that match the prefix, or empty list of no matches are        found.
*/
public List<String> getWordsForPrefix(String prefix) {
if (prefix.length() == 0) {
    return Collections.emptyList();
}
Node node = getNodeForPrefix(root, prefix);
if (node == null) {
    return Collections.emptyList();
}
List<LinkedList<Character>> chars = collectChars(node);
List<String> words = new ArrayList<String>(chars.size());
for (LinkedList<Character> charList : chars) {
    words.add(combine(prefix.substring(0, prefix.length() - 1), charList));
}
return words;
}


private String combine(String prefix, List<Character> charList) {
StringBuilder sb = new StringBuilder(prefix);
for (Character character : charList) {
    sb.append(character);
}
return sb.toString();
}


private Node getNodeForPrefix(Node node, String prefix) {
if (prefix.length() == 0) {
    return node;
}
Node next = node.getNode(prefix.charAt(0));
if (next == null) {
    return null;
}
return getNodeForPrefix(next, prefix.substring(1, prefix.length()));
}


private List<LinkedList<Character>> collectChars(Node node) {
List<LinkedList<Character>> chars = new ArrayList<LinkedList<Character>>();

if (node.getChildren().size() == 0) {
    chars.add(new LinkedList<Character>(Collections.singletonList(node.getChar())));
} else {
    if (node.isEnd()) {
        chars.add(new LinkedList<Character>    (Collections.singletonList(node.getChar())));
    }
    List<Node> children = node.getChildren();
    for (Node child : children) {
        List<LinkedList<Character>> childList = collectChars(child);

        for (LinkedList<Character> characters : childList) {
            characters.push(node.getChar());
            chars.add(characters);
        }
     }
    }
   return chars;
}


public void addWord(String word) {
addWord(root, word);
}

private void addWord(Node parent, String word) {
if (word.trim().length() == 0) {
    return;
}
Node child = parent.getNode(word.charAt(0));
if (child == null) {
    child = new Node(word.charAt(0));
    parent.addChild(child);
}
if (word.length() == 1) {
    child.setEnd(true);
} else {
    addWord(child, word.substring(1, word.length()));
}
}

public void load() {
    WordTree tree = new WordTree();
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(new    File("C:/Users/Sironka/Documents/NetBeansProjects/Final Maa Adaptive System/dictionary/Maa    Corpus.txt-02-ngrams-Freq.txt")));
        String eachLine = null;
        while ((eachLine = br.readLine()) != null) {
            tree.addWord(eachLine);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    System.out.println(tree.getWordsForPrefix("ore"));
  }


public static void main(String[] args) {

WordTree trieloader = new WordTree();
trieloader.load();
System.out.println("");
}
}

Here are the challenges:

    1. Changing the for loop format
    2. Classes like linkedList, Collections are not available in j2me

Request: 1. converting the above to j2me. (A similar j2me implementation will help).

Please assist me in this since i am completely stuck in my project. The trie will assist me in text prediction (t9).

Tom
  • 63
  • 5
  • 1
    related: [How to deal with the most common classes missing on J2ME](http://stackoverflow.com/questions/859449/how-to-deal-with-the-most-common-classes-missing-on-j2me) – gnat Apr 16 '12 at 14:55

1 Answers1

3

It seems there is no builtin implementation in J2ME but this code might be an option:

Just try the implementation mentioned in this post. You can download the code here.

Beachwalker
  • 7,685
  • 6
  • 52
  • 94
  • Thanks for the link, I can now import the linkedList. Converting the for loop to a form that can be used in j2me is now my major setback. Your further assistance will be highly appreciated – Tom Apr 16 '12 at 16:26
  • Please, correct me if I'm wrong... but AFAIK it is not possible to use for(each) in J2ME like you did (and know from java se). But you can "standard" for-loops instead. (the example from the link uses a while-loop, which can be used very similar to a for loop. – Beachwalker Apr 17 '12 at 08:21
  • Take a look here to get more infos about j2me restrictions: http://books.google.de/books?id=chepSC2iAB8C&printsec=frontcover&dq=j2me&hl=de&sa=X&ei=YiKNT4-cIePJ0QWY-8iODQ&ved=0CDUQ6AEwAA#v=snippet&q=loop&f=false – Beachwalker Apr 17 '12 at 08:22
  • ... and don't forget to upvote my post if you think it was helpful. ;-) (click the up arrow near my answer) – Beachwalker Apr 17 '12 at 08:23
  • Your post is helpful but have no such privileges yet. – Tom Apr 17 '12 at 17:09
  • ... you can earn some reputation by adding more info to your profile => increasing privileges – Beachwalker Apr 18 '12 at 11:20