哈夫曼树又叫最优二叉树,指具有一组特定权值的叶子节点的具有最小带权路径长度的二叉树。

  实现代码如下:

 

  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4
  5namespace 哈夫曼树
  6{
  7    class Program
  8    {
  9        public class Node
 10        
 11            //节点权值
 12            private int weight;
 13            //左孩子
 14            private int lChild;
 15            //右孩子
 16            private int rChild;
 17            //父结点
 18            private int parent;
 19            //节点权值属性
 20            public int Weight
 21            {
 22                get
 23                {
 24                    return weight;
 25                }

 26                set
 27                {
 28                    weight = value;
 29                }

 30            }

 31            //左孩子节点属性
 32            public int LChild
 33            {
 34                get
 35                {
 36                    return lChild;
 37                }

 38                set
 39                {
 40                    lChild = value;
 41                }

 42            }

 43            //右孩子节点属性
 44            public int RChild
 45            {
 46                get
 47                {
 48                    return rChild;
 49                }

 50                set
 51                {
 52                    rChild = value;
 53                }

 54            }

 55            //父节点属性
 56            public int Parent
 57            {
 58                get
 59                {
 60                    return parent;
 61                }

 62                set
 63                {
 64                    parent = value;
 65                }

 66            }

 67
 68            //构造函数
 69            public Node()
 70            {
 71                weight = 0;
 72                lChild = -1;
 73                rChild = -1;
 74                parent = -1;
 75            }

 76            //构造函数
 77            public Node(int w, int l, int r, int p)
 78            {
 79                weight = w;
 80                lChild = l;
 81                rChild = r;
 82                parent = p;
 83            }

 84            
 85        }

 86        //哈夫曼树类
 87        public class HaffmanTree
 88        {
 89            //节点数目
 90            private Node[] data;
 91            //叶子节点数目
 92            private int leafNum;
 93            //索引器
 94            public Node this[int index]
 95            {
 96                get
 97                {
 98                    return data[index];
 99                }

100                set
101                {
102                    data[index] = value;
103                }

104            }

105            //叶子节点数目属性
106            public int LeafNum
107            {
108                get
109                {
110                    return leafNum;
111                }

112                set
113                {
114                    leafNum = value;
115                }

116            }

117            //构造函数
118            public HaffmanTree(int n)
119            {
120                data = new Node[2 * n - 1];
121                leafNum = n;
122            }

123            //创建哈夫曼树
124            public void CreateTree()
125            {
126                int max1, max2, tmp1, tmp2;
127                //输入N个叶子节点的权值
128                for (int i = 0; i < this.leafNum; i++)
129                {
130                    data[i].Weight = Console.Read();
131                }

132                //处理N个叶子节点,建立哈夫曼树
133                for (int i = 0; i < this.leafNum - 1; i++)
134                {
135                    
136                    max1 = max2 = Int32.MaxValue;
137                    tmp1 = tmp2 = 0;
138                    //在全部节点中找权值最小的两个节点
139                    for (int j = 0; j < this.leafNum + i; ++j)
140                    {
141                        if ((data[j].Weight < max1) && (data[i].Parent == -1))
142                        {
143                            max2 = max1;
144                            tmp2 = tmp1;
145                            tmp1 = j;
146                            max1 = data[j].Weight;
147                        }

148                        else if ((data[j].Weight < max2) && (data[i].Parent == -1))
149                        {
150                            max2 = data[j].Weight;
151                            tmp2 = j;
152                        }

153
154                    }

155
156                    data[tmp1].Parent = this.leafNum + i;
157                    data[this.leafNum + i].Weight = data[tmp1].Weight + data[tmp2].Weight;
158                    data[this.leafNum + i].LChild = tmp1;
159                    Console.WriteLine(data[this.leafNum + i].LChild);
160                    data[this.leafNum + i].RChild = tmp2;
161                    Console.WriteLine(data[this.leafNum + i].RChild);
162
163                }

164            }

165        }

166        static void Main(string[] args)
167        {
168
169            HaffmanTree haffman = new HaffmanTree(5);
170            haffman.CreateTree();
171        }

172    }

173}

174
posted on 2009-03-24 00:20  甲乙丙丁  阅读(595)  评论(0编辑  收藏  举报