• tinys
    2018-11-17
    老师可能不熟java..我提一个无关紧要的建议..
    一般java的构造函数直接声明属性就好..不要再在里面new自己了..
     public TrieNode(char val) {
            this.val = val;
        }

    然后老师讲得挺好的..赞一个..
    展开
     1
     8
  • Douglas
    2019-02-12
    个人觉得java 更有助于理解,算法还是细节处理比较重要,也有可能是不懂python的原因
    
     2
  • dfx
    2019-04-20
    提供一个go版本
    type Trie struct {
        children map[rune]*Trie
        isEnd bool
    }

    func Constructor() *Trie {
        return &Trie{map[rune]*Trie{}, false}
    }

    func (t *Trie) Insert(word string) {
        node := t
        for _, char := range word {
            nextNode, ok := node.children[char]
            if !ok {
                nextNode = Constructor()
                node.children[char] = nextNode
            }
            node = nextNode
        }
        node.isEnd = true
    }

    func (t *Trie) Search(word string) bool {
        node := t
        for _, char := range word {
            nextNode, ok := node.children[char]
            if !ok {
                return false
            }
            node = nextNode
        }
        return node.isEnd
    }

    func (t *Trie) StartWith(prefix string) bool {
        node := t
        for _, char := range prefix {
            nextNode, ok := node.children[char]
            if !ok {
                return false
            }
            node = nextNode
        }
        return true
    }
    展开

    作者回复: 赞!感谢贡献

    
     1
  • 狮子🦁️独白
    2018-11-19
    Java 实例为啥不用Map替代数组? Java Map就可以实现 Python dict同样的功能了。

    作者回复: Java也可以用map的,只是正好想用java语言来说明数组的定义示例。

    
     1
  • Aliliin
    2019-11-20
    PHP 解:
    class Node
    {
        public $is_end = false;
        public $child = [];
    }

    class Trie
    {
        private $root = null;

        function __construct()
        {
            $this->root = new Node();

        }

        function insert($word)
        {
            $node = $this->root;
            for ($i = 0; $i < strlen($word); $i++) {
                if (!isset($node->child[$word[$i]])) {
                    $node->child[$word[$i]] = new Node();
                }
                $node = $node->child[$word[$i]];
                if ($i == strlen($word) - 1) {
                    $node->is_end = true;
                }
            }
        }

        function search($word)
        {
            $node = $this->root;
            for ($i = 0; $i < strlen($word); $i++) {
                if (isset($node->child[$word[$i]])) {
                    $node = $node->child[$word[$i]];
                } else {
                    return false;
                }
            }

            return $node->is_end;
        }

        function startsWith($prefix)
        {
            $node = $this->root;
            for ($i = 0; $i < strlen($prefix); $i++) {
                if (isset($node->child[$prefix[$i]])) {
                    $node = $node->child[$prefix[$i]];
                } else {
                    return false;
                }
            }
            return true;
        }
    }
    展开
    
    
  • 文静
    2019-10-08
    TrieNode 定义成这样就行,不需要那个 val....
    public class TrieNode {
        public boolean theEnd;
        public TrieNode[] children = new TrieNode[26];
        public TrieNode() {}
    }
    展开

    作者回复: 正确。的确这里有可以优化的地方。

    
    
  • yunfei_lei
    2019-04-29
    中文用字典树该怎么处理呢?

    作者回复: 不能简单套用字母树的办法。

    
    
  • wind
    2019-04-09
    search和startWith部分的代码重复了, 应该可以search调用startWith, 再与上一个是否是结尾.
    
    
  • Null
    2019-04-03
    是我没有理解还是咋了,按照老师提供的代码,我先insert(ant),然后搜索startWith(at)是会返回yes,难道我理解的有问题,感觉并没有记录层的感觉。
    
    
  • 预见
    2018-11-21
    同意一楼,直接用 java map,而不用数组,是不是效率更高呢

    作者回复: 内存利用率会更好,但是时间性能会弱于数组的情况。

    
    
我们在线,来聊聊吧