标签:字符 前缀 har send 存储 stringbu class 字符串 append
public class TrieTree { private TrieNode root = new TrieNode(‘/‘);//存储无意义字符 //往Trie树中插入一个字符串 public void insert(char[] text) { TrieNode p = root; for(int i=0; i<text.length; i++) { int index = text[i] - ‘a‘; if(p.children[index] == null) { TrieNode newNode = new TrieNode(text[i]); p.children[index] = newNode; } p = p.children[index]; } p.isEndingChar = true; } //在Trie树中查找一个字符串 public boolean find(char[] pattern) { TrieNode p = root; for(int i=0; i<pattern.length; i++) { int index = pattern[i] - ‘a‘; if(p.children[index] == null) { return false;//不存在pattern } p = p.children[index]; } if(p.isEndingChar) {//完全匹配到pattern return true; }else {//不能完全匹配,只是前缀 return false; } } public class TrieNode{ public char data; public TrieNode[] children = new TrieNode[26]; public boolean isEndingChar = false; public TrieNode(char data) { this.data = data; } } public static void main(String[] args) { TrieTree trieTree = new TrieTree(); trieTree.insert("hello".toCharArray()); trieTree.insert("world".toCharArray()); trieTree.insert("word".toCharArray()); trieTree.insert("teacher".toCharArray()); trieTree.insert("wild".toCharArray()); String pattern = "word"; System.out.println(trieTree.find(pattern.toCharArray()) ? "找到了 " + pattern : "没有完全匹配的字符串 " + pattern); pattern = "wor"; System.out.println(trieTree.find(pattern.toCharArray()) ? "找到了 " + pattern : "没有完全匹配的字符串 " + pattern); } }
import java.util.ArrayList; import java.util.List; public class TrieTree { private TrieNode root = new TrieNode(‘/‘);//存储无意义字符 //往Trie树中插入一个字符串 public void insert(char[] text) { TrieNode p = root; for(int i=0; i<text.length; i++) { int index = text[i] - ‘a‘; if(p.children[index] == null) { TrieNode newNode = new TrieNode(text[i]); p.children[index] = newNode; } p = p.children[index]; } p.isEndingChar = true; } //在Trie树中查找一个字符串 public List<String> find(char[] pattern) { TrieNode p = root; for(int i=0; i<pattern.length; i++) { int index = pattern[i] - ‘a‘; if(p.children[index] == null) { return dfsResult;//不存在pattern } p = p.children[index]; } if(p.isEndingChar) {//完全匹配到pattern dfsResult.add(new String(pattern)); return dfsResult; }else {//不能完全匹配,只是前缀 String startPath = new String(pattern); //模式串 pattern的最后一个字符保存在p中,所以传入的path去掉该字符 dfs(p, new StringBuffer(startPath.substring(0, startPath.length()-1))); return dfsResult; } } private List<String> dfsResult = new ArrayList<String>(); private void dfs(TrieNode p, StringBuffer path) { if(p.isEndingChar) { dfsResult.add(new String(path.append(p.data).toString())); }else { for(int j=0; j<26; j++) { if(p.children[j] != null) { StringBuffer pathCopy = new StringBuffer(path.toString()); dfs(p.children[j], pathCopy.append(p.data)); } } } } public class TrieNode{ public char data; public TrieNode[] children = new TrieNode[26]; public boolean isEndingChar = false; public TrieNode(char data) { this.data = data; } } public static void main(String[] args) { TrieTree trieTree = new TrieTree(); trieTree.insert("hello".toCharArray()); trieTree.insert("world".toCharArray()); trieTree.insert("word".toCharArray()); trieTree.insert("teacher".toCharArray()); trieTree.insert("wild".toCharArray()); String pattern = "w"; List<String> findResult = trieTree.find(pattern.toCharArray()); for(String item : findResult) { System.out.println(item); } System.out.println("------------------"); trieTree.dfsResult.clear(); pattern = "wor"; findResult = trieTree.find(pattern.toCharArray()); for(String item : findResult) { System.out.println(item); } System.out.println("------------------"); trieTree.dfsResult.clear(); pattern = "word"; findResult = trieTree.find(pattern.toCharArray()); for(String item : findResult) { System.out.println(item); } System.out.println("------------------"); } }
标签:字符 前缀 har send 存储 stringbu class 字符串 append
原文地址:https://www.cnblogs.com/jiangwangxiang/p/11106262.html