Пример #1
0
        public TrieTree()
        {
            this.rootNode = new BranchNode(NodeType.Root, null, null);

            wordDic = new List <string>();

            string       Dic        = System.Configuration.ConfigurationManager.AppSettings["Dic"];
            StreamReader fileReader = new StreamReader(Dic, Encoding.GetEncoding("gb2312"));

            while (!fileReader.EndOfStream)
            {
                string tempLine      = fileReader.ReadLine();
                int    FirstTabIndex = tempLine.IndexOf('\t');
                string wordInFile    = tempLine.Substring(0, FirstTabIndex);
                wordInFile = wordInFile.Trim().ToLower();
                if (!wordDic.Contains(wordInFile))
                {
                    wordDic.Add(wordInFile);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// 根据深度为当前分支节点添加子节点
        /// </summary>
        /// <param name="node">当前的分支节点</param>
        /// <param name="searchDepth">搜索深度,最开始是0</param>
        /// <param name="wordList">当前的所搜集合</param>
        private void BuildBranch(Node node, List <string> wordList, int searchDepth)
        {
            char searchChar = 'a';

            while (searchChar <= 'z')
            {
                List <string> nowList = GetGroup(wordList, searchDepth, searchChar);
                if (nowList.Count == 1)
                {
                    Node nowLeaf = new LeafNode(nowList[0], node);
                    if (node.GetFirstChildNode() != null)
                    {
                        Node tempNode = node.GetFirstChildNode();
                        while (tempNode.GetBrotherNode() != null)
                        {
                            tempNode = tempNode.GetBrotherNode();
                        }
                        tempNode.SetBrother(nowLeaf);
                    }
                    else
                    {
                        node.SetChild(nowLeaf);
                    }
                }
                else if (nowList.Count > 1)
                {
                    List <string> nextList = new List <string>();
                    foreach (string word in nowList)
                    {
                        char[] wordArray = word.ToCharArray();
                        if (searchDepth == (wordArray.Length - 1))
                        {
                            Node nowLeaf = new LeafNode(word, node);
                            if (node.GetFirstChildNode() != null)
                            {
                                Node tempNode = node.GetFirstChildNode();
                                while (tempNode.GetBrotherNode() != null)
                                {
                                    tempNode = tempNode.GetBrotherNode();
                                }
                                tempNode.SetBrother(nowLeaf);
                            }
                            else
                            {
                                node.SetChild(nowLeaf);
                            }
                        }
                        else
                        {
                            nextList.Add(word);
                        }
                    }
                    if (nextList.Count > 1)
                    {
                        Node nowBranch = new BranchNode(NodeType.Branch, node, searchChar.ToString());
                        if (node.GetFirstChildNode() != null)
                        {
                            Node tempNode = node.GetFirstChildNode();
                            while (tempNode.GetBrotherNode() != null)
                            {
                                tempNode = tempNode.GetBrotherNode();
                            }
                            tempNode.SetBrother(nowBranch);
                        }
                        else
                        {
                            node.SetChild(nowBranch);
                        }
                        BuildBranch(nowBranch, nextList, searchDepth + 1);
                    }
                    else if (nextList.Count == 1)
                    {
                        Node nowLeaf = new LeafNode(nextList[0], node);
                        if (node.GetFirstChildNode() != null)
                        {
                            Node tempNode = node.GetFirstChildNode();
                            while (tempNode.GetBrotherNode() != null)
                            {
                                tempNode = tempNode.GetBrotherNode();
                            }
                            tempNode.SetBrother(nowLeaf);
                        }
                        else
                        {
                            node.SetChild(nowLeaf);
                        }
                    }
                }

                searchChar++;
            }
        }