【问题标题】:Print BST Preorder in Windows Form label在 Windows 窗体标签中打印 BST 预购
【发布时间】:2016-05-04 15:16:11
【问题描述】:

可能是一个超级简单的问题,但我不知道如何处理表单。

我需要在 Winform 的代码中调用 Preorder 遍历方法(在另一个类中实现),以便 Preorder 打印在 Form 接口的标签中。

所以我有一个 BST 类,其中有一个在 Preorder 中遍历树的方法。还有一种向树中插入值的方法。像这样:

namespace BinaryTree //the BST's class
{
   public partial class BinarySearchTreeNode<T> where T : IComparable<T>
   {


public void Insert(T value) //method for inserting
    {
       ....
    }

public IEnumerable<T> Preorder() //method for Preorder traversal
    {
        List<T> preOrdered= new List<T>();
        if (_value != null)
        {
            preOrdered.Add(Value);

            if (LeftChild != null) //
            {
                preOrdered.AddRange(LeftChild.Preorder());
            }

            if (RightChild != null) //
            {
                preOrdered.AddRange(RightChild.Preorder());
            }
        }
        return preOrdered;
    }
}

现在,要使用这些操作,我有一个 Windows 窗体界面。它具有创建新树的代码,向树添加值(通过在 inputTextBox 中键入值并单击 btnCreate)和 显示树用户(通过 PaintTree),但我还需要向用户打印树的 Preorder(例如在界面中的标签中)。

假设我在界面中有一个名为“PreorderLabel”的标签;这是我想要打印预购的地方

表单的代码如下所示:

namespace BinaryTree
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private BinarySearchTree<int> _tree;


    void PaintTree()
    {
        if (_tree == null) return;
        pictureBox1.Image = _tree.Draw();
    }




    private void btnCreate_Click(object sender, EventArgs e)
    {
        try
        {
            _tree = new BinarySearchTree<int>(new BinarySearchTreeNode<int>(int.MinValue));
            PaintTree();
        }

        catch(NotImplementedException) { MessageBox.Show("There is no implementation!"); }
    }


    private void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            var val = int.Parse(inputTextBox.Text);  //makes a variable out of the input value from the user, to work with
            if (_tree == null)
                btnCreate_Click(btnCreate, new EventArgs());
            _tree.Insert(val);  //***calls the "Insert" method from the BST class, to insert the value to the tree
            PaintTree();  //shows the user the tree

//**this is [I guess] where I need code for printing the tree in Preorder**
           PreorderLabel.Text = ???????

            inputTextBox.SelectAll();
            this.Update();
        }

        catch (Exception exp) { MessageBox.Show(exp.Message); }
    }

所以,当用户点击添加按钮(btnAdd)时,树“_tree”不仅会调用Insert方法,还会调用PaintTree方法来显示树;还要调用 Preorder 方法来在标签“PreorderLabel”中打印 Preorder

如何做到这一点?

如果能得到所有帮助,我将非常高兴!

【问题讨论】:

  • 前序遍历方法代码显示在第一个代码粘贴中?还是?
  • 嗯,这不是超级简单的问题,主要是因为不清楚label 中的print 是什么意思。标签只是一个静态文本,你真的想在那里打印什么?
  • 对不起。我正在考虑每次用户添加值时将“label.Text”更改为预先排序的树节点字符串,这就是我想在那里打印的内容。

标签: c# winforms label binary-search-tree inorder


【解决方案1】:

您可以将String.Join(string separator, IEnumerable&lt;string&gt; values) 方法与如下分隔符字符串一起使用:

PreorderLabel.Text = String.Join("; ", _tree.PreOrder().Select(item => item.ToString()));

PreOrder() 方法将返回IEnumerable&lt;int&gt; 实例,所以我们只需将int 值转换为string

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多