二叉树和LinkedList合并

时间:2014-03-07 10:01:58

标签: c# linked-list binary-tree

我正在尝试创建一个记录系统,用户将音乐艺术家名称输入文本字段,然后将其保存到二叉树中。它也会添加到列表框中,然后用户可以单击特定的艺术家名称,然后将相册添加到我想要保存在链接列表中的该艺术家。如果用户想要添加另一个艺术家,他们只需单击艺术家文本字段并添加另一个。我发现很难理解的是为艺术家添加专辑。我开发了几个类,例如带有set的Artist类,以及获取其名称的方法等。

我希望用户保存相册,然后将其保存到艺术家名称,这样如果他们再次点击相同的艺术家,相册就可以选择添加更多。使用.addFirst方法将相册保存到链接列表中,该方法将它们附加到可以看到它们的列表框中。

我希望我已经清楚我需要什么帮助。我现在已经尝试了几天,似乎没有在哪里。

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

    }
  }
 }

我希望能够使用文本框添加艺术家,然后在可能的情况下将专辑添加到该艺术家。任何帮助都会被SOOO赞赏!!

1 个答案:

答案 0 :(得分:0)

我将修改艺术家类以在其中包含一个专辑列表和一个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;
        }
    }

然后在二叉树中添加搜索哪个函数以获取具有GUID的节点。检索该节点的艺术家对象,并将该专辑添加到该节点的专辑列表中。

搜索树

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