在AVL树中存储Author类

时间:2013-03-15 15:27:30

标签: c# database avl-tree

我正在创建一个软件产品,它是一个包含作者详细信息的AVLTree。 Author类包含:名称,发布年份和书籍列表(使用LinkedList<>集合)。 Author对象将存储在AVLTree中,并将Name作为比较密钥。

我的问题是我似乎无法在AVLTree中正确存储Author类。

我感谢任何建议和帮助。

我创建了Author数组,并创建了一个AVLTree:

public Author[] author = new Author[i];

public AVLTree<Author> authorAVL = new AVLTree<Author>();

“添加作者”按钮的代码如下:

        author[i].Name = textBoxAddAuthor.Text;
        author[i].YrOfPub = textBoxYrOfPub.Text;
        author[i] = new Author(author[i].Name, author[i].YrOfPub);
        Array.Sort(author);

        authorAVL.InsertItem(artist[i]);

我在Author类中实现了CompareTo,如下所示:

public int CompareTo(object obj)
    {
        if (obj is Author) //compare by name
        {
            Author other = (Author)obj;
            return name.CompareTo(other.name);
        }

AVLTree中的InsertItem方法如下所示:

public 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);

    }

节点类包括:

public class Node<T> where T : IComparable
{
    private T data;
    public Node<T> Left, Right;
    private int balanceFactor = 0;

    public Node(T item)
    {
        data = item;
        Left = null;
        Right = null;
    }
    public T Data
    {
        set { data = value; }
        get { return data; }

    }

    public int BalanceFactor
    {
        set { balanceFactor = value; }
        get { return balanceFactor; }
    }


}

1 个答案:

答案 0 :(得分:1)

在我看来问题出在这里:

author[i].Name = textBoxAddAuthor.Text;
author[i].YrOfPub = textBoxYrOfPub.Text;
author[i] = new Author("Name", "yearofpublish");

特别是操作顺序不对。您正在尝试设置author[i]的属性,然后使用Author的新实例覆盖该属性..没有任何意义。

应该是:

author[i] = new Author(textBoxAddAuthor.Text, textBoxYrOfPub.Text);

我对你的代码中的其他三件事情也有点困惑:

  1. 如果您首先将它们放在树中,为什么还有一个包含作者的数组呢?
  2. 为什么要像这样初始化autor数组:public Author[] author = new Author[i];i来自哪里?
  3. 每次要插入树时,为什么要对数组进行排序?树是自平衡的。
  4. 然后你在插入树之前重新使用i来初始化/设置作者..?!

    给我以下一块:

    // where does this i come from here?
    author[i].Name = textBoxAddAuthor.Text;                       // this is useless..
    author[i].YrOfPub = textBoxYrOfPub.Text;                      // this is useless..
    author[i] = new Author(author[i].Name, author[i].YrOfPub);    // overwriting author[i] here
    Array.Sort(author);            // why are you sorting the array each time you insert?
    authorAVL.InsertItem(artist[i]);
    

    应该重写为:

    Author newAuthor = new Author(textBoxAddAuthor.Text, textBoxYrOfPub.Text);
    authorAVL.InsertItem(newAuthor);