class Node {
    protected static final int ALPHABET_SIZE = 26;
    int outDegree; // the number of children a node has
    boolean terminal; // true if node represents the last letter in a word
    Children[] node;

    Node() {
        outDegree = 0;
        terminal = false;
        node = new Children[ALPHABET_SIZE];
    }
}

class Children extends Node {
    Children(int outDegree, boolean terminal) {
        this.outDegree = outDegree;
        this.terminal = terminal;
        node = new Children[ALPHABET_SIZE];
    }
}

class Trie {
    Node head;
    private int membership = 0;
    // initialized and used by listAll to build the current dictionary of words in the Trie.
    private String[] dictionary;
    private int dictIndex;

    Trie() {
        head = new Node();
    }

    public boolean insert(String word) {
        boolean placed = insert(word, head);
        if (placed)
            membership++;
        return placed;
    }

    private boolean insert(String word, Node x) {
        if (word.length() == 1)
            return insertLastLetter(word, x);

        int i = getFirstLetterIndex(word);
        String prefix = word.substring(1);
        if (x.node[i] == null) {
            x.node[i] = new Children(0, false);
            x.outDegree++;
        }
        return insert(prefix, x.node[i]);
    }

    private boolean insertLastLetter(String word, Node x) {
        int i = getFirstLetterIndex(word);
        if (isTerminal(x, i))
            return false; // word already inserted
        else if (letterIsPresent(x, i)) {
            x.node[i].terminal = true;
            return true;
        } else {
            x.node[i] = new Children(0, true);
            x.outDegree++;
            return true;
        }
    }

    public boolean isPresent(String word) {
        Node current = head;

        while (word.length() > 0) {
            int firstLetterIndex = getFirstLetterIndex(word);
            word = (word.length() > 0) ? word.substring(1) : "";

            if (word.length() == 0 && isTerminal(current, firstLetterIndex))
                return true;
            else if (letterIsPresent(current, firstLetterIndex))
                current = current.node[firstLetterIndex];
            else
                return false;
        }
        return false;
    }

    public boolean delete(String word) {
        int i = getFirstLetterIndex(word);
        head.node[i] = null;
        boolean removed = head.node[i] == null;
        if (removed)
            membership--;
        return removed;
    }

    private int getFirstLetterIndex(String word) {
        return word.charAt(0) - 'a';
    }

    public void listAll() {
        dictIndex = 0;
        dictionary = new String[membership];
        buildDictionary(head, "");

        for (String word : dictionary)
            System.out.println(word);
    }

    private void buildDictionary(Node x, String s) {
        int i = 0;
        int outFound = 0;

        while (outFound < x.outDegree) {
            if (letterIsPresent(x, i)) {
                if (isTerminalWithOutDegree(x, i)) {
                    String completeWord = s + getLetter(i);
                    addToDictionary(completeWord);
                    buildDictionary(x.node[i], completeWord);
                } else if (isTerminal(x, i)) {
                    String completeWord = s + getLetter(i);
                    addToDictionary(completeWord);
                } else
                    buildDictionary(x.node[i], s + getLetter(i));

                outFound++;
            }
            i++;
        }
    }

    private void addToDictionary(String word) {
        dictionary[dictIndex] = word;
        dictIndex++;
    }

    private boolean isTerminalWithOutDegree(Node x, int i) {
        return isTerminal(x, i) && x.node[i].outDegree > 0;
    }

    private boolean isTerminal(Node x, int i) {
        return x.node[i] != null && x.node[i].terminal;
    }

    private char getLetter(int i) {
        return (char) ('a' + i);
    }

    private boolean letterIsPresent(Node x, int i) {
        return x.node[i] != null;
    }

    public int membership() {
        return membership;
    }
}

public class WIBUP2 {
    public static void main(String[] args) {

    }
}

@@ -1480,32 +1480,34 @@
 erIndex(word);%0D%0A
+%0D%0A
         if (isTe
@@ -3171,80 +3171,22 @@
     
-if (isTerminalWithOutDegree(x, i)) %7B%0D%0A                    String complet
+String possibl
 eWor
@@ -3228,40 +3228,46 @@
     
-    addToDictionary(complet
+processPossibleWord(x, possibl
 eWord
+, i
 );%0D%0A
@@ -3286,52 +3286,18 @@
     
-   
- buildDictionary(x.node%5Bi%5D, completeWord)
+outFound++
 ;%0D%0A 
@@ -3311,55 +3311,37 @@
     
+%7D%0D%0A
     
-%7D else if (isTerminal(x, i)) %7B%0D%0A   
+        i++;%0D%0A
         
     
@@ -3340,66 +3340,131 @@
     
+%7D%0D%0A
     
-     String completeWord = s + getLetter(i);%0D%0A        
+%7D%0D%0A%0D%0A    private void processPossibleWord(Node x, String s, int i) %7B%0D%0A        if (isTerminalWithOutDegree(x, i)) %7B%0D%0A
     
@@ -3491,20 +3491,9 @@
 ary(
-completeWord
+s
 );%0D%0A
@@ -3508,115 +3508,108 @@
     
-    %7D else%0D%0A                    buildDictionary(x.node%5Bi%5D, s + getLetter(i));%0D%0A%0D%0A                outFound++
+buildDictionary(x.node%5Bi%5D, s);%0D%0A        %7D else if (isTerminal(x, i))%0D%0A            addToDictionary(s)
 ;%0D%0A 
@@ -3607,37 +3607,36 @@
 ry(s);%0D%0A        
-    %7D
+else
 %0D%0A            i+
@@ -3629,39 +3629,54 @@
             
-i++;%0D%0A        %7D
+buildDictionary(x.node%5Bi%5D, s);
 %0D%0A    %7D%0D%0A%0D%0A 

refactor buildDictionary