【数据结构】Trie

需求

  • 如何判断一堆不重复的字符串是否以某个前缀开头?
    • 用Set\Map存储字符串
    • 遍历所有字符串进行判断
    • 时间复杂度: O(n)
  • 有没有更优的数据结构实现前缀搜索
    Trie

简介

在这里插入图片描述

  • Trie 也叫做字典树、前缀树(Prefix Tree)、单词查找树
  • Trie搜索字符串的效率主要跟字符串的长度有关
  • 假设使用Trie存储cat、dog、doggy、does、cast、add六个单词

在这里插入图片描述

接口设计

在这里插入图片描述
或者
在这里插入图片描述

实现类

public class Trie<V> {
	private int size;
	private Node<V> root;
	
	public int size() {
		return size;
	}

	public boolean isEmpty() {
		return size == 0;
	}

	/**
	 * 清空数据,根节点置空即可
	 */
	public void clear() {
		size = 0;
		root = null;
	}

	public V get(String key) {
		Node<V> node = node(key);
		return node != null && node.isEnd ? node.value : null;
	}

	public boolean contains(String key) {
		Node<V> node = node(key);
		return node != null && node.isEnd;
	}

	public V add(String key, V value) {
		keyCheck(key);
		
		// 创建根节点
		if (root == null) {
			root = new Node<>(null);
		}

		Node<V> node = root;
		int len = key.length();
		for (int i = 0; i < len; i++) {
			char c = key.charAt(i); 
			boolean emptyChildren = node.children == null;
			Node<V> childNode = emptyChildren ? null : node.children.get(c);
			if (childNode == null) {
				childNode = new Node<>(node);
				childNode.character = c;
				node.children = emptyChildren ? new HashMap<>() : node.children;
				node.children.put(c, childNode);
			}
			node = childNode;
		}

		// 已经存在这个单词
		if (node.isEnd) {
			V oldValue = node.value;
			node.value = value;
			return oldValue;
		}
		
		// 新增一个单词
		node.isEnd = true;
		node.value = value;
		size++;
		return null;
	}

	public V remove(String key) {
		// 找到最后一个节点
		Node<V> node = node(key);
		// 如果不是单词结尾,不用作任何处理
		if (node == null || !node.isEnd) {
			return null;
		}
		size--;
		V oldValue = node.value;
		
		// 如果还有子节点
		if (node.children != null && !node.children.isEmpty()) {
			node.isEnd = false;
			node.value = null;
			return oldValue;
		}
		
		// 如果没有子节点
		Node<V> parent = null;
		while ((parent = node.parent) != null) {
			parent.children.remove(node.character);
			//parent.isEnd: parent是否为一个单词的尾结点
			//parent.children.isEmpty(): parent是否还有子节点
			if (parent.isEnd || !parent.children.isEmpty()) {
				break;
			}
			node = parent;
		}
		
		return oldValue;
	}

	public boolean startsWith(String prefix) {
		return node(prefix) != null;
	}
	
	private Node<V> node(String key) {
		keyCheck(key);
		
		Node<V> node = root;
		int len = key.length();
		for (int i = 0; i < len; i++) {
			if (node == null || node.children == null || node.children.isEmpty()) {
				return null;
			}
			char c = key.charAt(i); 
			node = node.children.get(c);
		}
		
		return node;
	}

	//判断key是否为空
	private void keyCheck(String key) {
		if (key == null || key.length() == 0) {
			throw new IllegalArgumentException("key must not be empty");
		}
	}
	
	private static class Node<V> {
		Node<V> parent;
		HashMap<Character, Node<V>> children;
		Character character;
		V value;
		// 是否为单词的结尾(是否为一个完整的单词)
		boolean isEnd;
		public Node(Node<V> parent) {
			this.parent = parent;
		}
	}
}

测试类

Main.java

public class Main {

	static void test1() {
		Trie<Integer> trie = new Trie<>();
		trie.add("cat", 1);
		trie.add("dog", 2);
		trie.add("catalog", 3);
		trie.add("cast", 4);
		trie.add("hmx", 5);
		Asserts.test(trie.size() == 5);
		Asserts.test(trie.startsWith("do"));
		Asserts.test(trie.startsWith("c"));
		Asserts.test(trie.startsWith("ca"));
		Asserts.test(trie.startsWith("cat"));
		Asserts.test(trie.startsWith("cata"));
		Asserts.test(!trie.startsWith("hehe"));
		Asserts.test(trie.get("hmx") == 5);
		Asserts.test(trie.remove("cat") == 1);
		Asserts.test(trie.remove("catalog") == 3);
		Asserts.test(trie.remove("cast") == 4);
		Asserts.test(trie.size() == 2);
		Asserts.test(trie.startsWith("do"));
		Asserts.test(!trie.startsWith("c"));
	}

	public static void main(String[] args) {
		test1();
	}

}

Asserts.java

public class Asserts {
	public static void test(boolean value) {
		try {
			if (!value) {
				throw new Exception("测试未通过");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

总结

  • 优点:
    • 搜索前缀的效率主要跟前缀的长度有关
  • 缺点:
    • 需要耗费大量的内存,因此还有待改进
  • 与Trie相关的数据结构和算法
    • Double-array Trie、Suffix Tree、Patricia Tree、Crit-bit Tree、AC自动机
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值