diff --git a/DataStructureAndAlgorithm/15 æ ‘.md b/DataStructureAndAlgorithm/15 æ ‘.md new file mode 100644 index 0000000..6c936b4 --- /dev/null +++ b/DataStructureAndAlgorithm/15 æ ‘.md @@ -0,0 +1,1161 @@ + +# 15 Ê÷ + +**֪ʶ½á¹¹£º** + + +![ͼ1 ֪ʶ½á¹¹](https://img-blog.csdnimg.cn/20201030133612918.png) + +--- +## 1. Ê÷µÄ»ù±¾¸ÅÄîÓëÊõÓï + +### 1.1 Ê÷µÄ¶¨Òå + +Ê÷ÊÇ$N(N \geq 0)$¸ö½áµã×é³ÉµÄÓÐÇºÏ £¬¸Ã¼¯ºÏ¾ßÓÐÈçÏÂÌØÕ÷£º + +£¨1£©³ý$N=0$µÄÊ÷Í⣬ÓÐÇÒ½öÓÐÒ»¸öÌØ¶¨µÄ³ÆÎª¸ùµÄ½áµã¡£ + +£¨2£©ÆäÓà½áµã·ÖΪ$m (m\geq 0)$¸ö»¥²»ÏཻµÄÓÐÇºÏ$T_1,T_2,\cdots,T_m$£¬ÆäÖÐÿһ¸ö¼¯ºÏ¶¼ÊÇÒ»¿ÃÊ÷£¬³ÆÎª¸Ã¸ùµÄ×ÓÊ÷¡£ + +Àý£º + +![ͼ2 Ê÷](https://img-blog.csdnimg.cn/20201027184843843.png) + +Ê÷µÄÌØµã£º + +£¨1£©ÓÐÒ»¸ö×ܸù¡£ + +£¨2£©Ã»ÓзÖÖ§Ïཻ¡£ + +£¨3£©Ê÷Óвã´Î¡£ + + + +### 1.2 Ê÷µÄÏà¹ØÊõÓï + +**½áµãµÄ¶È**£º¸Ã½áµã¾ßÓÐ×ÓÊ÷µÄÊýÄ¿¡£ + +**Ò¶×Ó½áµã**£º¶ÈΪÁãµÄ½áµã¡££¨Ã»ÓÐ×ÓÊ÷µÄ½áµã£© + +**Ê÷µÄ¶È**£ºÊ÷Öнáµã¶ÈµÄ×î´óÖµ¡£ + +**×ÓÅ®ÓëË«Ç×**£º×ÓŮָ¸Ã½áµã×ÓÊ÷µÄ¸ù½áµã£¬Ë«Ç×¼´¸Ã½áµã¡£ + +**½áµãµÄ²ã´Î£¨Level£©**£º¸ùµ½¸Ã½áµãµÄ·¾¶³¤¶È¡££¨¸ùΪµÚÁã²ã¡¢Èô½áµã$X$ÔÚ$L$²ã£¬ÔòÆä×ÓÅ®ÔÚ$L+1$²ã£©¡£ + +**Ê÷µÄ¸ß¶È£¨Éî¶È£©**£ºÊ÷Öнáµã²ãµÄ×î´óÖµ¡£ + +**ÐÖµÜÓëÌÃÐÖµÜ**£ºÍ¬Ò»Ë«Ç׺¢×ÓÖ®¼ä³ÆÎªÐֵܣ¬ÆäË«Ç×ÔÚͬһ²ãµÄ½áµã»¥ÎªÌÃÐֵܡ£ + +**׿ÏÈÓë×ÓËï**£º´Ó¸ùµ½¸Ã½áµãËù¾­·ÖÖ§ÉϵÄËùÓнáµã³ÆÎª¸Ã½áµãµÄ׿ÏÈ£¬ÒԸýáµãΪ¸ùµÄ×ÓÊ÷ÖеÄËùÓнáµã³ÆÎª¸Ã½áµãµÄ×ÓËï¡£ + +**É­ÁÖ**£º$m (m \geq 0)$¿Ã»¥²»ÏཻµÄÊ÷µÄ¼¯ºÏ¡£ + + +--- +## 2. ¶þ²æÊ÷ + +### 2.1 ¶þ²æÊ÷µÄ¶¨Òå¼°ÆäÐÔÖÊ + +£¨1£©¶þ²æÊ÷µÄ¶¨Òå + +¶þ²æÊ÷ÊǽáµãµÄÓÐÇºÏ£¬ÇÒ±ØÐëÂú×ãÏÂÃæµÄÌõ¼þÖ®Ò»£º + +- ËüÊǿռ¯¡£ +- ËüÓÉÒ»¸ö¸ù½áµãºÍÆä×óÓÒ×ÓÊ÷¹¹³ÉÇÒÆä×óÓÒ×ÓÊ÷Âú×ã¶þ²æÊ÷µÄ¶¨Òå¡£ + +£¨2£©¶þ²æÊ÷µÄ»ù±¾ÐÎ̬ + + +![ͼ3 ¶þ²æÊ÷µÄ»ù±¾ÐÎ̬](https://img-blog.csdnimg.cn/20201027190058238.png) + +£¨3£©¶þ²æÊ÷µÄÌØÕ÷ + +- ¶þ²æÊ÷µÄÿ¸ö½áµãµÄ¶È²»´óÓÚ2¡£ +- ¶þ²æÊ÷µÄ×ÓÊ÷ÓÐ×óÓÒÖ®·Ö¡£ + +£¨4£©¶þ²æÊ÷µÄÐÔÖÊ + +ÐÔÖÊ1£º¶þ²æÊ÷ÖвãÊýΪ$i(i\geq 0)$µÄ½áµãÖÁ¶àÓÐ$2^i$¸ö¡£ + + +ÐÔÖÊ2£º¸ß¶ÈΪ$k$µÄ¶þ²æÊ÷ÖÁ¶àÓÐ$2^{k+1}-1(k\geq 0)$¸ö½áµã¡£ + +ÈôÒ»¿Ã¸ß¶ÈΪ$k$µÄ¶þ²æÊ÷£¬¾ßÓÐ$2^{k+1}-1$¸ö½áµã£¬ÕâÑùµÄ¶þ²æÊ÷³ÆÎª**Âú¶þ²æÊ÷**¡£ + +ÈôÒ»¿Ã¾ßÓÐ$n$¸ö½áµã£¬¸ß¶ÈΪ$k$µÄ¶þ²æÊ÷£¬ÆäÖÐËùÓнáµãÓë¸ß¶ÈΪ$k$µÄÂú¶þ²æÊ÷ÖбàºÅÓÉ1ÖÁ$n$µÄÄÇЩ½áµã¶ÔÓ¦£¬ÕâÑùµÄ¶þ²æÊ÷³ÆÎª**ÍêÈ«¶þ²æÊ÷**¡£ + +![ͼ4 ÍêÈ«¶þ²æÊ÷](https://img-blog.csdnimg.cn/2020102719093017.png) + +×¢£ºÂú¶þ²æÊ÷Ò»¶¨ÊÇÍêÈ«¶þ²æÊ÷£¬ÍêÈ«¶þ²æÊ÷²»Ò»¶¨ÊÇÂú¶þ²æÊ÷¡£ + +ÐÔÖÊ3£ºÉè¶þ²æÊ÷ÖÐÒ¶×Ó½áµãµÄ¸öÊýΪ$n_0$£¬¶ÈΪ2½áµãµÄ¸öÊýΪ$n_2$£¬Ôò$n_0=n_2+1$¡£ + +ÐÔÖÊ4£º½«Ò»¿Ã¾ßÓÐ$n$¸ö½áµãµÄÍêÈ«¶þ²æÊ÷°´²ã´Î˳Ðò£¨´ÓÉϵ½Ï¡¢´Ó×óµ½ÓÒ£©´Ó1¿ªÊ¼±àºÅ£¬Ôò¶Ô±àºÅΪ$i(1\leq i\leq n)$µÄ½áµãÓУº + +- Èô$i$²»µÈÓÚ1£¬Ôò±àºÅΪ$i$½áµãµÄË«Ç×½áµãµÄ±àºÅΪ$[\frac{i}{2}]$£» +- Èô$2i\leq n$£¬Ôò±àºÅΪ$i$½áµãµÄ×óº¢×ӵıàºÅΪ$2i$£¬·ñÔò$i$ÎÞ×óº¢×Ó£» +- Èô$2i+1 \leq n$£¬Ôò±àºÅΪ$i$½áµãµÄÓÒº¢×ӵıàºÅΪ$2i+1$£¬·ñÔò$i$ÎÞÓÒº¢×Ó£» + +ÐÔÖÊ5£º¾ßÓÐ$n$¸ö½áµãµÄÍêÈ«¶þ²æÊ÷µÄ¸ß¶ÈΪ$[log_2^n]$¡£ + + + +### 2.2 ¶þ²æÊ÷µÄ´æ´¢ + +£¨1£©Ë³Ðò´æ´¢ + +ÍêÈ«¶þ²æÊ÷£º + +![ͼ5 ÍêÈ«¶þ²æÊ÷µÄ´æ´¢](https://img-blog.csdnimg.cn/20201027192502347.png) + + +·ÇÍêÈ«¶þ²æÊ÷£º + +![ͼ6 ·ÇÍêÈ«¶þ²æÊ÷µÄ´æ´¢](https://img-blog.csdnimg.cn/20201027192532754.png) + +£¨2£©Á´Ê½´æ´¢ + +![ͼ7 ¶þ²æÊ÷µÄÁ´Ê½´æ´¢](https://img-blog.csdnimg.cn/20201027192607546.png) + +ͨ³££ºÍêÈ«¶þ²æÊ÷ÓÃ˳Ðò´æ´¢£¬ÆÕͨ¶þ²æÊ÷ÓÃÁ´Ê½´æ´¢¡£ + +¶þ²æÊ÷½áµãµÄ½á¹¹£º + +![ͼ8 ¶þ²æÊ÷½áµãµÄ½á¹¹](https://img-blog.csdnimg.cn/20201027192734281.png) + +![ͼ9 ¶þ²æÊ÷½áµãÀàͼ](https://img-blog.csdnimg.cn/20201027193138134.png) + +```c +using System; +namespace NonLinearStruct +{ + /// + /// ¶þ²æÊ÷½áµã + /// + /// ½áµãÖÐÊý¾ÝÔªËØµÄÀàÐÍ + public class BinTreeNode : IComparable> where T : IComparable + { + private T _data; + + /// + /// »ñÈ¡»òÉèÖøýáµãµÄ×óº¢×Ó + /// + public BinTreeNode LeftChild { get; set; } + + /// + /// »ñÈ¡»òÉèÖøýáµãµÄÓÒº¢×Ó + /// + public BinTreeNode RightChild { get; set; } + + /// + /// »ñÈ¡»òÉèÖøýáµãµÄÊý¾ÝÔªËØ + /// + public T Data + { + get { return _data; } + set + { + if (value == null) + throw new ArgumentNullException(); + _data = value; + } + } + + /// + /// ³õʼ»¯BinTreeNodeÀàµÄÐÂʵÀý + /// + /// ¸Ã½áµãµÄ×óº¢×Ó + /// ¸Ã½áµãµÄÊý¾ÝÔªËØ + /// ¸Ã½áµãµÄÓÒº¢×Ó + public BinTreeNode(T data, BinTreeNode lChild = null, BinTreeNode rChild = null) + { + if (data == null) + throw new ArgumentNullException(); + LeftChild = lChild; + _data = data; + RightChild = rChild; + } + + /// + /// ʵÏÖ½Ó¿ÚIComparable>Öеķ½·¨¡£ + /// + /// + /// + public int CompareTo(BinTreeNode other) + { + if (other == null) + throw new ArgumentNullException(); + return _data.CompareTo(other.Data); + } + } +} +``` + + +### 2.3 ¶þ²æÊ÷µÄ±éÀú + +¶¨Ò壺°´ÕÕÒ»¶¨´ÎÐò·ÃÎÊÊ÷ÖÐËùÓнáµã£¬²¢ÇÒÿ¸ö½áµãµÄÖµ½ö±»·ÃÎÊÒ»´ÎµÄ¹ý³Ì¡£ + +±éÀúµÄ·½Ê½£º +- ǰÐò±éÀú£¨Ïȸù±éÀú£©£º·ÃÎʸù½áµã£¬Ç°Ðò±éÀú×ó×ÓÊ÷£¬Ç°Ðò±éÀúÓÒ×ÓÊ÷£» +- ÖÐÐò±éÀú£¨Öиù±éÀú£©£ºÖÐÐò±éÀú×ó×ÓÊ÷£¬·ÃÎʸù½áµã£¬ÖÐÐò±éÀúÓÒ×ÓÊ÷£» +- ºóÐò±éÀú£¨ºó¸ù±éÀú£©£ººóÐò±éÀú×ó×ÓÊ÷£¬ºóÐò±éÀúÓÒ×ÓÊ÷£¬·ÃÎʸù½áµã +- ²ã´Î±éÀú£º±éÀúµÚ0²ã£¬±éÀúµÚ1²ã£¬$\cdots$£¬±éÀúµÚ$k$²ã£¨$k$ΪÊ÷µÄÉî¶È£©£» + +Àý×Ó£º + +![ͼ10 ¶þ²æÊ÷µÄ±éÀú](https://img-blog.csdnimg.cn/20201028073031395.png) + +Àý×Ó£ºÖÐÐò±éÀúÓëµÝ¹é + + +![ͼ11 ÖÐÐò±éÀúÓëµÝ¹é](https://img-blog.csdnimg.cn/20201028073123468.png) + +### 2.4 ¶þ²æÊ÷µÄʵÏÖ + +¶þ²æÊ÷µÄ²Ù×÷ +- »ñÈ¡»òÉèÖöþ²æÊ÷µÄ¸ù½áµã +- Ïò¶þ²æÊ÷ÖвåÈë½áµã +- »ñȡǰÐò±éÀúÐòÁÐ +- »ñÈ¡ÖÐÐò±éÀúÐòÁÐ +- »ñÈ¡ºóÐò±éÀúÐòÁÐ +- »ñÈ¡²ã´Î±éÀúÐòÁÐ +- »ñÈ¡¸ø¶¨½áµãµÄË«Ç×½áµã +- »ñÈ¡¸ø¶¨½áµãµÄ×óÐֵܽáµã +- »ñÈ¡¸ø¶¨½áµãµÄÓÒÐֵܽáµã +- ɾ³ýÒÔ¸ø¶¨½áµãΪ¸ùµÄ×ÓÊ÷ +- ¸ù¾Ý¸ø¶¨Êý¾Ý²éÕÒÆäÔÚ¶þ²æÊ÷ÖеĽáµã +- »ñȡҶ×Ó½áµãµÄ¸öÊý +- ½»»»¶þ²æÊ÷µÄ×óÓÒ×ÓÊ÷ + +![ͼ12 ¶þ²æÊ÷½á¹¹µÄ·â×°](https://img-blog.csdnimg.cn/20201028073508804.png) + + +```c +using System; +using LinearStruct; +namespace NonLinearStruct +{ + /// + /// ¶þ²æÊ÷µÄ³éÏóÊý¾ÝÀàÐÍʵÏÖ + /// + /// ¶þ²æÊ÷ÖнáµãÊý¾ÝµÄÀàÐÍ + public class BinTree where T : IComparable + { + private string _orderString = string.Empty; + + /// + /// »ñÈ¡»òÉèÖöþ²æÊ÷µÄ¸ù½áµã + /// + public BinTreeNode Root { get; set; } + + /// + /// ³õʼ»¯BinTreeÀàµÄÐÂʵÀý + /// + /// ¶þ²æÊ÷µÄ¸ù½áµã + public BinTree(BinTreeNode root) + { + Root = root; + } + + /// + /// ÏòÊ÷ÖвåÈë½áµã + /// + /// Òª²åÈëµÄ½áµã + /// ¸Ã½áµãµÄ×óº¢×Ó½áµã + /// ¸Ã½áµãµÄÓÒº¢×Ó½áµã + public void Insert(BinTreeNode current, BinTreeNode lChild, BinTreeNode rChild) + { + if (Root == null) + throw new Exception("Ê÷Ϊ¿Õ."); + if (current == null) + throw new ArgumentNullException(); + + current.LeftChild = lChild; + current.RightChild = rChild; + } + + /// + /// ǰÐò±éÀúµÄµÝ¹éº¯Êý + /// + /// ¿ªÊ¼µÝ¹éµÄ¸ù½áµã + private void PreOrder(BinTreeNode current) + { + if (current == null) + return; + _orderString += current.Data + " "; + PreOrder(current.LeftChild); + PreOrder(current.RightChild); + } + + /// + /// µÃµ½Ç°Ðò±éÀúÐòÁÐ + /// + /// ǰÐò±éÀúÐòÁÐ + public string PreOrderTraversal() + { + _orderString = string.Empty; + PreOrder(Root); + return _orderString.Trim(); + } + + /// + /// ÖÐÐò±éÀúµÄµÝ¹éº¯Êý + /// + /// ¿ªÊ¼µÝ¹éµÄ¸ù½áµã + private void MidOrder(BinTreeNode current) + { + if (current == null) + return; + MidOrder(current.LeftChild); + _orderString += current.Data + " "; + MidOrder(current.RightChild); + } + + /// + /// µÃµ½ÖÐÐò±éÀúÐòÁÐ + /// + /// ÖÐÐò±éÀúÐòÁÐ + public string MidOrderTraversal() + { + _orderString = string.Empty; + MidOrder(Root); + return _orderString.Trim(); + } + + /// + /// ºóÐò±éÀúµÄµÝ¹éº¯Êý + /// + /// ¿ªÊ¼µÝ¹éµÄ¸ù½áµã + private void PostOrder(BinTreeNode current) + { + if (current == null) + return; + PostOrder(current.LeftChild); + PostOrder(current.RightChild); + _orderString += current.Data + " "; + } + + /// + /// µÃµ½ºóÐò±éÀúÐòÁÐ + /// + /// ºóÐò±éÀúÐòÁÐ + public string PostOrderTraversal() + { + _orderString = string.Empty; + PostOrder(Root); + return _orderString.Trim(); + } + + /// + /// µÃµ½²ã´Î±éÀúÐòÁÐ + /// + /// ²ã´Î±éÀúÐòÁÐ + public string LevelTraversal() + { + _orderString = string.Empty; + if (Root != null) + { + LinkQueue> lq = new LinkQueue>(); + lq.EnQueue(Root); + while (lq.IsEmpty() == false) + { + BinTreeNode temp = lq.QueueFront; + lq.DeQueue(); + + _orderString += temp.Data + " "; + if (temp.LeftChild != null) + lq.EnQueue(temp.LeftChild); + if (temp.RightChild != null) + lq.EnQueue(temp.RightChild); + } + + } + return _orderString.Trim(); + } + + /// + /// ´Ócurrent½áµã¿ªÊ¼Ñ°ÕÒfind½áµãµÄ¸¸Ç×½áµãµÄµÝ¹éº¯Êý + /// + /// ¿ªÊ¼µÝ¹éµÄ¸ù½áµã + /// ҪѰÕҵĽáµã + /// find½áµãµÄ¸¸Ç×½áµã + private BinTreeNode FindParent(BinTreeNode current, BinTreeNode find) + { + if (find == null) + throw new ArgumentNullException(); + if (current == null) + return null; + + if (current.LeftChild != null && current.LeftChild.Equals(find)) + return current; + + if (current.RightChild != null && current.RightChild.Equals(find)) + return current; + + BinTreeNode temp = FindParent(current.LeftChild, find); + + if (temp != null) + return temp; + return FindParent(current.RightChild, find); + } + + /// + /// µÃµ½find½áµãµÄ¸¸Ç×½áµã,Èô²»´æÔÚ·µ»Ønull. + /// + /// ҪѰÕҵĽáµã + /// ¸Ã½áµãµÄ¸¸Ç×½áµã + public BinTreeNode GetParent(BinTreeNode find) + { + if (find == null) + throw new ArgumentNullException(); + return FindParent(Root, find); + } + + /// + /// µÃµ½µ±Ç°½áµãµÄ×óÐֵܽáµã,Èô²»´æÔÚ·µ»Ønull. + /// + /// µ±Ç°½áµã + /// µ±Ç°½áµãµÄ×óÐֵܽáµã + public BinTreeNode GetLeftSibling(BinTreeNode current) + { + if (current == null) + throw new ArgumentNullException(); + BinTreeNode parent = GetParent(current); + if (parent != null && parent.LeftChild != null && parent.LeftChild.Equals(current) == false) + return parent.LeftChild; + return null; + } + + /// + /// µÃµ½µ±Ç°½áµãµÄÓÒÐֵܽáµã,Èô²»´æÔÚ·µ»Ønull. + /// + /// µ±Ç°½áµã + /// µ±Ç°½áµãµÄÓÒÐֵܽáµã + public BinTreeNode GetRightSibling(BinTreeNode current) + { + if (current == null) + throw new ArgumentNullException(); + BinTreeNode parent = GetParent(current); + if (parent != null && parent.RightChild != null && parent.RightChild.Equals(current) == false) + return parent.RightChild; + return null; + } + + /// + /// ɾ³ýÒÔcurrentΪ¸ùµÄ×ÓÊ÷ + /// + /// Ҫɾ³ý×ÓÊ÷µÄ¸ù + public void DeleteSubTree(BinTreeNode current) + { + if (current == null) + throw new ArgumentNullException(); + if (Root == null) + throw new Exception("¶þ²æÊ÷Ϊnull."); + + if (Root.Equals(current)) + { + Root = null; + } + else + { + BinTreeNode parent = GetParent(current); + if (parent != null && parent.LeftChild != null && parent.LeftChild.Equals(current)) + parent.LeftChild = null; + if (parent != null && parent.RightChild != null && parent.RightChild.Equals(current)) + parent.RightChild = null; + } + } + + /// + /// ѰÕÒ½áµãµÄµÝ¹éº¯Êý + /// + /// ¿ªÊ¼½áµã + /// Êý¾Ý + /// Êý¾ÝËùÔڵĽáµã + private BinTreeNode FindData(BinTreeNode current, T data) + { + if (data == null) + throw new ArgumentNullException(); + + if (current == null) + return null; + + if (current.Data.CompareTo(data) == 0) + return current; + + BinTreeNode temp = FindData(current.LeftChild, data); + if (temp != null) + return temp; + return FindData(current.RightChild, data); + } + + /// + /// ¸ù¾ÝÊý¾ÝµÄÖµÕÒµ½¶þ²æÊ÷ÖжÔÓ¦µÄ½áµã,Èô²»´æÔÚ·µ»Ønull. + /// + /// Êý¾ÝµÄÖµ + /// ¶þ²æÊ÷ÖжÔÓ¦µÄ½áµã + public BinTreeNode Search(T data) + { + if (data == null) + throw new ArgumentNullException(); + + return FindData(Root, data); + } + + /// + /// ͳ¼ÆÒ¶×Ó½áµã¸öÊýµÄµÝ¹éº¯Êý + /// + /// ͳ¼ÆÒ¶×Ó½áµã×ÓÊ÷µÄ¸ù + /// Ò¶×Ó½áµãµÄ¸öÊý + private void FindLeafCount(BinTreeNode current, ref int count) + { + if (current == null) + return; + if (current.LeftChild == null && current.RightChild == null) + { + count++; + return; + } + FindLeafCount(current.LeftChild, ref count); + FindLeafCount(current.RightChild, ref count); + } + + /// + /// µÃµ½¸ÃÊ÷Ò¶×Ó½áµãµÄ¸öÊý + /// + /// Ò¶×Ó½áµãµÄ¸öÊý + public int GetLeafCount() + { + int count = 0; + FindLeafCount(Root, ref count); + return count; + } + + /// + /// ½»»»×óÓÒ×ÓÊ÷µÄµÝ¹éº¯Êý + /// + /// ×ÓÊ÷µÄ¸ù½áµã + private void Exchange(BinTreeNode current) + { + if (current == null) + return; + if (current.LeftChild != null || current.RightChild != null) + { + BinTreeNode temp = current.LeftChild; + current.LeftChild = current.RightChild; + current.RightChild = temp; + } + + if (current.LeftChild != null) + Exchange(current.LeftChild); + + if (current.RightChild != null) + Exchange(current.RightChild); + } + + /// + /// ½»»»¶þ²æÊ÷µÄ×óÓÒ×ÓÊ÷ + /// + public void Exchange() + { + Exchange(Root); + } + } +} +``` + +Àý×Ó£º + +```c +class Program +{ + static void Main(string[] args) + { + BinTreeNode a = new BinTreeNode("A"); + BinTreeNode b = new BinTreeNode("B"); + BinTreeNode c = new BinTreeNode("C"); + BinTreeNode d = new BinTreeNode("D"); + BinTree bintree = new BinTree(a); + bintree.Insert(a, b, c); + bintree.Insert(b, d, null); + + Console.WriteLine("ǰÐò±éÀú:{0}", bintree.PreOrderTraversal()); + // ǰÐò±éÀú:A B D C + Console.WriteLine("ÖÐÐò±éÀú:{0}", bintree.MidOrderTraversal()); + // ÖÐÐò±éÀú:D B A C + Console.WriteLine("ºóÐò±éÀú:{0}", bintree.PostOrderTraversal()); + // ºóÐò±éÀú:D B C A + Console.WriteLine("²ã´Î±éÀú:{0}", bintree.LevelTraversal()); + // ²ã´Î±éÀú:A B C D + + BinTreeNode parent = bintree.GetParent(d); + BinTreeNode lSibling = bintree.GetLeftSibling(c); + BinTreeNode rSibling = bintree.GetRightSibling(b); + if (parent != null) + Console.WriteLine("½áµã{0}µÄParent½áµã:{1}", d.Data, parent.Data); + else + Console.WriteLine("½áµã{0}ÎÞParent.", d.Data); + // ½áµãDµÄParent½áµã:B + + if (lSibling != null) + Console.WriteLine("½áµã{0}µÄ×óÐֵܽáµã:{1}", c.Data, lSibling.Data); + else + Console.WriteLine("½áµã{0}ÎÞ×óÐֵܽáµã.", c.Data); + // ½áµãCµÄ×óÐֵܽáµã:B + + if (lSibling != null) + Console.WriteLine("½áµã{0}µÄÓÒÐֵܽáµã:{1}", b.Data, rSibling.Data); + else + Console.WriteLine("½áµã{0}ÎÞÓÒÐֵܽáµã.", b.Data); + // ½áµãBµÄÓÒÐֵܽáµã:C + + bintree.DeleteSubTree(d); + Console.WriteLine("°Ñ½áµã{0}´Ó¶þ²æÊ÷ÖÐÒÆ³ý.", d.Data); + // °Ñ½áµãD´Ó¶þ²æÊ÷ÖÐÒÆ³ý. + Console.WriteLine("ǰÐò±éÀú:{0}", bintree.PreOrderTraversal()); + // ǰÐò±éÀú: A B C + Console.WriteLine("ÖÐÐò±éÀú:{0}", bintree.MidOrderTraversal()); + // ÖÐÐò±éÀú:B A C + Console.WriteLine("ºóÐò±éÀú:{0}", bintree.PostOrderTraversal()); + // ºóÐò±éÀú: B C A + Console.WriteLine("²ã´Î±éÀú:{0}", bintree.LevelTraversal()); + // ²ã´Î±éÀú: A B C + + BinTreeNode e = bintree.Search("A"); + if (e != null && e.LeftChild != null) + Console.WriteLine("ѰÕÒ½áµã{0}µÄ×óº¢×ÓΪ{1}", e.Data, e.LeftChild.Data); + else + Console.WriteLine("δÕÒµ½{0}½áµã»òÕß{0}½áµã×óº¢×ÓΪnull", a.Data); + // ѰÕÒ½áµãAµÄ×óº¢×ÓΪB + + Console.WriteLine("Ò¶×Ó½áµãµÄ¸öÊý:{0}", bintree.GetLeafCount()); + // Ò¶×Ó½áµãµÄ¸öÊý:2 + + bintree.Exchange(); + Console.WriteLine("ǰÐò±éÀú:{0}", bintree.PreOrderTraversal()); + // ǰÐò±éÀú:A C B + Console.WriteLine("ÖÐÐò±éÀú:{0}", bintree.MidOrderTraversal()); + // ÖÐÐò±éÀú: C A B + Console.WriteLine("ºóÐò±éÀú:{0}", bintree.PostOrderTraversal()); + // ºóÐò±éÀú: C B A + Console.WriteLine("²ã´Î±éÀú:{0}", bintree.LevelTraversal()); + // ²ã´Î±éÀú: A C B + } +} +``` + + +--- +## 3. Ê÷ÓëÉ­ÁÖ + +### 3.1 Ê÷µÄ´æ´¢ + +£¨1£©Ë³Ðò´æ´¢ + +![ͼ13 Ê÷µÄ˳Ðò´æ´¢](https://img-blog.csdnimg.cn/20201028170504458.png) + +£¨2£©Á´Ê½´æ´¢ + +A. ´æ´¢Ë«Ç×½áµã + +![ͼ14 Ê÷µÄÁ´Ê½´æ´¢](https://img-blog.csdnimg.cn/20201028170708611.png) + +B. ´æ´¢º¢×Ó½áµã + +![ͼ15 Ê÷µÄÁ´Ê½´æ´¢](https://img-blog.csdnimg.cn/20201028171018892.png) + + +C. ´æ´¢º¢×ÓºÍÐֵܽáµã + +![ͼ16 Ê÷µÄÁ´Ê½´æ´¢](https://img-blog.csdnimg.cn/20201028171137619.png) + + + + + +### 3.2 Ê÷Óë¶þ²æÊ÷µÄÏ໥ת»» + +£¨1£©Ê÷ת»»³É¶þ²æÊ÷ + +°´ÕÕ×óº¢×Ó£¬ÓÒÐֵܹæÔò¡£ + +![ͼ17 Ê÷ת»¯³É¶þ²æÊ÷](https://img-blog.csdnimg.cn/20201028171452143.png) + + +£¨2£©¶þ²æÊ÷ת»»³ÉÊ÷ + +°´ÕÕ×óº¢×Ó£¬ÓÒÐֵܹæÔòµÄÄæ¹ý³Ì¡£ + +![ͼ18 ¶þ²æÊ÷ת»¯³ÉÊ÷](https://img-blog.csdnimg.cn/20201028171531337.png) + + + + +### 3.3 É­ÁÖÓë¶þ²æÊ÷µÄÏ໥ת»» + +£¨1£©É­ÁÖת»¯³É¶þ²æÊ÷ + +Éè$F=(T_1,T_2,\cdots,T_n)$ΪÊ÷$T_1,T_2,\cdots,T_n$×é³ÉµÄÉ­ÁÖ£¬$B(F)$ΪÆä¶ÔÓ¦µÄ¶þ²æÊ÷¡£ +- Èô$n=0$£¬Ôò$B(F)=\phi$¡£ +- Èô$n>0$£¬`$B(F)$µÄ¸ùΪ$Root(T_1)$£¬$B(F)$µÄ×ó×ÓÊ÷ÓÉ$T_1$µÄÖî×ÓÊ÷×é³É£¬ÓÒ×ÓÊ÷ÓÉ$B(\lbrace T_2,\cdots,T_n\rbrace)$×é³É¡£ + + +·½Ê½1£º + +![ͼ19 É­ÁÖת»¯³É¶þ²æÊ÷£¨1£©](https://img-blog.csdnimg.cn/20201028173938828.png) + +·½Ê½2£º + +![ͼ20 É­ÁÖת»¯³É¶þ²æÊ÷£¨2£©](https://img-blog.csdnimg.cn/20201028174016119.png) + + +£¨2£©¶þ²æÊ÷ת»¯³ÉÉ­ÁÖ + +Éè$B=(Root,LB,RB)$ÊÇÒ»¿Å¶þ²æÊ÷£¬$F(B)=\lbrace T_1,T_2,\cdots,T_n \rbrace$ΪÆä¶ÔÓ¦µÄÉ­ÁÖ¡£ +- Èô$B=\phi$£¬Ôò$F(B)=\phi$ $(n=0)$¡£ +- Èô$B$²»µÈÓÚ$\phi$£¬Ôò$T_1$µÄ¸ùΪ¶þ²æÊ÷$B$µÄ¸ù$Root$£¬$T_1$µÄ×ÓÊ÷É­ÁÖÓÉ$B$µÄ×ó×ÓÊ÷$LB$ת»»¶ø³É£¬ÆäÓàÊ÷×é³ÉµÄÉ­ÁÖ$F(T)=\lbrace T_2,\cdots,T_n \rbrace$ÓÉ$B$µÄÓÒ×ÓÊ÷$RB$ת»»¶ø³É¡£ + +·½Ê½1£º + +![ͼ21 ¶þ²æÊ÷ת»¯³ÉÉ­ÁÖ£¨1£©](https://img-blog.csdnimg.cn/20201028173757637.png) + +·½Ê½2£º + +![ͼ22 ¶þ²æÊ÷ת»¯³ÉÉ­ÁÖ£¨2£©](https://img-blog.csdnimg.cn/20201028173839965.png) + + +### 3.4 Ê÷ÓëÉ­ÁֵıéÀú + +£¨1£©Ê÷µÄ±éÀú + +![ͼ23 Ê÷](https://img-blog.csdnimg.cn/20201028174830506.png) + +ǰÐò±éÀú£ºÊ×ÏÈ·ÃÎʸù½áµã£»È»ºóǰÐò±éÀúÿ¿Ã×ÓÊ÷¡£ +- ǰÐò±éÀúÐòÁУºA B E C F H G D + +ºóÐò±éÀú£ºÊ×ÏȺóÐò±éÀúÿ¿Ã×ÓÊ÷£»È»ºó·ÃÎʸù½áµã¡£ +- ºóÐò±éÀúÐòÁУºE B H F G C D A + + +£¨2£©É­ÁֵıéÀú + +![ͼ24 É­ÁÖ](https://img-blog.csdnimg.cn/20201028175043630.png) + +ǰÐò±éÀú£ºÊ×ÏÈ·ÃÎʵÚÒ»¿ÃÊ÷µÄ¸ù½áµã£»È»ºóǰÐò±éÀúµÚÒ»¿ÃÊ÷µÄ×ÓÊ÷£»ÔÙǰÐò±éÀúÓÉÆäÓàÊ÷×é³ÉµÄÉ­ÁÖ¡£ +- ǰÐò±éÀúÐòÁУºA B C D E F G H I J + +ºóÐò±éÀú£ºÏȺóÐò±éÀúµÚÒ»¿ÃÊ÷µÄ×ÓÊ÷£»È»ºó·ÃÎʵÚÒ»¿ÃÊ÷µÄ¸ù½áµã£»ÔÙºóÐò±éÀúÓÉÆäÓàÊ÷×é³ÉµÄÉ­ÁÖ¡£ +- ºóÐò±éÀúÐòÁУºB C D A F E H J I G + + +--- +## 4. ÏßË÷¶þ²æÊ÷ + +### 4.1 ÒýÈë + +ÐÔÖÊ£ºÉè¶þ²æÊ÷ÀûÓöþ²æÁ´±í´æ´¢ÇÒ½áµã¸öÊýΪ$n$£¬Ôò¸Ã¶þ²æÊ÷µÄÁ´Óò¸öÊýΪ$2n$£¬¿ÕÁ´Óò¸öÊýΪ$n+1$¡£ + + +Ïë·¨£ºÀûÓÿÕÁ´Óò´æ´¢¸ÃÊ÷°´ÕÕijÖÖ·½Ê½±éÀú£¨Ç°Ðò¡¢ÖÐÐò¡¢ºóÐò£©µÄǰÇ÷»òºó¼Ì½áµã¡£ + + +½á¹¹£º + +![ͼ25 ÏßË÷¶þ²æÊ÷½áµã½á¹¹](https://img-blog.csdnimg.cn/20201028200739710.png) + + +### 4.2 Ïà¹Ø¸ÅÄî + +**ÏßË÷**£ºÔÚÕâÖֽṹÖÐÖ¸ÏòÆäǰÇ÷»òºó¼Ì½áµãµÄÖ¸Õë¡£ + +**ÏßË÷¶þ²æÊ÷**£º¼ÓÉÏÏßË÷µÄ¶þ²æÊ÷¡£ + +**ÏßË÷»¯**£º°Ñ¶þ²æÊ÷±ä³ÉÏßË÷¶þ²æÊ÷µÄ¹ý³Ì¡£ + +**ÏßË÷»¯µÄ·½Ê½**£ºÇ°ÐòÏßË÷»¯¡¢ÖÐÐòÏßË÷»¯¡¢ºóÐòÏßË÷»¯ + +Àý×Ó£ºÖÐÐòÏßË÷»¯ + +![ͼ26 ÖÐÐòÏßË÷»¯](https://img-blog.csdnimg.cn/20201028200125188.png) + +Àý×Ó£ºÇ°ÐòÏßË÷»¯ + +![ͼ27 ǰÐòÏßË÷»¯](https://img-blog.csdnimg.cn/20201028200201217.png) + + +--- +## 5. ¹þ·òÂüÊ÷¼°Æä±àÂ루Huffman£© + +### 5.1 ¹þ·òÂüÊ÷µÄ¶¨Òå + +**·¾¶**£ºÁ¬½ÓÁ½¸ö½áµãµÄ·ÖÖ§£¬¹¹³ÉÕâÁ½¸ö½áµãµÄ·¾¶¡£ + +**·¾¶³¤¶È**£ºÁ½¸ö½áµã·¾¶ÉϵķÖÖ§Êý¡£ + +**½áµãµÄȨ**£ºÎª½áµã¸³µÄÒ»¸öÓÐÒâÒåµÄʵÊý¡£ + +**½áµãµÄ´øÈ¨Â·¾¶³¤¶È**£º¸ù½áµãµ½¸Ã½áµãµÄ·¾¶³¤¶ÈÓë¸Ã½áµãȨֵµÄ³Ë»ý¡£ + +**Ê÷µÄ´øÈ¨Â·¾¶³¤¶È**£ºÊ÷ÖÐËùÓÐÒ¶×Ó½áµãµÄ´øÈ¨Â·¾¶³¤¶ÈÖ®ºÍ¡£¼ÇΪ£º$WPL=\sum_{i=1}^{n}\omega_iL_i$£¬$n$ΪҶ×Ó½áµãµÄ¸öÊý¡£ + +**¹þ·òÂüÊ÷**£º$n$¸ö´øÈ¨Ò¶×Ó½áµã¹¹³ÉµÄËùÓжþ²æÊ÷ÖУ¬´øÈ¨Â·¾¶³¤¶È$WPL$×îСµÄ¶þ²æÊ÷¡££¨×îÓŶþ²æÊ÷£© + + +### 5.2 ¹þ·òÂüÊ÷µÄ¹¹Ôì + +µÚ1²½£º¸ù¾Ý¸ø¶¨µÄ$n$¸öȨֵ$\omega_1,\omega_2,\cdots,\omega_n$£¬¹¹³É$n$¿Ã¶þ²æÊ÷µÄÉ­ÁÖ$F=\lbrace T_1,T_2,\cdots,T_n \rbrace$£¬ÆäÖÐÿ¿Ã¶þ²æÊ÷$T_i$Öж¼Ö»ÓÐÒ»¸öȨֵ`$\omega_i$µÄ¸ù½áµã£¬Æä×óÓÒ×ÓÊ÷¾ùΪ¿Õ¡£ + +µÚ2²½£ºÔÚÉ­ÁÖ$F$ÖÐÑ¡³öÁ½¿Ã¸ù½áµãȨֵ×îСµÄÊ÷×÷Ϊһ¿ÃÐÂÊ÷µÄ×óÓÒ×ÓÊ÷£¬ÇÒÖÃÐÂÊ÷µÄ¸ù½áµãµÄȨֵΪÆä×óÓÒ×ÓÊ÷Éϸù½áµãµÄȨֵ֮ºÍ¡£ + +µÚ3²½£º´Ó$F$ÖÐɾ³ý¹¹³ÉÐÂÊ÷µÄÄÄÁ½¿ÃÊ÷£¬Í¬Ê±°ÑÐÂÊ÷¼ÓÈë$F$ÖС£ + +µÚ4²½£ºÖظ´µÚ2ºÍ3²½£¬Ö±µ½$F$ÖÐÖ»º¬ÓÐÒ»¿ÃÊ÷Ϊֹ£¬´ËÊ÷±ãÊÇHuffmanÊ÷¡£ + +Àý×Ó£º¸ø¶¨È¨¼¯$\omega=\lbrace 2,3,4,7,8,9 \rbrace$ £¬ÊÔ¹¹Ôì¹ØÓÚ$\omega$µÄÒ»¿Å¹þ·òÂüÊ÷£¬²¢ÇóÆä´øÈ¨Â·¾¶³¤¶È ¡£ + +![ͼ28 HuffmanÊ÷µÄ¹¹Ôì](https://img-blog.csdnimg.cn/20201028201850962.png) + +### 5.3 ¹þ·òÂü±àÂëµÄ¸ÅÄî + +**ǰ׺Âë¹æÔò**£º¶Ô×Ö·û¼¯±àÂëʱ£¬×Ö·û¼¯ÖÐÈÎÒ»×Ö·ûµÄ±àÂë¶¼²»ÊÇÆäËü×Ö·û±àÂëµÄǰ׺¡£ + +Àý×Ó£º + +`A£º0 B£º00 C£º01 D£º10`£¨Î¥·´Ç°×ºÂë¹æÔò£© + +000010 BBD¡¢AAAAD¡¢BAAD¡¢AABD £¨²»Î¨Ò»£¬½âÂëʱÓжþÒåÐÔ£© + +`A£º00 B£º01 C£º10 D£º11`£¨Âú×ãǰ׺Âë¹æÔò£© + +000010 AAC £¨Î¨Ò»£© + + +**¹þ·òÂü±àÂë**£º½«¹þ·òÂüÊ÷ÖУ¬Ã¿¸ö·ÖÖ§½áµãµÄ×ó·ÖÖ§±ê0£¬ÓÒ·ÖÖ§±ê1£¬°Ñ´Ó¸ù½áµãµ½Ã¿¸öÒ¶×Ó½áµãµÄ·¾¶ÉϵıêºÅÁ¬½ÓÆðÀ´×÷Ϊ¸ÃÒ¶×Ó½áµãËù´ú±í·ûºÅµÄ±àÂ룬ÕâÑùµÃµ½µÄ±àÂë³ÆÎªHuffman±àÂë¡££¨Óŵ㣺Âú×ãǰ׺Âë¹æÔò£© + + +Àý×Ó£º¶ÔËùÒª·¢Ë͵ÄÊý¾Ý½øÐжþ½øÖƱàÂë¡£ + +state£¬seat£¬act£¬tea£¬cat£¬set£¬a£¬eat + +µÚÒ»ÖÖ±àÂ뷽ʽ£º + +` 's'£º"000¡± 't'£º¡°001¡± 'a'£º¡°010¡± 'e'£º¡°011¡± 'c'£º¡°100¡± ','£º¡°110¡± ` + +´ËÖÖ±àÂëÂú×ãǰ׺Âë¹æÔò£¬µ«±àÂë¹ý³¤¡£eat£º011010001 + +µÚ¶þÖÖ±àÂ뷽ʽ£º + +ͳ¼Æ×Ö·û³öÏֵįµÊý£º¡®s¡¯£º3 ¡®t¡¯£º8 ¡®a¡¯£º7 ¡®e¡¯£º5 ¡®c¡¯£º2 ¡®,¡¯£º7 + +![ͼ29 Huffman±àÂë](https://img-blog.csdnimg.cn/20201028202334543.png) + + +` +'a'£º"00¡± ','£º¡°01¡± 't'£º¡°10¡± 'e'£º¡°110¡± 'c'£º¡°1110¡± 's'£º¡°1111¡± +` + +- eat£º1100010 +- state£º1111100010110 +- seat£º11111100010 + + + + +### 5.4 ¹þ·òÂü±àÂëµÄʵÏÖ + +![ͼ30 HuffmanÊ÷µÄ½áµã](https://img-blog.csdnimg.cn/20201028202821127.png) + +```c +namespace NonLinearStruct +{ + /// + /// ±íʾHuffmanÊ÷µÄ½áµã + /// + public class HuffmanTreeNode : BinTreeNode + { + /// + /// ½áµãµÄȨֵ + /// + public int Weight { get; set; } + + /// + /// Ò¶×Ó½áµã -- ×Ö·û + /// + /// ×Ö·û + /// ½áµãµÄȨ + public HuffmanTreeNode(char data, int weight) : base(data) + { + Weight = weight; + } + + /// + /// ÆäËü½áµã + /// + /// ½áµãµÄȨ + public HuffmanTreeNode(int weight) : base('\0') + { + Weight = weight; + } + } +} +``` + +![ͼ31 Huffman×Öµä](https://img-blog.csdnimg.cn/20201028203147321.png) + +```c +namespace NonLinearStruct +{ + /// + /// Huffman×Öµä + /// + public class HuffmanDicItem + { + /// + /// »ñÈ¡»òÉèÖÃ×Ö·û + /// + public char Character { get; set; } + + /// + /// »ñÈ¡»òÉèÖñàÂë + /// + public string Code { get; set; } + + /// + /// ¹¹Ô캯Êý + /// + /// ×Ö·û + /// ±àÂë + public HuffmanDicItem(char charactor, string code) + { + Character = charactor; + Code = code; + } + } +} +``` + +![ͼ32 HuffmanÊ÷](https://img-blog.csdnimg.cn/20201028204632221.png) + +```c +using System; +using System.Collections.Generic; +using System.Linq; +namespace NonLinearStruct +{ + /// + /// HuffmanÊ÷ + /// + public class HuffmanTree + { + /// + /// ¹¹ÔìHuffmanÊ÷³õʼµÄÉ­ÁÖ + /// + /// ×Ö·û´® + /// ½áµãµÄ¼¯ºÏ£¬¹¹³É³õʼµÄÉ­ÁÖ + private List CreateInitForest(string str) + { + if (string.IsNullOrEmpty(str)) + throw new ArgumentNullException(); + + List result = new List(); + char[] charArray = str.ToCharArray(); + List> lst = charArray.GroupBy(a => a).ToList(); + + foreach (IGrouping g in lst) + { + char data = g.Key; + int weight = g.ToList().Count; + HuffmanTreeNode node = new HuffmanTreeNode(data, weight); + result.Add(node); + } + return result; + } + + /// + /// ¹¹ÔìHuffmanÊ÷ + /// + /// ³õʼµÄ½áµãµÄ¼¯ºÏ + /// HuffmanÊ÷µÄRoot + private HuffmanTreeNode CreateHuffmanTree(List sources) + { + if (sources == null) + throw new ArgumentNullException(); + if (sources.Count < 2) + throw new ArgumentException("¹¹ÔìHuffmanÊ÷£¬×îÉÙΪ2¸ö½áµã¡£"); + + HuffmanTreeNode root = default(HuffmanTreeNode); + bool isNext = true; + + while (isNext) + { + List lst = sources.OrderBy(a => a.Weight).ToList(); + HuffmanTreeNode n1 = lst[0]; + HuffmanTreeNode n2 = lst[1]; + int weight = n1.Weight + n2.Weight; + + HuffmanTreeNode node = new HuffmanTreeNode(weight); + node.LeftChild = n1; + node.RightChild = n2; + + if (lst.Count == 2) + { + root = node; + isNext = false; + } + else + { + sources = lst.GetRange(2, lst.Count - 2); + sources.Add(node); + } + } + return root; + } + + /// + /// ´´½¨Huffman±àÂëµÄ×Öµä + /// + /// + /// + /// + private List CreateHuffmanDict(string code, HuffmanTreeNode current) + { + if (current == null) + throw new ArgumentNullException(); + + List result = new List(); + if (current.LeftChild == null && current.RightChild == null) + { + result.Add(new HuffmanDicItem(current.Data, code)); + } + else + { + List dictL = CreateHuffmanDict(code + "0", + (HuffmanTreeNode)current.LeftChild); + List dictR = CreateHuffmanDict(code + "1", + (HuffmanTreeNode)current.RightChild); + + result.AddRange(dictL); + result.AddRange(dictR); + } + return result; + } + + /// + /// ´´½¨Huffman±àÂëµÄ×Öµä + /// + /// HuffmanÊ÷ + /// Huffman×Öµä + private List CreateHuffmanDict(HuffmanTreeNode root) + { + if (root == null) + throw new ArgumentNullException(); + + return CreateHuffmanDict(string.Empty, root); + } + + /// + /// ¶Ô×Ö·û´®½øÐбàÂë + /// + /// Huffman×Öµä + /// ±àÂë×Ö·û´® + /// ±àÂëºóµÄ×Ö·û´® + public string StringToHuffmanCode(out List dict, string str) + { + List forest = CreateInitForest(str); + HuffmanTreeNode root = CreateHuffmanTree(forest); + dict = CreateHuffmanDict(root); + string result = ToHuffmanCode(str, dict); + return result; + } + + /// + /// ¸ø¶¨Huffman×Öµä¶Ô±àÂë½øÐнâÂë + /// + /// Huffman×Öµä + /// ½âÂëµÄ×Ö·û + /// ½âÂëºóµÄ×Ö·û + public string HuffmanCodeToString(List dict, string code) + { + string result = string.Empty; + for (int i = 0; i < code.Length;) + { + foreach (HuffmanDicItem item in dict) + { + if (code[i] == item.Code[0] && item.Code.Length + i <= code.Length) + { + string temp = code.Substring(i, item.Code.Length); + if (temp == item.Code) + { + result += item.Character; + i += item.Code.Length; + break; + } + } + } + } + return result; + } + + /// + /// ÀûÓÃHuffman×Öµä¶Ô×Ö·û¼¯½øÐбàÂë + /// + /// ±àÂëµÄ×Ö·û + /// Huffman×Öµä + /// ±àÂëºóµÄ×Ö·û´® + private string ToHuffmanCode(string source, List lst) + { + if (string.IsNullOrEmpty(source)) + throw new ArgumentNullException(); + if (lst == null) + throw new ArgumentNullException(); + + string result = string.Empty; + for (int i = 0; i < source.Length; i++) + { + result += lst.Single(a => a.Character == source[i]).Code; + } + return result; + } + } +} +``` + +¿Í»§¶Ë´úÂ룺 + +```c +class Program +{ + static void Main(string[] args) + { + string str = "state,seat,act,tea,cat,set,a,eat"; + Console.WriteLine(str); + // state,seat,act,tea,cat,set,a,eat + + HuffmanTree huffmanTree = new HuffmanTree(); + List dic; + string huffmanCode = huffmanTree.StringToHuffmanCode(out dic, str); + string temp = string.Empty; + for (int i = 0; i < dic.Count; i++) + { + temp += @"'" + dic[i].Character + "':" + "\"" + dic[i].Code + "\"\r\n"; + } + Console.WriteLine(temp.Substring(0, temp.Length - 2)); + // 'a':"00" + // ',':"01" + // 't':"10" + // 'e':"110" + // 'c':"1110" + // 's':"1111" + Console.WriteLine(huffmanCode); + // 1111100010110011111110001001001110100110110000111100010011111110100100011100010 + + string decode = huffmanTree.HuffmanCodeToString(dic, huffmanCode); + Console.WriteLine(decode); + //state,seat,act,tea,cat,set,a,eat + } +} +``` + +