1. 程式人生 > 實用技巧 >將git修改的檔案按照原來的目錄層次全部複製出來【轉】

將git修改的檔案按照原來的目錄層次全部複製出來【轉】

資料結構:字典樹

class TrieNode{
      private TrieNode[] children;
      private int size = 26;
      private boolean isEnd = false;
      public TrieNode(){
            children = new TrieNode[size];
      }
      public boolean containsKey(char c){
            return children[c-'a'] != null;
      }
      public TrieNode get(char c){
            return children[c-'a'];
      }
      public void put(char c,TireNode node){
            children[c-'a'] = node;
      }
      public void setEnd(){
            this.isEnd = true;
      }
      public boolean isEnd(){
            return isEnd;
      }
}
class Trie{
      private TrieNode root;
      // inital
      public Trie(){
            root = new TrieNode();
      }
      // insert a word into the trie
      public void insert(String word){
            TrieNode node = root;
            for(int i = 0;i<word.length();i++){
                  char c = word.charAt(i);
                  if(!node.containsKey(c)){
                        node.put(c,new TrieNode());
                  }
                  node = node.get(c);
            }
            node.setEnd();
      }
      // search a prefix or whole key in the trie and return the node where search ends
      public TrieNode searchPrefix(String word){
            TrieNode node = root;
            for(int i = 0;i<word.length();i++){
                  char c = word.charAt(i);
                  if(node.containsKey(c)){
                        node = node.get(c);
                  }else{
                        return null;
                  }
            }
            return node;
      }
      // return if the word is in the trie
      public boolean search(String word){
            TrieNode node = searchPrefix(word);
            return node!=null && node.isEnd();
      }      
      // return if there is any word in the trie that start with the prefix 
      public boolean startWith(String prefix){
            TrieNode node = searchPrefix(prefix);
            return node!=null;
      }
}