示例#1
0
        /// <summary>
        /// Tries to add a new list to the tree.
        /// </summary>
        /// <param name="list">list of numbers that will be added to prefix tree</param>
        /// <param name="omittedIndex">element of the list at this index is treated as non-existent</param>
        /// <param name="storeOmittedAtLeaf">omitted element is stored in the leaf node of the tree</param>
        /// <returns>true if a given element was not already in the tree</returns>
        public bool TryAdd(List <int> list, int omittedIndex, bool storeOmittedAtLeaf)
        {
            if (root == null)
            {
                root = new PrefixTreeNode(width);
            }
            PrefixTreeNode currentNode = root;
            var            listEnum    = list.GetEnumerator();

            for (int n = 0; n < list.Count; ++n)
            {
                listEnum.MoveNext();
                if (n == omittedIndex)
                {
                    continue;
                }
                int i = listEnum.Current;
                if (currentNode.Get(i) == null)
                {
                    currentNode.Set(i, new PrefixTreeNode(width));
                }
                currentNode = currentNode.Get(i);
            }

            if (storeOmittedAtLeaf)
            {
                currentNode.StoreValue(list[omittedIndex]);
            }

            return(currentNode.SetEnd());
        }
示例#2
0
        public PrefixTreeNode GetNode(List <int> list, int omittedIndex)
        {
            if (root == null)
            {
                return(null);
            }
            PrefixTreeNode currentNode = root;
            var            listEnum    = list.GetEnumerator();

            for (int n = 0; n < list.Count; ++n)
            {
                listEnum.MoveNext();
                if (n == omittedIndex)
                {
                    continue;
                }
                int i = listEnum.Current;
                if (currentNode.Get(i) == null)
                {
                    return(null);
                }
                currentNode = currentNode.Get(i);
            }
            return(currentNode);
        }