2013-03-20 60 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