namespace Hafman
{
class Program
{
static void Main(string[] args)
{
int[] list ={ 2, 4, 1 };
HafmanTree tree = new HafmanTree(list);
// tree.TrackLeaf();
tree.PrintLeaf();
}
}
public class HafmanTree
{
TreeNode root;
public HafmanTree(int[] list)
{
if (list.Length <= 1)
{
throw new Exception("must more than one element!");
}
else
{
OrderedList orderList = new OrderedList();
for (int i = 0; i < list.Length; i++)
{
TreeNode node=new TreeNode(list[i]);
orderList.Add(node);
}
//while (orderList.Count != 0)
//{
// Console.Write((orderList.RemoveFirst() as TreeNode).Element);
//}
while (orderList.Count != 1)
{
TreeNode node1 = orderList.RemoveFirst() as TreeNode;
TreeNode node2 = orderList.RemoveFirst() as TreeNode;
//node1.Path = node1.Path.Insert(0, "0");
//node2.Path = node2.Path.Insert(0, "1");
TreeNode newNode = new TreeNode();
newNode.Element = node1.Element + node2.Element;
newNode.Left = node1;
newNode.Right = node2;
Track(node1, "0");
Track(node2, "1");
//TreeNode tempNode1 = node1;
//TreeNode tempNode2 = node2;
//while (tempNode1.Left != null)
//{
// tempNode1.Left.Path = tempNode1.Left.Path.Insert(0, "0");
// tempNode1 = tempNode1.Left;
//}
//while (tempNode2.Left != null)
//{
// tempNode2.Left.Path = tempNode2.Left.Path.Insert(0, "0");
// tempNode2 = tempNode2.Left;
//}
//while (tempNode2.Right != null)
//{
// tempNode2.Right.Path = tempNode2.Right.Path.Insert(0, "1");
// tempNode2 = tempNode2.Right;
//}
//while (tempNode2.Right!= null)
//{
// tempNode2.Right.Path = tempNode2.Right.Path.Insert(0, "1");
// tempNode2 = tempNode2.Right;
//}
orderList.Add(newNode);
//TreeNode temp = newNode;
//while (temp.Left != null)
//{
// temp = temp.Left;
// temp.Path = temp.Path.Insert(0, "0");
//}
//temp = newNode;
//while (temp.Right != null)
//{
// temp = temp.Right;
// temp.Path = temp.Path.Insert(0, "1");
//}
}
root = orderList.First() as TreeNode;
}
}
public void Track(TreeNode node,string flag)
{
if (node != null && node.Left == null && node.Right == null)
{
node.Path = node.Path.Insert(0, flag);
}
else
{
Track(node.Left, flag);
Track(node.Right, flag);
}
}
//string path = string.Empty;
//public void TrackLeaf()
//{
// TrackLeaf(root, string.Empty);
//}
//private void TrackLeaf(TreeNode root,string temp)
//{
// if (root != null && root.Left == null && root.Right == null)
// {
// root.Path = root.Path.Insert(0,temp);
// }
// else
// {
// TrackLeaf(root.Left,"0");
// TrackLeaf(root.Right,"1");
// }
//}
public void PrintLeaf()
{
PrintLeaf(root);
}
string temp = string.Empty;
private void PrintLeaf(TreeNode root)
{
//Stack stack = new Stack();
//TreeNode current = root;
//string temp = string.Empty;
//while (current != null || stack.Count != 0)
//{
// if (current != null)
// {
// stack.Push(current);
// current = current.Left;
// if (current != null)
// {
// temp += "0";
// }
// }
// else
// {
// current = stack.Peek() as TreeNode;
// stack.Pop();
// if (current.Left == null && current.Right == null)
// {
// current.Path = temp;
// Console.WriteLine(current.Element + " "+current.Path);
// }
// current = current.Right;
// if (current != null)
// {
// temp = temp.Remove(temp.Length - 1);
// temp += "1";
// }
// }
//}
if (root != null && root.Left == null && root.Right == null)
{
Console.WriteLine(root.Element + " " + root.Path + " ");
}
else
{
PrintLeaf(root.Left);
PrintLeaf(root.Right);
}
}
public void InOrder()
{
}
}
}
转载于:https://www.cnblogs.com/healer_zll/archive/2009/03/14/1411234.html