예제 #1
0
        public static Dictionary<TrieNode, int> getDescendant(TrieNode n,
            Dictionary<TrieNode, int> descendents, int depth, int k)
        {
            Queue<pair> queue = new Queue<pair>();
            queue.Enqueue(new pair(n, k));
            if (k > depth)
                return descendents;
            if (!descendents.ContainsKey(n))
                descendents.Add(n, k);
            else
            {
                int kk = descendents[n];
                if (k > kk) descendents[n] = kk;
            }
            while (queue.Count > 0)
            {
                // get the first TrieNode of the queue
                pair p = queue.Dequeue();
                // add children to the queue
                if (p.depth < depth)
                {
                    foreach (TrieNode c in p.n.SubTrieNodes)
                    {
                        if (descendents.ContainsKey(c))
                        {
                            int v = descendents[c];
                            int vv = (int)Math.Min(p.depth + 1, v);

                            if (vv <= depth)
                            {
                                descendents[c] = vv;
                                queue.Enqueue(new pair(c, vv));
                            }
                        }
                        else
                        {
                            descendents.Add(c, p.depth + 1);
                        }
                    }
                }
            }
            return descendents;
        }
예제 #2
0
 public static Dictionary<TrieNode, int> getDescendant(TrieNode n, int depth, int k)
 {
     Dictionary<TrieNode, int> descendents = new Dictionary<TrieNode, int>();
     getDescendant(n, descendents, depth, k);
     return descendents;
 }
예제 #3
0
 public pair(TrieNode n, int depth)
 {
     this.n = n;
     this.depth = depth;
 }
예제 #4
0
 public static void BuildActiveNodes(TrieNode n, int depth)
 {
     TrieNode r=n.parent;
     mybuildActiveTrieNodes(r, n, depth);
 }
예제 #5
0
        //for inner TrieNodes
        private static void mybuildActiveTrieNodes(TrieNode parent, TrieNode TrieNode, int depth)
        {
            Dictionary<TrieNode, int> parentActiveTrieNodes = ht[parent];
            Dictionary<TrieNode, int> activeTrieNodes = ht[TrieNode];
            // deletion
            // add all p active TrieNode to this, with distance +1 if possible
            foreach (TrieNode n in parentActiveTrieNodes.Keys)
            {
                if (n == parent && depth > 0)
                    ht[TrieNode].Add(n, 1);
                else
                {
                    int l = parentActiveTrieNodes[n] + 1;
                    if (l <= depth)
                        ht[TrieNode].Add(n, l);
                }
            }

            foreach (TrieNode p in parentActiveTrieNodes.Keys)
            {
                // if p.c=c // we have a match
                int d = parentActiveTrieNodes[p];

                if (p.c == TrieNode.c)
                {
                    getDescendant(TrieNode, activeTrieNodes, depth, d);
                }

                foreach (TrieNode c in p.SubTrieNodes)
                {
                    if (c == TrieNode)
                        continue;
                    // insertion
                    if (c.c == TrieNode.c)
                    {// we have a match
                        getDescendant(c, activeTrieNodes, depth, d);
                    }
                    else
                        if (d <= depth)
                        {
                            if (activeTrieNodes.ContainsKey(c))
                            {
                                int m = Math.Min(d + 1, activeTrieNodes[c]);
                                if (m <= depth)
                                    activeTrieNodes[c] = m;
                            }
                            else
                            {
                                activeTrieNodes.Add(c, d + 1);
                            }
                        }
                }
            }
            // add myself & my Descendant
            if (activeTrieNodes.ContainsKey(TrieNode))
            {
                activeTrieNodes[TrieNode] = 0;
            }
            else
                activeTrieNodes.Add(TrieNode, 0);
            getDescendant(TrieNode, activeTrieNodes, depth, 0);

            // System.out.println("Active TrieNode " + id + ":" + Text() + ":"
            // + activeTrieNodes);
        }