2012-04-16 3 views
2

J'ai trouvé une implémentation java d'un trie et j'aimerais en avoir une similaire dans J2ME. Voici le code.Equivalence LinkedList dans J2ME

classe Node

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 Classe

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(""); 
} 
} 

Voici les défis:

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

Demande: 1. conversion e e ci-dessus à j2me. (Une implémentation similaire de j2me aidera).

S'il vous plaît aidez-moi car je suis complètement coincé dans mon projet. Le trie m'aidera dans la prédiction de texte (t9).

+1

related: [Comment gérer les classes les plus courantes manquantes sur J2ME] (http://stackoverflow.com/questions/859449/how-to-deal-with-the-most-common-classes-missing-on -j2me) – gnat

Répondre

3

Il semble qu'il n'y a pas de mise en œuvre dans J2ME mais builtin ce code pourrait être une option:

Juste essayer la mise en œuvre mentionnée dans this post. Vous pouvez télécharger le code here.

+0

Merci pour le lien, je peux maintenant importer le linkedList. Convertir la boucle for en une forme qui peut être utilisée dans j2me est maintenant mon revers majeur. Votre aide supplémentaire sera très appréciée – Tom

+0

S'il vous plaît, corrigez-moi si je me trompe ... mais je vous le rappelle, il n'est pas possible de l'utiliser pour (chaque) dans J2ME comme vous l'avez fait (et je sais de java se). Mais vous pouvez utiliser des boucles "standard" à la place. (L'exemple du lien utilise une boucle while, qui peut être très similaire à une boucle for.) – Beachwalker

+0

Jetez un oeil ici pour obtenir plus d'infos sur les restrictions de j2me: 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