package udo.logic;

public class TernarySearchTree {
    public static class Node {
        private char myChar;
        private Node left, right, center;
        private Boolean isWordEnd;
        
        public Node(char myChar, Boolean isWordEnd) {
            this.myChar = myChar;
            this.isWordEnd = isWordEnd;
        }
    }
    
    private Node root = null;
    
    private Node add(String s, Integer pos, Node node) {
        if (node == null) {
            node = new Node(s.charAt(pos), false);
        }
        
        if (s.charAt(pos) < node.myChar) {
            node.left = add(s, pos, node.left);
        } else if (s.charAt(pos) > node.myChar) {
            node.right = add(s, pos, node.right);
        } else {
            if (pos + 1 == s.length()) {
                node.isWordEnd = true;
            } else {
                node.center = add(s, pos + 1, node.center); 
            }
        }
        
        return node;
    }
    
    public void add(String s) {
        if (s == null) {
            return;
        }
        
        s = s.trim();
        if (s.equalsIgnoreCase("")) {
            return;
        }
        
        root = add(s, 0, root);
    }

    public static void main(String[] args) {
        
    }

}

@@ -13,16 +13,40 @@
 logic;%0A%0A
+import java.util.List;%0A%0A
 public c
@@ -439,19 +439,21 @@
 Integer 
-pos
+index
 , Node n
@@ -524,19 +524,21 @@
 .charAt(
-pos
+index
 ), false
@@ -580,19 +580,21 @@
 .charAt(
-pos
+index
 ) %3C node
@@ -631,27 +631,29 @@
 ft = add(s, 
-pos
+index
 , node.left)
@@ -682,19 +682,21 @@
 .charAt(
-pos
+index
 ) %3E node
@@ -738,19 +738,21 @@
  add(s, 
-pos
+index
 , node.r
@@ -791,19 +791,21 @@
     if (
-pos
+index
  + 1 == 
@@ -919,11 +919,13 @@
 (s, 
-pos
+index
  + 1
@@ -1238,16 +1238,1150 @@
 );%0A    %7D
+%0A    %0A    public List%3CString%3E searchPrefix(String prefix, Integer numWords) %7B%0A        int index = 0;%0A        Node curNode = root;%0A        %0A        while (curNode != null) %7B%0A            if (index == prefix.length()) %7B%0A                return searchTree(curNode, numWords);%0A            %7D%0A            %0A            if (prefix.charAt(index) %3C curNode.myChar) %7B%0A                curNode = curNode.left;%0A            %7D else if (prefix.charAt(index) %3E curNode.myChar) %7B%0A                curNode = curNode.right;%0A            %7D else %7B%0A                index++;%0A                curNode = curNode.center;%0A            %7D%0A        %7D%0A        %0A        return null;%0A    %7D%0A    %0A    public List%3CString%3E searchPrefix(String prefix) %7B%0A        return searchPrefix(prefix, null);%0A    %7D%0A    %0A    /**%0A     * Find words in the tree rooted at some node%0A     * @param node the root of the tree for the search%0A     * @param numWords maximum number of words to search for or%0A     *                 search for all words if this is null%0A     * @return list of found words%0A     */%0A    private List%3CString%3E searchTree(Node node, Integer numWords) %7B%0A        return null;%0A    %7D
 %0A%0A    pu

Add search prefix method to ternary search tree