zoukankan      html  css  js  c++  java
  • 实现 Trie (前缀树)

    题目:

    实现一个 Trie (前缀树),包含 insertsearch, 和 startsWith 这三个操作。

    Trie trie = new Trie();
    
    trie.insert("apple");
    trie.search("apple");   // 返回 true
    trie.search("app");     // 返回 false
    trie.startsWith("app"); // 返回 true
    trie.insert("app");   
    trie.search("app");     // 返回 true

    思路:

    Trie 树的结点结构

    Trie 树是一个有根的树,其结点具有以下字段:

    • 最多 R 个指向子结点的链接,其中每个链接对应字母表数据集中的一个字母。本文中假定 R 为 26,小写拉丁字母的数量。
    • 布尔字段,以指定节点是对应键的结尾还是只是键前缀。

    Trie 树中最常见的两个操作是键的插入和查找。

    向 Trie 树中插入键

    我们通过搜索 Trie 树来插入一个键。我们从根开始搜索它对应于第一个键字符的链接。有两种情况:

    • 链接存在。沿着链接移动到树的下一个子层。算法继续搜索下一个键字符。
    • 链接不存在。创建一个新的节点,并将它与父节点的链接相连,该链接与当前的键字符相匹配。

    重复以上步骤,直到到达键的最后一个字符,然后将当前节点标记为结束节点,算法完成。

    在 Trie 树中查找键

    每个键在 trie 中表示为从根到内部节点或叶的路径。我们用第一个键字符从根开始,。检查当前节点中与键字符对应的链接。有两种情况:

    • 存在链接。我们移动到该链接后面路径中的下一个节点,并继续搜索下一个键字符。
    • 不存在链接。若已无键字符,且当前结点标记为 isEnd,则返回 true。否则有两种可能,均返回 false :
      • 还有键字符剩余,但无法跟随 Trie 树的键路径,找不到键。
      • 没有键字符剩余,但当前结点没有标记为 isEnd。也就是说,待查找键只是Trie树中另一个键的前缀。

    查找 Trie 树中的键前缀

    该方法和查找基本相似,甚至更简单因为不需要考虑当前 Trie 节点是否用 “isend” 标记,因为我们搜索的是键的前缀,而不是整个键。

    代码
    package main
    
    import "fmt"
    
    type Trie struct {
    	next [26]*Trie
    	isEnd bool
    }
    
    
    /** Initialize your data structure here. */
    func Constructor() *Trie {
    	return &Trie{}
    }
    
    
    /** Inserts a word into the trie. */
    func (this *Trie) Insert(word string)  {
    	for _, v := range word {
    		if this.next[v-'a'] == nil {
    			this.next[v-'a'] = new(Trie)
    			this = this.next[v-'a']
    		}else {
    			this = this.next[v-'a']
    		}
    	}
    	this.isEnd = true
    }
    
    
    /** Returns if the word is in the trie. */
    func (this *Trie) Search(word string) bool {
    	for _, v := range word {
    		if this.next[v-'a'] == nil {
    			return false
    		}
    		this = this.next[v-'a']
    	}
    	if this.isEnd == false {
    		return false
    	}
    	return true
    }
    
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    func (this *Trie) StartsWith(prefix string) bool {
    	for _, v := range prefix {
    		if this.next[v-'a'] == nil {
    			return false
    		}
    		this = this.next[v-'a']
    	}
    	return true
    }
    
    func main() {
    
    	trie := Constructor()
    
    	trie.Insert("apple");
    
    	fmt.Println(trie.Search("apple"))  // 返回 true
    	fmt.Println(trie.Search("app"))     // 返回 false
    	fmt.Println(trie.StartsWith("app")) // 返回 true
    	trie.Insert("app")
    	fmt.Println(trie.Search("app"))     // 返回 true
    
    }
    

      地址:https://mp.weixin.qq.com/s/qkM88Ok-SY-QjoI-6hVCOw

    small_lei_it 技术无止境,追求更高。
  • 相关阅读:
    时间复杂度和空间复杂度的故事
    Go -- 并发编程的两种限速方法
    Go -- type 和断言 interface{}转换
    Go -- 实现二叉搜索树
    Go语言程序的状态监控
    Go -- 中开启gctrace
    Go --- GC优化经验
    Mysql 性能优化20个原则(4)
    Mysql 性能优化20个原则(3)
    查看 activex 组件的方法
  • 原文地址:https://www.cnblogs.com/smallleiit/p/13937875.html
Copyright © 2011-2022 走看看