Beispiel #1
0
 /// <summary>
 /// 用于创建非根节点的构造函数
 /// </summary>
 /// <param name="root">Trie 的根</param>
 public BasicTrieNode(BasicTrieNode <TChar> root, TChar charOfNode)
 {
     this.root  = root;
     Children   = new BasicTrieChildren <TChar>(this);
     Fail       = root;
     CharOfNode = charOfNode;
 }
 /// <summary>
 /// 先序遍历以当前节点为根的子树
 /// </summary>
 /// <param name="u">当前节点</param>
 /// <param name="action">要执行的操作</param>
 private void PreOrderTraverseDfs(BasicTrieNode <TChar> u,
                                  Action <BasicTrieNode <TChar> > action)
 {
     action?.Invoke(u);
     foreach (var kv in u.Children)
     {
         PreOrderTraverseDfs(kv.Value, action);
     }
 }
        /// <summary>
        /// 拓扑排序的 DFS 函数
        /// </summary>
        private void TopoDfs(BasicTrieNode <TChar> u)
        {
            u.TopoVis = -1;
            var v = u.Fail; // fail 出边指向的点

            if (!v.IsRoot)
            {
                Debug.Assert(v.TopoVis != -1);
                if (v.TopoVis == 0)
                {
                    TopoDfs(v);
                }
            }
            u.TopoVis = 1;
            topo.Add(u);
        }
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="node">所属的节点</param>
 public BasicTrieChildren(BasicTrieNode <TChar> node) => this.node = node;