Export to GitHub

radixtree - issue #6

Add a method that would complete a given string to the point where the ambiguity starts


Posted on Sep 17, 2008 by Grumpy Lion

It would be nice if the interface contained a method, like

String complete(String prefix)

that would "complete" the prefix to the point where the ambiguity starts.

Say, the tree contains: "blah1" and "blah2"; then complete("bl") would return "blah".

Comment #1

Posted on Sep 17, 2008 by Grumpy Lion

/* One possible implementation */

public String complete(String prefix) {
    return complete(prefix, root, "");
}

private String complete(String key, RadixTreeNode<T> node, String base) {
    int i = 0;
    int keylen = key.length();
    int nodelen = node.getKey().length();

    while (i < keylen && i < nodelen) {
        if (key.charAt(i) != node.getKey().charAt(i)) {
            break;
        }
        i++;
    }

    if (i == keylen && i <= nodelen) {
        return base + node.getKey();
    }
    else if (nodelen == 0 || (i < keylen && i >= nodelen)) {
        String beginning = key.substring(0, i);
        String ending = key.substring(i, keylen);
        for (RadixTreeNode<T> child : node.getChildern()) {
            if (child.getKey().startsWith(ending.charAt(0) + "")) {
                return complete(ending, child, base + beginning);
            }
        }
    }
    return null;
}

Comment #2

Posted on Sep 18, 2008 by Quick Camel

Thanks a lot for your contribution. I will write some unit test for this one and then i will include it :)

Comment #3

Posted on Oct 6, 2009 by Quick Camel

(No comment was entered for this change.)

Status: Fixed

Labels:
Type-Defect Priority-Medium