How to create a simple prefix index in Java?

后端 未结 4 380
Happy的楠姐
Happy的楠姐 2020-12-19 13:01

I have big set of urls and I want to implement an autocompletion. I don\'t like the complexity of the naive approach as it is linear with the set size:

for(S         


        
相关标签:
4条回答
  • 2020-12-19 13:23

    Long time ago I put a simple Trie implementation here:

    http://code.google.com/p/triebag/source/browse/trunk/src/triebag/tries/SimpleTrie.java

    However this is not a compact Trie, so it creates one node per character, creating a compact one is a bit trickier.

    0 讨论(0)
  • 2020-12-19 13:26

    A great alternative algo is a ternary search tree (more memory efficient) https://github.com/varunpant/TernaryTree/tree/master/TernaryTree

    here is a trie in java http://algs4.cs.princeton.edu/52trie/TrieST.java.html

    0 讨论(0)
  • 2020-12-19 13:34

    The Regexp implementation java.util.regex.Pattern can efficiently handle prefixes:

    StringBuilder buffer = new StringBuilder();
    for (String prefix : prefixes) {
        if (buffer.length() > 0)
            buffer.append("|");
        buffer.append(prefix);
    }
    Pattern prefixPattern = Pattern.compile("^(" + buffer + ")");
    

    You can test all prefixes:

    boolean containsPrefix = prefixPattern.matcher(stringToTest).find();
    

    Note: for simplicity, prefix strings are not escaped. Regexp characters [, ], \, *, ?, $, ^, (, ), {, } and | have to be prefixed by \.

    0 讨论(0)
  • 2020-12-19 13:41

    If you need to efficiently find prefixes of strings, use a Trie, a data structure designed precisely for that purpose:

    A trie, or prefix tree, is an ordered tree data structure that is used to store an associative array where the keys are usually strings. Unlike a binary search tree, no node in the tree stores the key associated with that node; instead, its position in the tree defines the key with which it is associated. All the descendants of a node have a common prefix of the string associated with that node, and the root is associated with the empty string

    Two links with sample implementations.

    0 讨论(0)
提交回复
热议问题