Search in sources :

Example 6 with NodeVisitor

use of org.trie4j.NodeVisitor in project trie4j by takawitter.

the class PatriciaTrieWikipediaTest method afterVerification.

@Override
protected void afterVerification(Trie trie) throws Exception {
    final AtomicInteger nodes = new AtomicInteger();
    final AtomicInteger leaves = new AtomicInteger();
    Algorithms.traverseByDepth(trie.getRoot(), new NodeVisitor() {

        @Override
        public boolean visit(Node node, int nest) {
            if (node.isTerminate())
                leaves.incrementAndGet();
            else
                nodes.incrementAndGet();
            return true;
        }
    });
    System.out.println(String.format("%d nodes and %d leaves", nodes.intValue(), leaves.intValue()));
    super.afterVerification(trie);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Node(org.trie4j.Node) NodeVisitor(org.trie4j.NodeVisitor)

Example 7 with NodeVisitor

use of org.trie4j.NodeVisitor in project trie4j by takawitter.

the class BitVectorExp method main.

public static void main(String[] args) throws Exception {
    Trie trie = new PatriciaTrie();
    int c = 0;
    // You can download archive from http://dumps.wikimedia.org/jawiki/latest/
    LapTimer t = new LapTimer();
    for (String word : new WikipediaTitles()) {
        trie.insert(word);
        c++;
        if (c == maxCount)
            break;
    }
    t.lapMillis("trie building done. %d words.", c);
    final BytesSuccinctBitVector bv = new BytesSuccinctBitVector(5000000);
    final AtomicInteger nodeCount = new AtomicInteger();
    Algorithms.traverseByDepth(trie.getRoot(), new NodeVisitor() {

        @Override
        public boolean visit(Node node, int nest) {
            Node[] children = node.getChildren();
            if (children != null) {
                int n = node.getChildren().length;
                for (int i = 0; i < n; i++) {
                    bv.append(true);
                }
            }
            bv.append(false);
            nodeCount.incrementAndGet();
            return true;
        }
    });
    trie = null;
    t.lapMillis("done. %d nodes inserted. do rank and select", nodeCount.intValue());
    for (int i = 0; i < c; i += 100) {
        int count = bv.rank(i, true);
        bv.select(count, true);
    }
    t.lapMillis("done.");
    Thread.sleep(10000);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PatriciaTrie(org.trie4j.patricia.PatriciaTrie) Node(org.trie4j.Node) BytesSuccinctBitVector(org.trie4j.bv.BytesSuccinctBitVector) WikipediaTitles(org.trie4j.test.WikipediaTitles) Trie(org.trie4j.Trie) PatriciaTrie(org.trie4j.patricia.PatriciaTrie) LapTimer(org.trie4j.test.LapTimer) NodeVisitor(org.trie4j.NodeVisitor)

Example 8 with NodeVisitor

use of org.trie4j.NodeVisitor in project trie4j by takawitter.

the class MapTailPatriciaTrieWithConcatTailBuilderWikipediaTest method afterVerification.

@Override
protected void afterVerification(Trie trie) throws Exception {
    final AtomicInteger nodes = new AtomicInteger();
    final AtomicInteger leaves = new AtomicInteger();
    Algorithms.traverseByDepth(trie.getRoot(), new NodeVisitor() {

        @Override
        public boolean visit(Node node, int nest) {
            if (node.isTerminate())
                leaves.incrementAndGet();
            else
                nodes.incrementAndGet();
            return true;
        }
    });
    System.out.println(String.format("%d nodes and %d leaves", nodes.intValue(), leaves.intValue()));
    super.afterVerification(trie);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Node(org.trie4j.Node) NodeVisitor(org.trie4j.NodeVisitor)

Aggregations

Node (org.trie4j.Node)8 NodeVisitor (org.trie4j.NodeVisitor)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Trie (org.trie4j.Trie)2 PatriciaTrie (org.trie4j.patricia.PatriciaTrie)2 LapTimer (org.trie4j.test.LapTimer)2 WikipediaTitles (org.trie4j.test.WikipediaTitles)2 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 BytesSuccinctBitVector (org.trie4j.bv.BytesSuccinctBitVector)1