初学者 求教 c# 哈夫曼树
我的程序出错,请教高手指点。小弟不胜感激!public class HuffmanTree
{
private Node[] data; // 结点数组
private int leafNum; // 叶子结点数目
// 索引器
public Node this[int index]
{
get
{
return data[index];
}
set
{
data[index] = value;
}
}
// 叶子结点数目属性
public int LeafNum
{
get
{
return leafNum;
}
set
{
leafNum = value;
}
}
// 构造器
public HuffmanTree (int n)
{
Node[] data = new Node[2*n-1];
leafNum = n;
}
// 创建哈夫曼树
public void Create()
{
// 输入n 个叶子结点的权值
for (int i = 0; i < leafNum; ++i)
{
data[i].Weight = Convert.ToInt32( Console.ReadLine());
}
//处理 n 个叶子结点,建立哈夫曼树
for (int i = 0; i < this.leafNum - 1; ++i)
{
int max2;
int tmp2;
int max1 = Int32.MaxValue;
max2 = Int32.MaxValue;
int tmp1 = tmp2 = 0;
//在全部结点中找权值最小的两个结点
for (int j = 0; j < this.leafNum + i; ++j)
{
if ((data[i].Weight < max1)
&& (data[i].Parent == -1))
{
max2 = max1;
tmp2 = tmp1;
tmp1 = j;
max1 = data[j].Weight;
}
else if ((data[i].Weight < max2)
&& (data[i].Parent == -1))
{
max2 = data[j].Weight;
tmp2 = j;
}
}
data[tmp1].Parent = this.leafNum + i;
data[this.leafNum + i].Weight = data[tmp1].Weight
+ data[tmp2].Weight;
data[this.leafNum + i].LChild = tmp1;
data[this.leafNum + i].RChild = tmp2;
}
}
}
class Program
{
static void Main(string[] args)
{
HuffmanTree ht = new HuffmanTree(21);
ht.Create();
}
}
}