C#實現TrieTree演算法

來源:互聯網
上載者:User
   public class TrieTree    {        TrieNode _root = null;        private TrieTree()        {            _root = new TrieNode(char.MaxValue,0);            charCount = 0;        }        static TrieTree _instance = null;        public static TrieTree GetInstance()        {            if (_instance == null)            {                _instance = new TrieTree();            }            return _instance;        }        public TrieNode Root         {            get { return _root; }        }        public void AddWord(char ch)        {            TrieNode newnode=_root.AddChild(ch);            newnode.IncreaseFrequency();            newnode.WordEnded = true;        }        int charCount;        public void AddWord(string word)        {            if (word.Length == 1)            {                AddWord(word[0]);                charCount++;            }            else            {                 char[] chars=word.ToCharArray();                TrieNode node = _root;                charCount += chars.Length;                for (int i = 0; i < chars.Length; i++)                {                    TrieNode newnode=node.AddChild(chars[i]);                    newnode.IncreaseFrequency();                    node = newnode;                }                node.WordEnded = true;            }        }        public int GetFrequency(char ch)        {            TrieNode matchedNode = _root.Children.FirstOrDefault(n => n.Character == ch);            if (matchedNode == null)            {                return 0;            }            return matchedNode.Frequency;                     }        public int GetFrequency(string word)        {            if (word.Length == 1)            {                return GetFrequency(word[0]);             }            else            {                char[] chars = word.ToCharArray();                TrieNode node = _root;                for (int i = 0; i < chars.Length; i++)                {                    if (node.Children == null)                        return 0;                    TrieNode matchednode = node.Children.FirstOrDefault(n => n.Character == chars[i]);                    if (matchednode == null)                    {                        return 0;                    }                    node = matchednode;                }                if (node.WordEnded == true)                    return node.Frequency;                else                    return -1;            }        }    }

這裡我們使用了單例模式,因為TrieTree類似緩衝,不需要重複建立。下面是TreeNode的實現:

    public class TrieNode    {        public TrieNode(char ch,int depth)        {            this.Character=ch;            this._depth=depth;        }        public char Character;        int _depth;        public int Depth        {            get{return _depth;}        }        TrieNode _parent=null;        public TrieNode Parent         {            get { return _parent; }            set { _parent = value; }        }        public bool WordEnded = false;        HashSet<TrieNode> _children=null;        public HashSet<TrieNode> Children        {            get { return _children; }        }        public TrieNode GetChildNode(char ch)        {            if (_children != null)                return _children.FirstOrDefault(n => n.Character == ch);            else                return null;        }        public TrieNode AddChild(char ch)        {            TrieNode matchedNode=null;            if (_children != null)            {                matchedNode = _children.FirstOrDefault(n => n.Character == ch);            }            if (matchedNode != null)    //found the char in the list            {                //matchedNode.IncreaseFrequency();                return matchedNode;            }            else            {   //not found                TrieNode node = new TrieNode(ch, this.Depth + 1);                node.Parent = this;                //node.IncreaseFrequency();                if (_children == null)                    _children = new HashSet<TrieNode>();                _children.Add(node);                return node;            }        }        int _frequency = 0;        public int Frequency        {            get { return _frequency; }        }        public void IncreaseFrequency()        {            _frequency++;        }        public string GetWord()        {             TrieNode tmp=this;            string result = string.Empty;            while(tmp.Parent!=null) //until root node            {                result = tmp.Character + result;                tmp = tmp.Parent;            }            return result;        }        public override string ToString()        {            return Convert.ToString(this.Character);        }    }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.