1 package org.apache.lucene.search.suggest.tst;
5 public class TSTAutocomplete {
8 * Inserting keys in TST in the order middle,small,big (lexicographic measure)
9 * recursively creates a balanced tree which reduces insertion and search
10 * times significantly.
13 * Sorted list of keys to be inserted in TST.
15 * stores the lower index of current list.
17 * stores the higher index of current list.
19 * a reference object to root of TST.
21 public void balancedTree(Object[] tokens, Object[] vals, int lo, int hi,
22 TernaryTreeNode root) {
24 int mid = (lo + hi) / 2;
25 root = insert(root, (String) tokens[mid], vals[mid], 0);
26 balancedTree(tokens, vals, lo, mid - 1, root);
27 balancedTree(tokens, vals, mid + 1, hi, root);
31 * Inserts a key in TST creating a series of Binary Search Trees at each node.
32 * The key is actually stored across the eqKid of each node in a successive
36 * a reference node where the insertion will take currently.
38 * key to be inserted in TST.
40 * index of character in key to be inserted currently.
41 * @return currentNode The new reference to root node of TST
43 public TernaryTreeNode insert(TernaryTreeNode currentNode, String s,
45 if (s == null || s.length() <= x) {
48 if (currentNode == null) {
49 TernaryTreeNode newNode = new TernaryTreeNode();
50 newNode.splitchar = s.charAt(x);
51 currentNode = newNode;
52 if (x < s.length() - 1) {
53 currentNode.eqKid = insert(currentNode.eqKid, s, val, x + 1);
55 currentNode.token = s;
56 currentNode.val = val;
59 } else if (currentNode.splitchar > s.charAt(x)) {
60 currentNode.loKid = insert(currentNode.loKid, s, val, x);
61 } else if (currentNode.splitchar == s.charAt(x)) {
62 if (x < s.length() - 1) {
63 currentNode.eqKid = insert(currentNode.eqKid, s, val, x + 1);
65 currentNode.token = s;
66 currentNode.val = val;
70 currentNode.hiKid = insert(currentNode.hiKid, s, val, x);
76 * Auto-completes a given prefix query using Depth-First Search with the end
77 * of prefix as source node each time finding a new leaf to get a complete key
78 * to be added in the suggest list.
81 * a reference to root node of TST.
83 * prefix query to be auto-completed.
85 * index of current character to be searched while traversing through
87 * @return suggest list of auto-completed keys for the given prefix query.
89 public ArrayList<TernaryTreeNode> prefixCompletion(TernaryTreeNode root,
92 TernaryTreeNode p = root;
93 ArrayList<TernaryTreeNode> suggest = new ArrayList<TernaryTreeNode>();
96 if (s.charAt(x) < p.splitchar) {
98 } else if (s.charAt(x) == p.splitchar) {
99 if (x == s.length() - 1) {
110 if (p == null) return suggest;
111 if (p.eqKid == null && p.token == null) return suggest;
112 if (p.eqKid == null && p.token != null) {
117 if (p.token != null) {
122 Stack<TernaryTreeNode> st = new Stack<TernaryTreeNode>();
124 while (!st.empty()) {
125 TernaryTreeNode top = st.peek();
127 if (top.token != null) {
130 if (top.eqKid != null) {
133 if (top.loKid != null) {
136 if (top.hiKid != null) {