我需要出现在列表框中的专辑,以具有通过 AVL 树或二叉搜索树的搜索功能。
这是我的 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;
}
}
}
表单类:
//搜索按钮
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
}
}
}
//专辑类
类专辑{
private String title;
private String daterel;
public Album(String aTitle, String saleDate)
{
daterel = saleDate;
title = aTitle;
}
}
}