-2

I'm creating a software product which is an AVLTree containing the details of Authors. The Author class contains: Name, Year Of Publish and List of Books (using LinkedList<> collection). The Author objects will be stored in the AVLTree with the Name as the key for comparison.

My problem is that I can't seem to store the Author class correctly in the AVLTree.

I appreciate any advice and help.

I create the Author array, and create an AVLTree:

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

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

The code for the 'Add Author' button is as follows:

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

I've implemented CompareTo in the Author class as follows:

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

The InsertItem method in the AVLTree looks like this:

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

    }

And the node class includes this:

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 Answers1

1

It seems to me the problem is here:

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

In particular the order of operations is not right. You are attempting to set properties of author[i] and then you are overwriting that with a new instance of Author.. doesn't make any sense.

It should be:

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

I'm also a bit confused about three other things in your code:

  1. Why do you also have an array holding Authors if you are putting them in the tree in the first place?
  2. Why are you initializing the array of autors like this: public Author[] author = new Author[i];. Where does i come from?
  3. Why are you sorting the array each time you want to insert into the tree? The tree is self-balancing..

And then you are re-using i to initialize/set up the author before inserting into tree.. ?!

To me the following block:

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

Should be re-written as:

Author newAuthor = new Author(textBoxAddAuthor.Text, textBoxYrOfPub.Text);
authorAVL.InsertItem(newAuthor);
Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • Thanks for the reply, yeah I corrected that a few moments ago in my original post, I made an error when pasting some of my code onto the website. I don't have a problem creating new instances of the Author class with user input, the problem is storing these values in the AVLTree. So far, I've only stored 'strings' and 'ints' in the AVLTree and storing objects of the Author class in it is quite problematic –  Mar 15 '13 at 15:35
  • I don't see any corrections. Can you please edit your original question and be more specific about what exactly doesn't work? – Mike Dinescu Mar 15 '13 at 15:43
  • And please see my last snippet. It should be all you need to do to insert an instance of `Author` into the tree.. – Mike Dinescu Mar 15 '13 at 15:44