Ejemplo n.º 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());
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Sets a reference of sub-node at specified index to a new one.
 /// </summary>
 /// <param name="n"></param>
 /// <param name="node"></param>
 public void Set(int n, PrefixTreeNode node)
 {
     nodes[n] = node;
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Constructs a new prefix tree with a specified maximal width.
 /// </summary>
 /// <param name="width"></param>
 public PrefixTree(int width /*, int depth*/)
 {
     this.width = width;
     //this.depth = depth;
     root = null;
 }