Trie树,Java实现

import  java.util.ArrayList;
import  java.util.List;


/**
 * A word trie which can only deal with 26 alphabeta letters.
 * 
@author Leeclipse
 * 
@since 2007-11-21
 
*/

public   class  Trie  {
    
    
private Vertex root;

    
protected class Vertex {
        
protected int words;
        
protected int prefixes;
        
protected Vertex[] edges;
        Vertex() 
{
            words 
= 0;
            prefixes 
= 0;
            edges 
= new Vertex[26];
            
for (int i = 0; i < edges.length; i++{
                edges[i] 
= null;
            }

        }

    }

    
    
public Trie () {
        root 
= new Vertex();
    }

    
    
/**
     * List all words in the Trie.
     * 
     * 
@return
     
*/

    
public List<String> listAllWords() {
        
        List
<String> words = new ArrayList<String>();
        
        Vertex[] edges 
= root.edges;
        
        
for (int i = 0; i < edges.length; i++{
            
if (edges[i] != null{
                String word 
= "" + (char)('a' + i);
                depthFirstSearchWords(words, edges[i], word);
            }

        }
        
        
return words;
    }

    
    
public int countPrefixes(String prefix) {
        
return countPrefixes(root, prefix);
    }

    
private int countPrefixes(Vertex vertex, String prefixSegment) {
        
if (prefixSegment.length() == 0//reach the last character of the word
            return vertex.prefixes;
        }

        
char c = prefixSegment.charAt(0);
        
int index = c - 'a';
        
if (vertex.edges[index] == null// the word does NOT exist
            return 0;
        }
 else {
            
return countPrefixes(vertex.edges[index], prefixSegment.substring(1));
        }
        
    }

    
    
public int countWords(String word) {
        
return countWords(root, word);
    }
    
    
private int countWords(Vertex vertex, String wordSegment) {
        
if (wordSegment.length() == 0//reach the last character of the word
            return vertex.words;
        }

        
char c = wordSegment.charAt(0);
        
int index = c - 'a';
        
if (vertex.edges[index] == null// the word does NOT exist
            return 0;
        }
 else {
            
return countWords(vertex.edges[index], wordSegment.substring(1));
        }
        
    }

    
    
/**
     * Depth First Search words in the Trie and add them to the List.
     * 
     * 
@param words
     * 
@param vertex
     * 
@param wordSegment
     
*/

    
private void depthFirstSearchWords(List<String> words, Vertex vertex, String wordSegment) {
        Vertex[] edges 
= vertex.edges;
        
boolean hasChildren = false;
        
for (int i = 0; i < edges.length; i++{
            
if (edges[i] != null{
                hasChildren 
= true;
                String newWord 
= wordSegment + (char)('a' + i);                
                depthFirstSearchWords(words, edges[i], newWord);
            }
            
        }

        
if (!hasChildren) {
            words.add(wordSegment);
        }

    }

    
    
/**
     * Add a word to the Trie.
     * 
     * 
@param word The word to be added.
     
*/

    
public void addWord(String word) {
        addWord(root, word);
    }

    
    
/**
     * Add the word from the specified vertex.
     * 
@param vertex The specified vertex.
     * 
@param word The word to be added.
     
*/

    
private void addWord(Vertex vertex, String word) {
        
if (word.length() == 0//if all characters of the word has been added
            vertex.words ++;
        }
 else {
            vertex.prefixes 
++;
            
char c = word.charAt(0);
            c 
= Character.toLowerCase(c);
            
int index = c - 'a';
            
if (vertex.edges[index] == null//if the edge does NOT exist
                vertex.edges[index] = new Vertex();
            }

            addWord(vertex.edges[index], word.substring(
1)); //go the the next character
        }

    }

      

}

 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值