Java Reference
In-Depth Information
26.8 Testing the AVLTree Class
This section gives an example of using the AVLTree class.
Key
Point
Listing 26.4 gives a test program. The program creates an AVLTree initialized with an array
of the integers 25 , 20 , and 5 (lines 4-5), inserts elements in lines 9-18, and deletes elements
in lines 22-28. Since AVLTree is a subclass of BST and the elements in a BST are iterable, the
program uses a foreach loop to traverse all the elements in lines 33-35.
L ISTING 26.4
TestAVLTree.java
1 public class TestAVLTree {
2 public static void main(String[] args) {
3 // Create an AVL tree
4 AVLTree<Integer> tree = new AVLTree<>( new Integer[]{ 25 ,
5 20 , 5 });
6 System.out.print( "After inserting 25, 20, 5:" );
7 printTree(tree);
8
9 tree.insert( 34 );
10 tree.insert( 50 );
11 System.out.print( "\nAfter inserting 34, 50:" );
12 printTree(tree);
13
14 tree.insert( 30 );
15 System.out.print( "\nAfter inserting 30" );
16 printTree(tree);
17
18 tree.insert( 10 );
19 System.out.print( "\nAfter inserting 10" );
20 printTree(tree);
21
22 tree.delete( 34 );
23 tree.delete( 30 );
24 tree.delete( 50 );
25 System.out.print( "\nAfter removing 34, 30, 50:" );
26 printTree(tree);
27
28 tree.delete( 5 );
29 System.out.print( "\nAfter removing 5:" );
30 printTree(tree);
31
32 System.out.print( "\nTraverse the elements in the tree: " );
33 for ( int e: tree) {
34 System.out.print(e + " " );
35 }
36 }
37
38 public static void printTree(BST tree) {
39 // Traverse tree
40 System.out.print( "\nInorder (sorted): " );
41 tree.inorder();
41 System.out.print( "\nPostorder: " );
43 tree.postorder();
44 System.out.print( "\nPreorder: " );
45 tree.preorder();
46 System.out.print( "\nThe number of nodes is " + tree.getSize());
create an AVLTree
insert 34
insert 50
insert 30
insert 10
delete 34
delete 30
delete 50
delete 5
for-each loop
 
 
 
Search WWH ::




Custom Search