C#从二叉搜索树创建树视图
因此,我有一个二进制搜索树,它由字符串填充,具有以下结构:C#从二叉搜索树创建树视图,c#,tree,binary,treeview,C#,Tree,Binary,Treeview,因此,我有一个二进制搜索树,它由字符串填充,具有以下结构: class Node { public string data; public Node left { get; set; } public Node right { get; set; } public Node(string data) { this.data = data; } } cl
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;
}
}
}
}
}
}
不要混淆模型和视图。创建树,然后遍历它以显示:
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);
}
}
公共类节点
{
公共字符串数据;
左公共节点{get;set;}
公共节点权限{get;set;}
公共节点(字符串数据)
{
这个数据=数据;
}
}
公共类树
{
公共节点根;
公树()
{
root=null;
}
公共void插入(字符串数据)
{
节点newItem=新节点(数据);
if(root==null)
{
root=newItem;
}
其他的
{
TreeNode sub=新的TreeNode();
节点电流=根;
节点父节点=null;
while(当前!=null)
{
父项=当前;
if(字符串比较(数据,当前数据)<0)
{
current=current.left;
如果(当前==null)
{
parent.left=newItem;
}
}
其他的
{
current=current.right;
如果(当前==null)
{
parent.right=newItem;
}
}
}
}
}
}
公共部分类Form1:Form
{
void ShowNode(节点节点,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(树树)
{
ShowNode(tree.root,treeView1.Nodes.Add(“root:”);
}
公共表格1()
{
初始化组件();
树=新树();
树。插入(“计算机”);
树。插入(“代码”);
插入(“编程”);
树。插入(“分析”);
树。插入(“冷却器”);
树。插入(“和”);
显示树(树);
}
}
注意:此示例仅用于演示目的。对于大型树遍历而不是递归,根据您的目的使用队列或堆栈类。这听起来像是递归函数的好处,例如如果节点不是最深的子节点,您可以创建一个节点,然后再次调用此函数。您想要快速搜索还是想要树结构?我只需要在TreeView控件中可视化树结构。非常感谢您的帮助:)。没有意识到我在同时进行插入和遍历。这正是我需要的。
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);
}
}