2

So, I have a binary search tree, which is populated with strings and has the following structure:

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;     
                        }
                    }
                }
            }
        }
    }

Using view.Nodes.Add("Root: " + root.data);, I successfully added the root element but I'm not really sure how to add the other child nodes, so as the structure of the tree view is the same as the binary tree. I need to achieve something like this in the TreeView:

- Root: computer
- - Left: code
- - - Left: analyzing
- - - - Right: and
- - - Right: cooler
- - Right: programming
3
  • This sounds like you would benefit from a recursive function, such as if the node is not the deepest child, you can create a node and then call this function again Commented Oct 13, 2018 at 14:51
  • Do you want fast searches or do you want a tree structure? Commented Oct 13, 2018 at 15:06
  • I just need to visualize the tree structure inside a TreeView control. Commented Oct 13, 2018 at 15:09

1 Answer 1

1

Don't mix your model and view. Create tree and then traverse it to display:

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);
        }
    }

Note: This sample is just for demonstration purposes. For large trees traverse instead of recursion use Queue or Stack classes depending on your purposes.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for the help :). Didn't realise I was doing insertion and traversal at the same time. This was just what I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.