經典算法題每日演練——第十三題 赫夫曼樹


       赫夫曼樹又稱最優二叉樹,也就是帶權路徑最短的樹,對於赫夫曼樹,我想大家對它是非常的熟悉,也知道它的應用場景,

但是有沒有自己親手寫過,這個我就不清楚了,不管以前寫沒寫,這一篇我們來玩一把。

 

一:概念

 赫夫曼樹里面有幾個概念,也是非常簡單的,先來看下面的圖:

1. 基礎概念

<1>  節點的權: 節點中紅色部分就是權,在實際應用中,我們用“字符”出現的次數作為權。

<2>  路徑長度:可以理解成該節點到根節點的層數,比如:“A”到根節點的路徑長度為3。

<3>  樹的路徑長度:各個葉子節點到根節點的路徑長度總和,用WPL標記。

最后我們要討論的的赫夫曼樹也就是帶權路徑長度最小的一棵樹。

2.構建

   由於要使WPL最短,赫夫曼樹的構建采用自低向上的方式,這里我們采用小根堆來存放當前需要構建的各個節點,我們的方

式是每次從小根堆中取出最小的兩個節點,合並后放入堆中,然后繼續取兩個最小的節點,一直到小根堆為空,最后我們采用

自底向上構建的赫夫曼樹也就完畢了。

 

 

好了,赫夫曼樹的典型應用就是在數據壓縮方面,下面我們就要在赫夫曼樹上面放入赫夫曼編碼了,我們知道普通的ASCII碼是

采用等長編碼的,即每個字符都采用2個字節,而赫夫曼編碼的思想就是采用不等長的思路,權重高的字符靠近根節點,權重低

的字符遠離根節點,標記方式為左孩子“0”,右孩子“1”,如下圖。

 

 

從圖中我們可以看到各個字符的赫夫曼編碼了,獲取字符的編碼采用從根往下的方式收集路徑上的‘0,1',如:

A:110。

B:111。

C:0。

D:10。

最后我們來比較他們的WPL的長度:  ASCII碼=10*2+20*2+40*2+80*2=300

                                               赫夫曼碼=10*3+20*3+40*2+80*1=250

可以看到,赫夫曼碼壓縮了50個0,1字符,太牛逼了,是不是啊。。。

三:代碼

1. 樹節點

    我們采用7元節點,其中parent方便我們在DFS的時候找到從葉子節點到根節點的路徑上的赫夫曼編碼。

 1 #region 赫夫曼節點
 2         /// <summary>
 3         /// 赫夫曼節點
 4         /// </summary>
 5         public class Node
 6         {
 7             /// <summary>
 8             /// 左孩子
 9             /// </summary>
10             public Node left;
11 
12             /// <summary>
13             /// 右孩子
14             /// </summary>
15             public Node right;
16 
17             /// <summary>
18             /// 父節點
19             /// </summary>
20             public Node parent;
21 
22             /// <summary>
23             /// 節點字符
24             /// </summary>
25             public char c;
26 
27             /// <summary>
28             /// 節點權重
29             /// </summary>
30             public int weight;
31 
32             //赫夫曼“0"or“1"
33             public char huffmancode;
34 
35             /// <summary>
36             /// 標記是否為葉子節點
37             /// </summary>
38             public bool isLeaf;
39         }
40         #endregion

 

1. 構建赫夫曼樹(Build)

   上面也說了,構建赫夫曼編碼樹我們采用小根堆的形式構建,構建完后,我們采用DFS的方式統計各個字符的編碼,復雜度為N*logN。

關於小根堆(詳細內容可以參考我的系列文章 "優先隊列")

 1 #region 構建赫夫曼樹
 2         /// <summary>
 3         /// 構建赫夫曼樹
 4         /// </summary>
 5         public void Build()
 6         {
 7             //構建
 8             while (queue.Count() > 0)
 9             {
10                 //如果只有一個節點,則說明已經到根節點了
11                 if (queue.Count() == 1)
12                 {
13                     root = queue.Dequeue().t;
14 
15                     break;
16                 }
17 
18                 //節點1
19                 var node1 = queue.Dequeue();
20 
21                 //節點2
22                 var node2 = queue.Dequeue();
23 
24                 //標記左孩子
25                 node1.t.huffmancode = '0';
26 
27                 //標記為右孩子
28                 node2.t.huffmancode = '1';
29 
30                 //判斷當前節點是否為葉子節點,hufuman無度為1點節點(方便計算huffman編碼)
31                 if (node1.t.left == null)
32                     node1.t.isLeaf = true;
33 
34                 if (node2.t.left == null)
35                     node2.t.isLeaf = true;
36 
37                 //父節點
38                 root = new Node();
39 
40                 root.left = node1.t;
41 
42                 root.right = node2.t;
43 
44                 root.weight = node1.t.weight + node2.t.weight;
45 
46                 //當前節點為根節點
47                 node1.t.parent = node2.t.parent = root;
48 
49                 //將當前節點的父節點入隊列
50                 queue.Eequeue(root, root.weight);
51             }
52 
53             //深度優先統計各個字符的編碼
54             DFS(root);
55         }
56         #endregion

 

 2:編碼(Encode,Decode)

  樹構建起來后,我會用字典來保存字符和”赫夫曼編碼“的對應表,然后拿着明文或者密文對着編碼表翻譯就行了, 復雜度O(N)。

 

 1         #region 赫夫曼編碼
 2         /// <summary>
 3         /// 赫夫曼編碼
 4         /// </summary>
 5         /// <returns></returns>
 6         public string Encode()
 7         {
 8             StringBuilder sb = new StringBuilder();
 9 
10             foreach (var item in word)
11             {
12                 sb.Append(huffmanEncode[item]);
13             }
14 
15             return sb.ToString();
16         }
17         #endregion
18 
19         #region 赫夫曼解碼
20         /// <summary>
21         /// 赫夫曼解碼
22         /// </summary>
23         /// <returns></returns>
24         public string Decode(string str)
25         {
26             StringBuilder decode = new StringBuilder();
27 
28             string temp = string.Empty;
29 
30             for (int i = 0; i < str.Length; i++)
31             {
32                 temp += str[i].ToString();
33 
34                 //如果包含 O(N)時間
35                 if (huffmanDecode.ContainsKey(temp))
36                 {
37                     decode.Append(huffmanDecode[temp]);
38 
39                     temp = string.Empty;
40                 }
41             }
42 
43             return decode.ToString();
44         }
45         #endregion

最后我們做個例子,壓縮9M的文件,看看到底能壓縮多少?

 1  public static void Main()
 2         {
 3             StringBuilder sb = new StringBuilder();
 4 
 5             for (int i = 0; i < 1 * 10000; i++)
 6             {
 7                 sb.Append("人民網北京12月8日電 (記者 宋心蕊) 北京時間8日晚的央視《新聞聯播》節目出現了直播失誤。上一條新聞尚未播放完畢時,播就將畫面切換回了演播間,主播李梓萌開始播報下一條新聞,導致兩條新聞出現了“混音”播出。央視新聞官方微博賬號在21點09分發布了一條致歉微博:【致歉】今晚《新聞聯播》因導播員口令失誤,導致畫面切換錯誤,特此向觀眾朋友表示歉意。央視特約評論員楊禹在個人微博中寫道:今晚《新聞聯播》出了個切換錯誤,@央視新聞 及時做了誠懇道歉。聯播一直奉行“金標准”,壓力源自全社會的高要求。其實報紙亦都有“勘誤”一欄,坦誠糾錯與道歉。《新聞聯播》是中國影響力最大的電視新聞節目。它有不可替代的符號感,它有失誤,更有悄然的進步。新的改進正在或即將發生,不妨期待");
 8             }
 9 
10             File.WriteAllText(Environment.CurrentDirectory + "//1.txt", sb.ToString());
11 
12             Huffman huffman = new Huffman(sb.ToString());
13 
14             Stopwatch watch = Stopwatch.StartNew();
15 
16             huffman.Build();
17 
18             watch.Stop();
19 
20             Console.WriteLine("構建赫夫曼樹耗費:{0}", watch.ElapsedMilliseconds);
21 
22             //將8位二進制轉化為ascII碼
23             var s = huffman.Encode();
24 
25             var remain = s.Length % 8;
26 
27             List<char> list = new List<char>();
28 
29             var start = 0;
30 
31             for (int i = 8; i < s.Length; i = i + 8)
32             {
33                 list.Add((char)Convert.ToInt32(s.Substring(i - 8, 8), 2));
34 
35                 start = i;
36             }
37 
38             var result = new String(list.ToArray());
39 
40             //當字符編碼不足8位時, 用‘艹'來標記,然后拿出’擦‘以后的所有0,1即可
41             result += "艹" + s.Substring(start);
42 
43             File.WriteAllText(Environment.CurrentDirectory + "//2.txt", result);
44 
45             Console.WriteLine("壓縮完畢!");
46 
47             Console.Read();
48 
49             //解碼
50             var str = File.ReadAllText(Environment.CurrentDirectory + "//2.txt");
51 
52             sb.Clear();
53 
54             for (int i = 0; i < str.Length; i++)
55             {
56                 int ua = (int)str[i];
57 
58                 //說明已經取完畢了  用'艹'來做標記
59                 if (ua == 33401)
60                     sb.Append(str.Substring(i));
61                 else
62                     sb.Append(Convert.ToString(ua, 2).PadLeft(8, '0'));
63             }
64 
65             var sss = huffman.Decode(sb.ToString());
66 
67             Console.Read();
68         }

 

看看,多帥氣,將9M的文件壓縮到了4M,同時我也打開了壓縮后的秘文,相信這些東西是什么,你懂我懂的。

主程序:

View Code
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Diagnostics;
  6 using System.Threading;
  7 using System.IO;
  8 
  9 namespace ConsoleApplication2
 10 {
 11     public class Program
 12     {
 13         public static void Main()
 14         {
 15             StringBuilder sb = new StringBuilder();
 16 
 17             for (int i = 0; i < 1 * 10000; i++)
 18             {
 19                 sb.Append("人民網北京12月8日電 (記者 宋心蕊) 北京時間8日晚的央視《新聞聯播》節目出現了直播失誤。上一條新聞尚未播放完畢時,播就將畫面切換回了演播間,主播李梓萌開始播報下一條新聞,導致兩條新聞出現了“混音”播出。央視新聞官方微博賬號在21點09分發布了一條致歉微博:【致歉】今晚《新聞聯播》因導播員口令失誤,導致畫面切換錯誤,特此向觀眾朋友表示歉意。央視特約評論員楊禹在個人微博中寫道:今晚《新聞聯播》出了個切換錯誤,@央視新聞 及時做了誠懇道歉。聯播一直奉行“金標准”,壓力源自全社會的高要求。其實報紙亦都有“勘誤”一欄,坦誠糾錯與道歉。《新聞聯播》是中國影響力最大的電視新聞節目。它有不可替代的符號感,它有失誤,更有悄然的進步。新的改進正在或即將發生,不妨期待");
 20             }
 21 
 22             File.WriteAllText(Environment.CurrentDirectory + "//1.txt", sb.ToString());
 23 
 24             Huffman huffman = new Huffman(sb.ToString());
 25 
 26             Stopwatch watch = Stopwatch.StartNew();
 27 
 28             huffman.Build();
 29 
 30             watch.Stop();
 31 
 32             Console.WriteLine("構建赫夫曼樹耗費:{0}", watch.ElapsedMilliseconds);
 33 
 34             //將8位二進制轉化為ascII碼
 35             var s = huffman.Encode();
 36 
 37             var remain = s.Length % 8;
 38 
 39             List<char> list = new List<char>();
 40 
 41             var start = 0;
 42 
 43             for (int i = 8; i < s.Length; i = i + 8)
 44             {
 45                 list.Add((char)Convert.ToInt32(s.Substring(i - 8, 8), 2));
 46 
 47                 start = i;
 48             }
 49 
 50             var result = new String(list.ToArray());
 51 
 52             //當字符編碼不足8位時, 用‘艹'來標記,然后拿出’擦‘以后的所有0,1即可
 53             result += "" + s.Substring(start);
 54 
 55             File.WriteAllText(Environment.CurrentDirectory + "//2.txt", result);
 56 
 57             Console.WriteLine("壓縮完畢!");
 58 
 59             Console.Read();
 60 
 61             //解碼
 62             var str = File.ReadAllText(Environment.CurrentDirectory + "//2.txt");
 63 
 64             sb.Clear();
 65 
 66             for (int i = 0; i < str.Length; i++)
 67             {
 68                 int ua = (int)str[i];
 69 
 70                 //說明已經取完畢了  用'艹'來做標記
 71                 if (ua == 33401)
 72                     sb.Append(str.Substring(i));
 73                 else
 74                     sb.Append(Convert.ToString(ua, 2).PadLeft(8, '0'));
 75             }
 76 
 77             var sss = huffman.Decode(sb.ToString());
 78 
 79             Console.Read();
 80         }
 81     }
 82 
 83     public class Huffman
 84     {
 85         #region 赫夫曼節點
 86         /// <summary>
 87         /// 赫夫曼節點
 88         /// </summary>
 89         public class Node
 90         {
 91             /// <summary>
 92             /// 左孩子
 93             /// </summary>
 94             public Node left;
 95 
 96             /// <summary>
 97             /// 右孩子
 98             /// </summary>
 99             public Node right;
100 
101             /// <summary>
102             /// 父節點
103             /// </summary>
104             public Node parent;
105 
106             /// <summary>
107             /// 節點字符
108             /// </summary>
109             public char c;
110 
111             /// <summary>
112             /// 節點權重
113             /// </summary>
114             public int weight;
115 
116             //赫夫曼“0"or“1"
117             public char huffmancode;
118 
119             /// <summary>
120             /// 標記是否為葉子節點
121             /// </summary>
122             public bool isLeaf;
123         }
124         #endregion
125 
126         PriorityQueue<Node> queue = new PriorityQueue<Node>();
127 
128         /// <summary>
129         /// 編碼對應表(加速用)
130         /// </summary>
131         Dictionary<char, string> huffmanEncode = new Dictionary<char, string>();
132 
133         /// <summary>
134         /// 解碼對應表(加速用)
135         /// </summary>
136         Dictionary<string, char> huffmanDecode = new Dictionary<string, char>();
137 
138         /// <summary>
139         /// 明文
140         /// </summary>
141         string word = string.Empty;
142 
143         public Node root = new Node();
144 
145         public Huffman(string str)
146         {
147             this.word = str;
148 
149             Dictionary<char, int> dic = new Dictionary<char, int>();
150 
151             foreach (var s in str)
152             {
153                 if (dic.ContainsKey(s))
154                     dic[s] += 1;
155                 else
156                     dic[s] = 1;
157             }
158 
159             foreach (var item in dic.Keys)
160             {
161                 var node = new Node()
162                 {
163                     c = item,
164                     weight = dic[item]
165                 };
166 
167                 //入隊
168                 queue.Eequeue(node, dic[item]);
169             }
170         }
171 
172         #region 構建赫夫曼樹
173         /// <summary>
174         /// 構建赫夫曼樹
175         /// </summary>
176         public void Build()
177         {
178             //構建
179             while (queue.Count() > 0)
180             {
181                 //如果只有一個節點,則說明已經到根節點了
182                 if (queue.Count() == 1)
183                 {
184                     root = queue.Dequeue().t;
185 
186                     break;
187                 }
188 
189                 //節點1
190                 var node1 = queue.Dequeue();
191 
192                 //節點2
193                 var node2 = queue.Dequeue();
194 
195                 //標記左孩子
196                 node1.t.huffmancode = '0';
197 
198                 //標記為右孩子
199                 node2.t.huffmancode = '1';
200 
201                 //判斷當前節點是否為葉子節點,hufuman無度為1點節點(方便計算huffman編碼)
202                 if (node1.t.left == null)
203                     node1.t.isLeaf = true;
204 
205                 if (node2.t.left == null)
206                     node2.t.isLeaf = true;
207 
208                 //父節點
209                 root = new Node();
210 
211                 root.left = node1.t;
212 
213                 root.right = node2.t;
214 
215                 root.weight = node1.t.weight + node2.t.weight;
216 
217                 //當前節點為根節點
218                 node1.t.parent = node2.t.parent = root;
219 
220                 //將當前節點的父節點入隊列
221                 queue.Eequeue(root, root.weight);
222             }
223 
224             //深度優先統計各個字符的編碼
225             DFS(root);
226         }
227         #endregion
228 
229         #region 赫夫曼編碼
230         /// <summary>
231         /// 赫夫曼編碼
232         /// </summary>
233         /// <returns></returns>
234         public string Encode()
235         {
236             StringBuilder sb = new StringBuilder();
237 
238             foreach (var item in word)
239             {
240                 sb.Append(huffmanEncode[item]);
241             }
242 
243             return sb.ToString();
244         }
245         #endregion
246 
247         #region 赫夫曼解碼
248         /// <summary>
249         /// 赫夫曼解碼
250         /// </summary>
251         /// <returns></returns>
252         public string Decode(string str)
253         {
254             StringBuilder decode = new StringBuilder();
255 
256             string temp = string.Empty;
257 
258             for (int i = 0; i < str.Length; i++)
259             {
260                 temp += str[i].ToString();
261 
262                 //如果包含 O(N)時間
263                 if (huffmanDecode.ContainsKey(temp))
264                 {
265                     decode.Append(huffmanDecode[temp]);
266 
267                     temp = string.Empty;
268                 }
269             }
270 
271             return decode.ToString();
272         }
273         #endregion
274 
275         #region 深度優先遍歷子節點,統計各個節點的赫夫曼編碼
276         /// <summary>
277         /// 深度優先遍歷子節點,統計各個節點的赫夫曼編碼
278         /// </summary>
279         /// <returns></returns>
280         public void DFS(Node node)
281         {
282             if (node == null)
283                 return;
284 
285             //遍歷左子樹
286             DFS(node.left);
287 
288             //遍歷右子樹
289             DFS(node.right);
290 
291             //如果當前葉節點
292             if (node.isLeaf)
293             {
294                 string code = string.Empty;
295 
296                 var temp = node;
297 
298                 //回溯的找父親節點的huffmancode LgN 的時間
299                 while (temp.parent != null)
300                 {
301                     //注意,這里最后形成的 “反過來的編碼”
302                     code += temp.huffmancode;
303 
304                     temp = temp.parent;
305                 }
306 
307                 var codetemp = new String(code.Reverse().ToArray());
308 
309                 huffmanEncode.Add(node.c, codetemp);
310 
311                 huffmanDecode.Add(codetemp, node.c);
312             }
313         }
314         #endregion
315     }
316 }

 

小根堆:

View Code
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Diagnostics;
  6 using System.Threading;
  7 using System.IO;
  8 
  9 namespace ConsoleApplication2
 10 {
 11     public class PriorityQueue<T> where T : class
 12     {
 13         /// <summary>
 14         /// 定義一個數組來存放節點
 15         /// </summary>
 16         private List<HeapNode> nodeList = new List<HeapNode>();
 17 
 18         #region 堆節點定義
 19         /// <summary>
 20         /// 堆節點定義
 21         /// </summary>
 22         public class HeapNode
 23         {
 24             /// <summary>
 25             /// 實體數據
 26             /// </summary>
 27             public T t { get; set; }
 28 
 29             /// <summary>
 30             /// 優先級別 1-10個級別 (優先級別遞增)
 31             /// </summary>
 32             public int level { get; set; }
 33 
 34             public HeapNode(T t, int level)
 35             {
 36                 this.t = t;
 37                 this.level = level;
 38             }
 39 
 40             public HeapNode() { }
 41         }
 42         #endregion
 43 
 44         #region  添加操作
 45         /// <summary>
 46         /// 添加操作
 47         /// </summary>
 48         public void Eequeue(T t, int level = 1)
 49         {
 50             //將當前節點追加到堆尾
 51             nodeList.Add(new HeapNode(t, level));
 52 
 53             //如果只有一個節點,則不需要進行篩操作
 54             if (nodeList.Count == 1)
 55                 return;
 56 
 57             //獲取最后一個非葉子節點
 58             int parent = nodeList.Count / 2 - 1;
 59 
 60             //堆調整
 61             UpHeapAdjust(nodeList, parent);
 62         }
 63         #endregion
 64 
 65         #region 對堆進行上濾操作,使得滿足堆性質
 66         /// <summary>
 67         /// 對堆進行上濾操作,使得滿足堆性質
 68         /// </summary>
 69         /// <param name="nodeList"></param>
 70         /// <param name="index">非葉子節點的之后指針(這里要注意:我們
 71         /// 的篩操作時針對非葉節點的)
 72         /// </param>
 73         public void UpHeapAdjust(List<HeapNode> nodeList, int parent)
 74         {
 75             while (parent >= 0)
 76             {
 77                 //當前index節點的左孩子
 78                 var left = 2 * parent + 1;
 79 
 80                 //當前index節點的右孩子
 81                 var right = left + 1;
 82 
 83                 //parent子節點中最大的孩子節點,方便於parent進行比較
 84                 //默認為left節點
 85                 var min = left;
 86 
 87                 //判斷當前節點是否有右孩子
 88                 if (right < nodeList.Count)
 89                 {
 90                     //判斷parent要比較的最大子節點
 91                     min = nodeList[left].level < nodeList[right].level ? left : right;
 92                 }
 93 
 94                 //如果parent節點大於它的某個子節點的話,此時篩操作
 95                 if (nodeList[parent].level > nodeList[min].level)
 96                 {
 97                     //子節點和父節點進行交換操作
 98                     var temp = nodeList[parent];
 99                     nodeList[parent] = nodeList[min];
100                     nodeList[min] = temp;
101 
102                     //繼續進行更上一層的過濾
103                     parent = (int)Math.Ceiling(parent / 2d) - 1;
104                 }
105                 else
106                 {
107                     break;
108                 }
109             }
110         }
111         #endregion
112 
113         #region 優先隊列的出隊操作
114         /// <summary>
115         /// 優先隊列的出隊操作
116         /// </summary>
117         /// <returns></returns>
118         public HeapNode Dequeue()
119         {
120             if (nodeList.Count == 0)
121                 return null;
122 
123             //出隊列操作,彈出數據頭元素
124             var pop = nodeList[0];
125 
126             //用尾元素填充頭元素
127             nodeList[0] = nodeList[nodeList.Count - 1];
128 
129             //刪除尾節點
130             nodeList.RemoveAt(nodeList.Count - 1);
131 
132             //然后從根節點下濾堆
133             DownHeapAdjust(nodeList, 0);
134 
135             return pop;
136         }
137         #endregion
138 
139         #region  對堆進行下濾操作,使得滿足堆性質
140         /// <summary>
141         /// 對堆進行下濾操作,使得滿足堆性質
142         /// </summary>
143         /// <param name="nodeList"></param>
144         /// <param name="index">非葉子節點的之后指針(這里要注意:我們
145         /// 的篩操作時針對非葉節點的)
146         /// </param>
147         public void DownHeapAdjust(List<HeapNode> nodeList, int parent)
148         {
149             while (2 * parent + 1 < nodeList.Count)
150             {
151                 //當前index節點的左孩子
152                 var left = 2 * parent + 1;
153 
154                 //當前index節點的右孩子
155                 var right = left + 1;
156 
157                 //parent子節點中最大的孩子節點,方便於parent進行比較
158                 //默認為left節點
159                 var min = left;
160 
161                 //判斷當前節點是否有右孩子
162                 if (right < nodeList.Count)
163                 {
164                     //判斷parent要比較的最大子節點
165                     min = nodeList[left].level < nodeList[right].level ? left : right;
166                 }
167 
168                 //如果parent節點小於它的某個子節點的話,此時篩操作
169                 if (nodeList[parent].level > nodeList[min].level)
170                 {
171                     //子節點和父節點進行交換操作
172                     var temp = nodeList[parent];
173                     nodeList[parent] = nodeList[min];
174                     nodeList[min] = temp;
175 
176                     //繼續進行更下一層的過濾
177                     parent = min;
178                 }
179                 else
180                 {
181                     break;
182                 }
183             }
184         }
185         #endregion
186 
187         #region 獲取元素並下降到指定的level級別
188         /// <summary>
189         /// 獲取元素並下降到指定的level級別
190         /// </summary>
191         /// <returns></returns>
192         public HeapNode GetAndDownPriority(int level)
193         {
194             if (nodeList.Count == 0)
195                 return null;
196 
197             //獲取頭元素
198             var pop = nodeList[0];
199 
200             //設置指定優先級(如果為 MinValue 則為 -- 操作)
201             nodeList[0].level = level == int.MinValue ? --nodeList[0].level : level;
202 
203             //下濾堆
204             DownHeapAdjust(nodeList, 0);
205 
206             return nodeList[0];
207         }
208         #endregion
209 
210         #region 獲取元素並下降優先級
211         /// <summary>
212         /// 獲取元素並下降優先級
213         /// </summary>
214         /// <returns></returns>
215         public HeapNode GetAndDownPriority()
216         {
217             //下降一個優先級
218             return GetAndDownPriority(int.MinValue);
219         }
220         #endregion
221 
222         #region 返回當前優先隊列中的元素個數
223         /// <summary>
224         /// 返回當前優先隊列中的元素個數
225         /// </summary>
226         /// <returns></returns>
227         public int Count()
228         {
229             return nodeList.Count;
230         }
231         #endregion
232     }
233 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM