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.
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.
Comment