Homework: AVL trees.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • araunity2006
    New Member
    • Mar 2008
    • 1

    Homework: AVL trees.

    I have this question for a lab in Univeraity to make AVLtrees here is the question.


    c) Implement the AVL tree ADT. You must implement the tree class, including insert() and print() methods, as well as the node class. The tree must maintain the AVL property. The insert() method inserts an item into the AVL tree (using the normal binary search tree insert procedure), and then re-balances the trees, using rotations (if necessary). The print() method outputs the AVL tree to the console, using an in-order traversal. The output should include the balance for each node (right subtree height – left subtree height). Sample output from print() is shown below:
    80 (-1)
    70 (0)
    60 (1)
    50 (0)
    40 (1)
    30 (0)
    20 (0)
    10 (0)
    d) Test the AVL tree using the driver class provided.


    I have to use the following Driver class and am having problems in finding any way to implement it.

    [CODE=Java] public class AVLDriver {
    public static void main(String[] args) {

    AVLTree avlt_random = new AVLTree();
    avlt_random.ins ert(26);
    avlt_random.ins ert(11);
    avlt_random.ins ert(31);
    avlt_random.ins ert(6);
    avlt_random.ins ert(17);
    avlt_random.ins ert(44);
    avlt_random.ins ert(2);
    avlt_random.ins ert(39);
    avlt_random.ins ert(20);

    System.out.prin tln("Random AVL Tree:");
    avlt_random.pri nt();
    System.out.prin tln();

    AVLTree avlt_wc = new AVLTree();
    avlt_wc.insert( 2);
    avlt_wc.insert( 6);
    avlt_wc.insert( 11);
    avlt_wc.insert( 17);
    avlt_wc.insert( 20);
    avlt_wc.insert( 26);
    avlt_wc.insert( 31);
    avlt_wc.insert( 39);
    avlt_wc.insert( 44);

    System.out.prin tln("Worst-case AVL Tree:");
    avlt_wc.print() ;
    System.out.prin tln();

    }
    }
    [/CODE]


    As seen the insert method takes in only an integer and not a node. I was trying to implement this using LinkedLists and am lost on how to keep track of the children since in each level I would need to make two pointers to keep track of the left and right child. THis program has to be made in Java. Any help or guidance by today will be highly appreciated.
    Last edited by BigDaddyLH; Mar 10 '08, 05:57 PM. Reason: added code tags
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Please enclose your posted code in [code] tags (See How to Ask a Question).

    This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

    Please use [code] tags in future.

    MODERATOR

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Please remember to provide a meaningful Title for any threads started (see the FAQ entry Use a Good Thread Title).

      This helps to ensure that other members, and also the general public, will have a better chance of finding answers to any similar questions.

      MODERATOR

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

        Please read the Posting Guidelines and particularly the Coursework Posting Guidelines.

        Then when you are ready post a new question in this thread.

        MODERATOR

        Comment

        Working...