Ejemplo n.º 1
0
            /// <summary>
            /// Adds a new value at this node.
            /// </summary>
            /// <param name="idx"></param>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <returns></returns>
            internal void Add(short idx, string key, T value)
            {
                // TODO: improve on this by inserting the mean value first.
                // TODO: improve on this by remove recursion and going to iteration.
                char s = key[idx];

                if (s < _split_char)
                { // s belongs at the low side.
                    // recursively add to the low side.
                    if (_lower_node == null)
                    { // there is no lower node; create one.
                        _lower_node = new StringTreeNode(key[idx]);
                        _lower_node.Add(idx, key, value);
                    }
                    else
                    { // add to the lower node.
                        _lower_node.Add(idx, key, value);
                    }
                }
                else if (s == _split_char)
                {         // s belongs at the equal node.
                    if (idx < key.Length - 1)
                    {     // there are still other chars left!
                        if (_equal_node == null)
                        { // there is no equal node; create one.
                            _equal_node = new StringTreeNode(key[idx + 1]);
                        }
                        // add to the equals node.
                        _equal_node.Add((short)(idx + 1), key, value);
                    }
                    else
                    {                   // there are no other char's left! assign the value here!
                        _value = value; // no recursion anymore, it ends here!
                    }
                }
                else
                { // s belongs at the high side.
                    // recursively add to the high side.
                    if (_higher_node == null)
                    { // there is no higher node; create one.
                        _higher_node = new StringTreeNode(key[idx]);
                        _higher_node.Add(idx, key, value);
                    }
                    else
                    { // add to the lower node.
                        _higher_node.Add(idx, key, value);
                    }
                }
            }
Ejemplo n.º 2
0
        public void AddShouldCreateAChildNodeForTheFirstLetterIfItDoesNotExist()
        {
            node.Add("a");

            Assert.IsType <StringTreeNode>(node.Find("a"));
        }