2013-03-20 62 views
-1

我需要出现在列表框中的专辑,以具有通过AVL树或二进制搜索树的搜索功能。如何搜索列表框中的相册并将其连接到AVL树或二进制搜索树? C#

这是我的AVLTree类代码

class AVLTree<T> : BSTree<T> where T : IComparable 
    { 
     public new void InsertItem(T item) 
     { 
      insertItem(item, ref root); 
     } 

     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); 
       tree.balanceFactor = Height(tree.Left)-Height(tree.Right); 
       if (tree.balanceFactor <= -2) 
        rotateLeft(ref tree); 
       if (tree.balanceFactor >= 2) 
        rotateRight(ref tree); 



     } 


     private void rotateLeft(ref Node<T> tree) 
     {    

      if (tree.Right.balanceFactor > 0) 
       rotateRight(ref tree.Right); 
      Node<T> pivot = tree.Right; 
      tree.Right = pivot.Left; 
      pivot.Left = tree; 
      tree = pivot; 



     } 

     private void rotateRight(ref Node<T> tree) 
     { 

      if (tree.Left.balanceFactor > 0) 
       rotateLeft(ref tree.Left); 
      Node<T> pivot = tree.Left;    
      tree.Left = pivot.Right; 
      pivot.Right = tree; 
      tree = pivot; 
     } 
    } 

} 

FormClass:

//搜索按钮

private void Search_Click(object sender, EventArgs e) //BASIC SEARCH NOT SUITABLE YET 
    { 
     listBox1.ClearSelected(); 
     int index = listBox1.FindString(SearchText.Text); 
     if (index <0) 

     { 
      MessageBox.Show("Item not found."); 
      SearchText.Text = String.Empty; 
     } 
     else 
     { 
      listBox1.SelectedIndex = index; 
     } 

// ARTIST CLASS { 类艺术家:IComparable的 {

public String name; //changed to public 
    public String member; 
    public LinkedList<String> Album; 

    public Artist(String artistname, String members, String[] albName) 
     { 
      name = artistname; 
      member = members; 
      Album = new LinkedList<String>(albName); 
     } 

    public LinkedList<String> getAlbum() 
    { 
     return Album; 
    } 

    public int CompareTo(object obj) 
    { 
     if (obj is Artist) //Artist name comparison 
     { 
      Artist other = (Artist)obj; 
      return name.CompareTo(other.name); 
     } 
     if (obj is string) //Album comparison 
     { 
      Artist other = (Artist)obj; 
      return name.CompareTo(other.Album); 
     } 
     else 
      return -999; //Comparison can not be made 
    } 
} 

}

// ALBUM CLASS

类相册 {

private String title; 
    private String daterel; 

    public Album(String aTitle, String saleDate) 
    { 
     daterel = saleDate; 
     title = aTitle;   
    } 
} 

}

+1

目前尚不清楚你要完成什么。您是否试图仅显示与列表框中的“搜索”字符串匹配的数据,或者您是否尝试选择该数据?此外,这是ASP.NET还是WinForms或WPF? (他们可能对列表框有不同的API,但不会期望别人知道这些细节。) – millimoose 2013-03-20 19:56:32

+2

我没有在您的代码中看到有关AVL树或二进制搜索树的任何内容。 – itsme86 2013-03-20 19:57:36

回答

0

假设这是Winforms,您可以将listbox1中的项添加到数组中,然后遍历它们,在每个项上调用您的搜索函数。

+0

好的,谢谢,我会尝试 – user2192536 2013-03-20 22:19:04