Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在AVL树中存储Author类_C#_Database_Avl Tree - Fatal编程技术网

C# 在AVL树中存储Author类

C# 在AVL树中存储Author类,c#,database,avl-tree,C#,Database,Avl Tree,我正在创建一个软件产品,它是一个包含作者详细信息的AVLTree。Author类包含:名称、出版年份和图书列表(使用LinkedList集合)。Author对象将以名称作为键存储在AVLTree中以进行比较 我的问题是,我似乎无法在AVLTree中正确存储Author类 我感谢你的建议和帮助 我创建了Author数组,并创建了一个AVLTree: public Author[] author = new Author[i]; public AVLTree<Author> autho

我正在创建一个软件产品,它是一个包含作者详细信息的AVLTree。Author类包含:名称、出版年份和图书列表(使用LinkedList集合)。Author对象将以名称作为键存储在AVLTree中以进行比较

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

我感谢你的建议和帮助

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

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

public AVLTree<Author> authorAVL = new AVLTree<Author>();
我在Author类中实现了CompareTo,如下所示:

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

    }
公共作废插入项(T项)
{
插入项(项,参考根);
}
私有void插入项(T项,引用节点树)
{
if(tree==null)
树=新节点(项目);
else if(项比较到(树数据)<0)
插入项(项目,左参考树);
else if(项比较(树数据)>0)
插入项(项目,参考树右侧);
tree.BalanceFactor=(高度(tree.Left)-高度(tree.Right));
如果(tree.BalanceFactor=2)
旋转光(参考树);
}
节点类包括:

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


}
公共类节点,其中T:IComparable
{
私有T数据;
公共节点左、右;
私人内部平衡系数=0;
公共节点(T项)
{
数据=项目;
左=空;
右=空;
}
公共T数据
{
设置{data=value;}
获取{返回数据;}
}
公共平衡因子
{
设置{balanceFactor=value;}
获取{return balanceFactor;}
}
}

在我看来,问题在于:

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);
对于代码中的其他三件事,我也有点困惑:

  • 如果首先要将作者放在树中,为什么还要有一个数组来容纳作者
  • 为什么要这样初始化autor数组:
    public Author[]Author=new Author[i]
    i
    来自哪里
  • 为什么每次要插入到树中时都要对数组进行排序?这棵树是自我平衡的
  • 然后在插入到树中之前,您正在使用
    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);
    

    谢谢你的回复,是的,我在几分钟前的原始帖子中纠正了这个问题,我在将一些代码粘贴到网站上时出错了。使用用户输入创建Author类的新实例没有问题,问题是将这些值存储在AVLTree中。到目前为止,我只在AVLTree中存储了'strings'和'int',在其中存储Author类的对象是一个相当大的问题,我没有看到任何更正。你能不能编辑一下你原来的问题,更具体地说什么不起作用?请看我的最后一个片段。您只需将
    Author
    的实例插入到树中即可。
    Author[]
    AVLTree
    ?而
    艺术家[]
    突然从何而来?