Пример #1
0
        public void GetChildNode_ChildForSpecifiedItemDoesntExist()
        {
            TrieNode <Char> childNode1 = new TrieNode <Char>('B');

            testTrieNode.AddChild(childNode1);

            ArgumentException e = Assert.ThrowsException <ArgumentException>(() =>
            {
                testTrieNode.GetChildNode('C');
            });

            StringAssert.StartsWith(e.Message, "The node does not contain a child for item 'C'.");
            Assert.AreEqual("childItem", e.ParamName);
        }
Пример #2
0
        public void GetChildTest()
        {
            // Arrange
            var trieNode = new TrieNode();

            trieNode.AddChild('a');
            trieNode.AddChild('b');

            // Act
            var tn = trieNode.GetChild('b');

            // Assert
            Assert.Empty(tn.children);
        }
Пример #3
0
        public void AddChildTest()
        {
            // Arrange
            var trieNode = new TrieNode();

            // Act
            trieNode.AddChild('a');
            trieNode.AddChild('b');

            // Assert
            Assert.Equal(2, trieNode.children.Count);
            Assert.Equal('a', trieNode.children.Keys.First());
            Assert.Equal('b', trieNode.children.Keys.Last());
        }
Пример #4
0
    /// <summary>
    /// 插入节点
    /// 插入时,单词查找树不存在该字符,则需要创建一个新的节点来保存该字符,此时词频为 1
    /// 插入时,单词查找树中已经存在此字符,则进入下一步,此时词频+1
    /// </summary>
    public void Insert(string value)
    {
        if (value.Length <= 0)
        {
            return;
        }

        TrieNode node = head;

        for (int i = 0; i < value.Length; i++)
        {
            char     c     = value[i];
            TrieNode child = node.GetChild(c);
            //该字符并没有在相应的孩子节点中
            if (child == null)
            {
                var isEnd = i == value.Length - 1;
                child = new TrieNode(c, isEnd);
                node.AddChild(c, child);
            }

            node.PrefixCount++;
            node = child;
        }
        node.WordCount++;
    }
Пример #5
0
    private TrieNode AddWord(string word, TrieNode root)
    {
        char currentChar = word[0];

        TrieNode currentNode = root.GetChild(currentChar);

        if (currentNode == null)
        {
            currentNode        = new TrieNode(currentChar);
            currentNode.Parent = root;
            root.AddChild(currentNode);
        }

        string wordTail = word.Substring(1);

        if (wordTail == string.Empty)
        {
            currentNode.EndWord = true;
            return(currentNode);
        }
        else
        {
            return(this.AddWord(wordTail, currentNode));
        }
    }
Пример #6
0
    /// <summary>
    /// Put a new key value pair, overwriting the existing value if the given key is already in use.
    /// </summary>
    /// <param name="key">Key to search for value by.</param>
    /// <param name="value">Value associated with key.</param>
    public void Put(string key, V value)
    {
        TrieNode <V> node = root;

        foreach (char c in key)
        {
            node = node.AddChild(c);
        }
        node.Value = value;
    }
    /** Inserts a word into the trie. */
    public void Insert(string word)
    {
        TrieNode curr = root;

        foreach (char c in word)
        {
            curr = curr.AddChild(c);
        }

        curr.FormsEndOfWord = true;
    }
                public void AddChild(string val, int index = 0)
                {
                    if (index >= val.Length)
                    {
                        IsWord = true;
                        return;
                    }
                    var newNode = new TrieNode(val[index], this.Index + 1);

                    newNode.AddChild(val, index + 1);
                    Children.Add(newNode);
                }
Пример #9
0
        /** Inserts a word into the trie. */
        public void Insert(string word)
        {
            TrieNode parentNode = root;

            for (int i = 0; i < word.Length; i++)
            {
                var ch = word[i];
                if (!parentNode.ContainKey(ch))
                {
                    parentNode.AddChild(ch);
                }
                parentNode = parentNode.GetChildNode(ch);
            }
            parentNode.HasEndHere = true;
        }