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 { class Artist : 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;
}
}
}