【发布时间】: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 控件中的树结构。