C # Создание TreeView из дерева двоичного поиска - PullRequest
0 голосов
/ 13 октября 2018

Итак, у меня есть двоичное дерево поиска, которое заполнено строками и имеет следующую структуру:

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

1 Ответ

0 голосов
/ 13 октября 2018

Не смешивайте свою модель и вид.Создайте дерево и затем просмотрите его, чтобы отобразить:

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 в зависимости от ваших целей.

...