C# 二叉树和链接列表合并
我正在尝试创建一个记录系统,用户将音乐艺术家的名字输入文本字段,然后保存到二叉树中。它还被添加到一个列表框中,用户可以单击特定的艺术家名称,然后将相册添加到我想要保存在链接列表中的艺术家。如果用户想要添加另一个艺术家,只需单击艺术家文本字段并添加另一个。我发现有一点很难理解,那就是给一位艺术家添加专辑。我已经开发了几个类,比如一个带有set和get方法的Artist类 我希望用户保存相册,然后将其保存到艺术家名称,这样,如果他们再次单击同一艺术家,相册将在那里,并可以选择添加更多。相册将通过.addFirst方法保存到一个链接列表中,该方法将相册附加到可以查看相册的列表框中 我希望我已经清楚我需要什么帮助。我已经试了好几天了,似乎没有什么进展C# 二叉树和链接列表合并,c#,linked-list,binary-tree,C#,Linked List,Binary Tree,我正在尝试创建一个记录系统,用户将音乐艺术家的名字输入文本字段,然后保存到二叉树中。它还被添加到一个列表框中,用户可以单击特定的艺术家名称,然后将相册添加到我想要保存在链接列表中的艺术家。如果用户想要添加另一个艺术家,只需单击艺术家文本字段并添加另一个。我发现有一点很难理解,那就是给一位艺术家添加专辑。我已经开发了几个类,比如一个带有set和get方法的Artist类 我希望用户保存相册,然后将其保存到艺术家名称,这样,如果他们再次单击同一艺术家,相册将在那里,并可以选择添加更多。相册将通过.a
BINARY TREE CLASS WITH INSERT METHOD
class BinarySrchTree<T> where T:IComparable
{
public Node<T> root;
public BinarySrchTree()
{
root = null;
}
public BinarySrchTree(T item)
{
root = new Node<T>(item);
}
private void insertItem(T item, ref Node<T> tree)
{
if (tree == null)
{
tree = new Node<T>(item);
}
else if (item.CompareTo(tree.Data) < 0)
{
insertItem(item, ref tree.Left);
}
else if (item.CompareTo(tree.Data) > 0)
{
insertItem(item, ref tree.Right);
}
}
public void InsertItem(T item)
{
insertItem(item, ref root);
}
NODE CLASS FOR BINARY TREE
namespace Assignment
{
class Node<T> where T:IComparable
{
private T data;
public Node<T> Left, Right;
public Node(T item)
{
data = item;
Left = null;
Right = null;
}
public T Data
{
set { data = value; }
get { return data; }
}
}
}
ARTIST CLASS
public class Artist : IComparable
{
public string name;
public string members;
public string albums;
public Artist()
{
}
public Artist(string name, string members, string album)
{
this.name = name;
this.members = members;
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Members
{
get { return members; }
set { members = value; }
}
public string Albums
{
get { return albums; }
set { albums = value; }
}
public int CompareTo(object obj)
{
if (obj is Artist)
{
Artist other = (Artist)obj;
return name.CompareTo(other.name);
}
if (obj is string)
{
string other = (string)obj;
return members.CompareTo(other);
}
else
{
return -999;
}
}
FORM CLASS (THE BIT I AM STRUGGLING WITH)
public partial class Form1 : Form
{
BinarySrchTree<string> bst = new BinarySrchTree<string>();
LinkedList<string> albm = new LinkedList<string>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string artist = textBox1.Text;
string members = textBox3.Text;
bst.InsertItem(artist);
listBox1.Items.Add(artist + "\t" + members);
Artist a = new Artist();
a.Name = artist;
a.Members = members;
}
private void button2_Click(object sender, EventArgs e)
{
string albums = textBox2.Text;
albm.AddFirst(albums);
listBox2.Items.Add(albums);
}
}
}
具有插入方法的二叉树类
类BinarySrchTree,其中T:IComparable
{
公共节点根;
公共二进制srchtree()
{
root=null;
}
公共二进制srchtree(T项)
{
根=新节点(项);
}
私有void插入项(T项,引用节点树)
{
if(tree==null)
{
树=新节点(项目);
}
else if(项比较到(树数据)<0)
{
插入项(项目,左参考树);
}
else if(项比较(树数据)>0)
{
插入项(项目,参考树右侧);
}
}
公共无效插入项(T项)
{
插入项(项,参考根);
}
二叉树的节点类
名称空间分配
{
类节点,其中T:IComparable
{
私有T数据;
公共节点左、右;
公共节点(T项)
{
数据=项目;
左=空;
右=空;
}
公共T数据
{
设置{data=value;}
获取{返回数据;}
}
}
}
艺术家班
公共级艺术家:IComparable
{
公共字符串名称;
公共字符串成员;
公共弦乐专辑;
公共艺术家()
{
}
公共艺术家(字符串名称、字符串成员、字符串相册)
{
this.name=名称;
这个.成员=成员;
}
公共字符串名
{
获取{返回名称;}
设置{name=value;}
}
公共字符串成员
{
获取{返回成员;}
集合{members=value;}
}
公共字符串相册
{
获取{返回相册;}
设置{albums=value;}
}
公共整数比较(对象对象对象)
{
如果(obj是艺术家)
{
艺术家其他=(艺术家)obj;
返回name.CompareTo(其他.name);
}
if(obj是字符串)
{
字符串other=(字符串)obj;
返回成员。与(其他)相比;
}
其他的
{
返回-999;
}
}
表单类(我正在努力解决的问题)
公共部分类Form1:Form
{
BinarySrchTree bst=新的BinarySrchTree();
LinkedList albm=新LinkedList();
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
字符串艺术家=textBox1.Text;
字符串成员=textBox3.Text;
bst.InsertItem(艺术家);
listBox1.Items.Add(艺术家+“\t”+成员);
艺术家a=新艺术家();
a、 姓名=艺术家;
a、 成员=成员;
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
字符串相册=textBox2.Text;
albm.AddFirst(相册);
listBox2.Items.Add(相册);
}
}
}
我希望能够使用文本框添加一个艺术家,然后如果可能的话,将相册添加到该艺术家。非常感谢您的帮助!!我将修改艺术家类,使其具有相册列表和每个艺术家对象唯一的GUID字段
public class Artist : IComparable
{
public string name;
public string members;
public string albums;
public Guid artistID;
public List<String> albums;
public Artist()
{
artistID = new Guid();
}
public Artist(string name, string members, string album)
{
this.name = name;
this.members = members;
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Members
{
get { return members; }
set { members = value; }
}
public string Albums
{
get { return albums; }
set { albums = value; }
}
public int CompareTo(object obj)
{
if (obj is Artist)
{
Artist other = (Artist)obj;
return name.CompareTo(other.name);
}
if (obj is string)
{
string other = (string)obj;
return members.CompareTo(other);
}
else
{
return -999;
}
}
感谢您的帮助,但我仍在努力理解如何实现它。我希望它能够将艺术家添加到列表框,然后用户单击列表框条目,然后通过吉他将相册添加到该艺术家。这应该仍然有效。您只保留一个模型对象,并且可以使用它管理所有内容。因此,当您添加艺术家创建一个新对象,并在添加新相册时将其添加到所选艺术家对象的相册列表中。此外,为什么要对艺术家对象使用树结构。
public Node<T> FindByValue(Guid value)
{
// search the list for the value
foreach (Node<T> node in Items)
if (node.artistID.Equals(value))
return node;
// if we reached here, we didn't find a matching node
return null;
}
node.albums.add(album);