在窗体上绘制二叉树 [英] Drawing binary tree on form

查看:88
本文介绍了在窗体上绘制二叉树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在那里

我正在实现用于在c#中压缩数据的Halfman算法,现在我要绘制它的树(由编码方法制成的树).如您在此链接中看到的,该树是一种二进制树http://en.wikipedia.org/wiki/File:Huffman_tree_2.svg)

我会像在上面的链接中看到的一样绘制我的树

我想知道c#有任何工具或方法可以帮助我更轻松地完成这些操作.

我的代码在这里:

hi there

I''m implementing the halfman algorithm that used for compressing the data in c# now i''m going to draw it''s tree (the tree that made by encode method). the tree is a binary kind as you see in this link http://en.wikipedia.org/wiki/File:Huffman_tree_2.svg)

im going to draw my tree exactly like what''s you seen in above link

i want to know is c# has any tools or method that could help me to do these easier.

my code is here:

public class Node
    {
            public char Symbol { get; set; }
            public int Frequency { get; set; }
            public Node Right { get; set; }
            public Node Left { get; set; }
            public List<bool> Traverse(char symbol, List<bool> data)
            {
                    // Leaf
                    if (Right == null && Left == null)
                    {
                            if (symbol.Equals(this.Symbol))
                            {
                                    return data;
                            }
                            else
                            {
                                    return null;
                            }
                    }
                    else
                    {
                            List<bool> left = null;
                            List<bool> right = null;
                            if (Left != null)
                            {
                                    List<bool> leftPath = new List<bool>();
                                    leftPath.AddRange(data);
                                    leftPath.Add(false);
                                    left = Left.Traverse(symbol, leftPath);
                            }
                            if (Right != null)
                            {
                                    List<bool> rightPath = new List<bool>();
                                    rightPath.AddRange(data);
                                    rightPath.Add(true);
                                    right = Right.Traverse(symbol, rightPath);
                            }
                            if (left != null)
                            {
                                    return left;
                            }
                            else
                            {
                                    return right;
                            }
                    }
            }
    }
using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    namespace HuffmanTest
    {
    public class HuffmanTree
    {
            private List<Node> nodes = new List<Node>();
            public Node Root { get; set; }
            public Dictionary<char, int> Frequencies = new Dictionary<char, int>();
            public void Build(string source)
            {
                    for (int i = 0; i < source.Length; i++)
                    {
                            if (!Frequencies.ContainsKey(source[i]))
                            {
                                    Frequencies.Add(source[i], 0);
                            }
                            Frequencies[source[i]]++;
                    }
                    foreach (KeyValuePair<char, int> symbol in Frequencies)
                    {
                            nodes.Add(new Node() { Symbol = symbol.Key, Frequency = symbol.Value });
                    }
                    while (nodes.Count > 1)
                    {
                            List<Node> orderedNodes = nodes.OrderBy(node => node.Frequency).ToList<Node>();
                            if (orderedNodes.Count >= 2)
                            {
                                    // Take first two items
                                    List<Node> taken = orderedNodes.Take(2).ToList<Node>();
                                    // Create a parent node by combining the frequencies
                                    Node parent = new Node()
                                    {
                                            Symbol = '*',
                                            Frequency = taken[0].Frequency + taken[1].Frequency,
                                            Left = taken[0],
                                            Right = taken[1]
                                    };
                                    nodes.Remove(taken[0]);
                                    nodes.Remove(taken[1]);
                                    nodes.Add(parent);
                            }
                            this.Root = nodes.FirstOrDefault();
                    }
            }
            public BitArray Encode(string source)
            {
                    List<bool> encodedSource = new List<bool>();
                    for (int i = 0; i < source.Length; i++)
                    {
                            List<bool> encodedSymbol = this.Root.Traverse(source[i], new List<bool>());
                            encodedSource.AddRange(encodedSymbol);
                    }
                    BitArray bits = new BitArray(encodedSource.ToArray());
                    return bits;
            }
            public string Decode(BitArray bits)
            {
                    Node current = this.Root;
                    string decoded = "";
                    foreach (bool bit in bits)
                    {
                            if (bit)
                            {
                                    if (current.Right != null)
                                    {
                                            current = current.Right;
                                    }
                            }
                            else
                            {
                                    if (current.Left != null)
                                    {
                                            current = current.Left;
                                    }
                            }
                            if (IsLeaf(current))
                            {
                                    decoded += current.Symbol;
                                    current = this.Root;
                            }
                    }
                    return decoded;
            }
            public bool IsLeaf(Node node)
            {
                    return (node.Left == null && node.Right == null);
            }
    }




我的主要代码在这里:

这是我的主要代码:




and my main code is here:

and this is my main code:

class Program
    {
        static void Main(string[] args)
        {
            string input = "Hussein";
            HuffmanTree huffmanTree = new HuffmanTree();

            // Build the Huffman tree
            huffmanTree.Build(input);

            // Encode
            BitArray encoded = huffmanTree.Encode(input);

            Console.Write("Encoded: ");
            foreach (bool bit in encoded)
            {
                Console.Write((bit ? 1 : 0) + "");
            }
            Console.WriteLine();

            // Decode
            string decoded = huffmanTree.Decode(encoded);

            Console.WriteLine("Decoded: " + decoded);

            Console.ReadLine();
        }
    }


如您所见,我们可以使用这部分代码来绘制我们的树,但我不知道如何:


As you see here we could use this part of code to draw our tree but i dont know how:

public string Decode(BitArray bits)
     {
             Node current = this.Root;
             string decoded = "";
             foreach (bool bit in bits)
             {
                     if (bit)
                     {
                             if (current.Right != null)
                             {
                                     current = current.Right;
                             }
                     }
                     else
                     {
                             if (current.Left != null)
                             {
                                     current = current.Left;
                             }
                     }
                     if (IsLeaf(current))
                     {
                             decoded += current.Symbol;
                             current = this.Root;
                     }
             }
             return decoded;
     }


我写了这些,但这还不够,而且真的很困难
我该怎么办


i wrote these but it is not enough and really difficult
what can i do

private void Print_Root()
 {
     Graphics g = this.CreateGraphics();
     g.DrawEllipse(Pens.Black, this.Width / 2, 0, 20, 20);
 }
 private void print_Right(int left,int top)
 {
     Graphics g = this.CreateGraphics();
     g.DrawEllipse(Pens.Black, left, top, 20, 20);
     g.DrawLine(Pens.Black, left+3, top+3, left-10, top-10);
 }
 private void print_left(int left, int top)
 {
     Graphics g = this.CreateGraphics();
     g.DrawEllipse(Pens.Black, left, top, 20, 20);
     g.DrawLine(Pens.Black, left + 3, top + 3, left - 10, top - 10);
 }


我没有很多时间
请你帮我画画

并且使用MSAGL组件很有用(我不知道它是组件还是.dll)

它是免费的,或者我们必须购买.

谢谢

删除了C ++标记-有些代码可能是C ++/CLI(我不知道),但肯定不是C ++


i dont have much time
Would you please to help me to draw

and it is useful to use MSAGL component(i don''t know it is component or .dll)

it is free or we have to buy it.

thanks

Removed C++ tag - some of the code may be C++/CLI (I''d have no idea) but it''s sure not C++

推荐答案

因为您还在这里询问:
Since you also asked here: http://www.nwoods.com/forum/forum_posts.asp?TID=4707&title=how-go-graphical-could-solve-my-problem[^]

we built you a sample with GoDiagram. (note: it''s a commercial product)


这篇关于在窗体上绘制二叉树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆