[数据结构]TrieTree

定义

(一)前缀树作用

  • 基本作用

    给定一系列的字符串,判断有没有以某些字符开头的字符串

  • 扩充用法1

    已有一系列字符串中是否包含某个字符串

    方案:在每一个节点上加上一个数据项,该数据项表示由多少字符串是以当前字符结尾的。

  • 扩充用法2

    给定一系列字符串,查询有多少字符串是以当前字符作为前缀的

    方案:在每个节点上再加上一个数据项,该数据项表示加入所有字符串时候划过当前节点的次数;

(二)生成前缀树

将字符串不断的加入前缀树中,默认刚开始树的结构只有头结点。然后在加入新字符串时候,判断当前有没有通向该结点的路径,如果没有就新建否则就复用。

示例问题

  • 一个字符串类型的数组 arr1,另一个字符串类型的数组 arr2,arr2 中有哪些字符是在 arr1 中出现的?请打印。

  • arr2 中有哪些字符是作为 arr1 中某个字符串前缀出现的?请打印。

  • arr2 中有哪些字符是作为 arr1 中某个字符串前缀出现的?请打印 arr2 中出现次数最大的前缀。

实现

package nowcoder.easy.class_07;

public class Code_01_TrieTree {

	public static class TrieNode {
		// 建立的时候经过结点的次数
		public int path;
		// 有多少字符串是以该结点结尾的
		public int end;
		// 一共有多少路,这里是 26 个字母,所有规定了 26 条路,通过标记该路的子节点是否为空来判断树是否存在
		public TrieNode[] nexts;

		public TrieNode() {
			path = 0;
			end = 0;
			nexts = new TrieNode[26];
		}
	}

	public static class Trie {
		private TrieNode root;
// 创建头结点
		public Trie() {
			root = new TrieNode();
		}

		public void insert(String word) {
			if (word == null) {
				return;
			}
			//把word变成char数组
			char[] chs = word.toCharArray();
			TrieNode node = root;
			int index = 0;
			for (int i = 0; i < chs.length; i++) {
				index = chs[i] - 'a';
				// 是否有指向该结点的路,没有就新建。
				if (node.nexts[index] == null) {
					node.nexts[index] = new TrieNode();
				}
				node = node.nexts[index];
				node.path++;
			}
			node.end++;
		}

		public void delete(String word) {
			if (search(word) != 0) {
				char[] chs = word.toCharArray();
				TrieNode node = root;
				int index = 0;
				for (int i = 0; i < chs.length; i++) {
					index = chs[i] - 'a';
					// 删除肯定 path -1 ,如果为 0,则下面结点就找不到了,置为空
					//这里应对的情况是,该节点下面只剩一条单链没有分支了
					if (--node.nexts[index].path == 0) {
						node.nexts[index] = null;
						return;
					}
					node = node.nexts[index];
				}
				node.end--;
			}
		}
// 是否包括该字符串(扩展一)
		public int search(String word) {
			if (word == null) {
				return 0;
			}
			char[] chs = word.toCharArray();
			TrieNode node = root;
			int index = 0;
			for (int i = 0; i < chs.length; i++) {
				index = chs[i] - 'a';
				//是否有该路径
				if (node.nexts[index] == null) {
					return 0;
				}
				node = node.nexts[index];
			}
			//返回end值说明有多少个这个字符串加入过这个前缀树
			return node.end;
		}

		//包含该前缀有多少个
		public int prefixNumber(String pre) {
			if (pre == null) {
				return 0;
			}
			char[] chs = pre.toCharArray();
			TrieNode node = root;
			int index = 0;
			for (int i = 0; i < chs.length; i++) {
				index = chs[i] - 'a';
				if (node.nexts[index] == null) {
					return 0;
				}
				node = node.nexts[index];
			}
			return node.path;
		}
	}

	public static void main(String[] args) {
		Trie trie = new Trie();
		System.out.println(trie.search("zuo"));
		trie.insert("zuo");
		System.out.println(trie.search("zuo"));
		trie.delete("zuo");
		System.out.println(trie.search("zuo"));
		trie.insert("zuo");
		trie.insert("zuo");
		trie.delete("zuo");
		System.out.println(trie.search("zuo"));
		trie.delete("zuo");
		System.out.println(trie.search("zuo"));
		trie.insert("zuoa");
		trie.insert("zuoac");
		trie.insert("zuoab");
		trie.insert("zuoad");
		trie.delete("zuoa");
		System.out.println(trie.search("zuoa"));
		System.out.println(trie.prefixNumber("zuo"));

	}

}

结果为:

0
1
0
1
0
0
3
posted @ 2020-02-24 19:56  KrisTse  阅读(171)  评论(0编辑  收藏  举报