【问题标题】:C# Creating a TreeView from a Binary Search TreeC# 从二叉搜索树创建 TreeView
【发布时间】:2018-10-13 14:49:00
【问题描述】:

所以,我有一个二叉搜索树,它填充了字符串并具有以下结构:

class Node
    {
        public string data;
        public Node left { get; set; }
        public Node right { get; set; }

        public Node(string data)
        {
            this.data = data;
        }
    }
    class Tree
    {
        public Node root;
        public Tree()
        {
            root = null;
        }
        public void insert(string data, TreeView view)
        {
            Node newItem = new Node(data); 
            if (root == null)
            {
                root = newItem;
                view.Nodes.Add("Root: " + root.data);
            }
            else
            {
                TreeNode sub = new TreeNode();
                Node current = root;
                Node parent = null;
                while (current != null)
                {
                    parent = current;
                    if (String.Compare(data, current.data) < 0)
                    {
                        current = current.left;    
                        if (current == null)
                        {
                            parent.left = newItem;
                        }
                    }
                    else
                    {
                        current = current.right;
                        if (current == null)
                        {
                            parent.right = newItem;     
                        }
                    }
                }
            }
        }
    }

使用view.Nodes.Add("Root: " + root.data);,我成功添加了根元素,但我不确定如何添加其他子节点,因此树视图的结构与二叉树相同。 我需要在 TreeView 中实现这样的目标:

- Root: computer
- - Left: code
- - - Left: analyzing
- - - - Right: and
- - - Right: cooler
- - Right: programming

【问题讨论】:

  • 这听起来你会受益于递归函数,比如如果节点不是最深的孩子,你可以创建一个节点,然后再次调用这个函数
  • 你想要快速搜索还是想要树状结构?
  • 我只需要可视化 TreeView 控件中的树结构。

标签: c# tree binary treeview


【解决方案1】:

不要混合你的模型和视图。创建树然后遍历显示:

public class Node
    {
        public string data;
        public Node left { get; set; }
        public Node right { get; set; }

        public Node(string data)
        {
            this.data = data;
        }
    }
    public class Tree
    {
        public Node root;
        public Tree()
        {
            root = null;
        }
        public void insert(string data)
        {
            Node newItem = new Node(data);
            if (root == null)
            {
                root = newItem;                
            }
            else
            {
                TreeNode sub = new TreeNode();
                Node current = root;
                Node parent = null;
                while (current != null)
                {
                    parent = current;
                    if (String.Compare(data, current.data) < 0)
                    {
                        current = current.left;
                        if (current == null)
                        {
                            parent.left = newItem;
                        }
                    }
                    else
                    {
                        current = current.right;
                        if (current == null)
                        {
                            parent.right = newItem;
                        }
                    }
                }
            }
        }
    }



    public partial class Form1 : Form
    {

        void ShowNode(Node node,TreeNode treeNode)
        {
            treeNode.Text += node.data;
            if (node.left != null)
            {
                ShowNode(node.left, treeNode.Nodes.Add("Left: "));
            }
            if (node.right != null)
            {
                ShowNode(node.right, treeNode.Nodes.Add("Right: "));
            }
        }
        void DisplayTree(Tree tree)
        {
            ShowNode(tree.root,treeView1.Nodes.Add("Root: "));
        }
        public Form1()
        {
            InitializeComponent();
            Tree tree = new Tree();
            tree.insert("computer");
            tree.insert("code");
            tree.insert("programming");
            tree.insert("analyzing");
            tree.insert("cooler");
            tree.insert("and");
            DisplayTree(tree);
        }
    }

注意:此示例仅用于演示目的。对于大树遍历而不是递归,请根据您的目的使用 Queue 或 Stack 类。

【讨论】:

  • 非常感谢您的帮助:)。没有意识到我正在同时进行插入和遍历。这正是我所需要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-23
  • 1970-01-01
相关资源
最近更新 更多