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