如何在列表框中搜索专辑并将其连接到AVL树或二叉搜索树?c#

本文关键字:AVL 搜索树 连接 列表 搜索 专辑 | 更新日期: 2023-09-27 18:04:51

我需要出现在列表框中的专辑,以通过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;
        }
    }
}

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

.

//艺术家类{艺术家:我可以比较{

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

}

如何在列表框中搜索专辑并将其连接到AVL树或二叉搜索树?c#

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