Java Reference
In-Depth Information
47 System.out.println();
48 }
49 }
After inserting 25, 20, 5:
Inorder (sorted): 5 20 25
Postorder: 5 25 20
Preorder: 20 5 25
The number of nodes is 3
After inserting 34, 50:
Inorder (sorted): 5 20 25 34 50
Postorder: 5 25 50 34 20
Preorder: 20 5 34 25 50
The number of nodes is 5
After inserting 30
Inorder (sorted): 5 20 25 30 34 50
Postorder: 5 20 30 50 34 25
Preorder: 25 20 5 34 30 50
The number of nodes is 6
After inserting 10
Inorder (sorted): 5 10 20 25 30 34 50
Postorder: 5 20 10 30 50 34 25
Preorder: 25 10 5 20 34 30 50
The number of nodes is 7
After removing 34, 30, 50:
Inorder (sorted): 5 10 20 25
Postorder: 5 20 25 10
Preorder: 10 5 25 20
The number of nodes is 4
After removing 5:
Inorder (sorted): 10 20 25
Postorder: 10 25 20
Preorder: 20 10 25
The number of nodes is 3
Traverse the elements in the tree: 10 20 25
Figure 26.10 shows how the tree evolves as elements are added to the tree. After
25
and
20
are added, the tree is as shown in Figure 26.10a.
5
is inserted as a left child of
20
, as shown in
Figure 26.10b. The tree is not balanced. It is left-heavy at node
25
. Perform an LL rotation to
result in an AVL tree, as shown in Figure 26.10c.
After inserting
34
, the tree is shown in Figure 26.10d. After inserting
50
, the tree is as
shown in Figure 26.10e. The tree is not balanced. It is right-heavy at node
25
. Perform an RR
rotation to result in an AVL tree, as shown in Figure 26.10f.
After inserting
30
, the tree is as shown in Figure 26.10g. The tree is not balanced. Perform
an RL rotation to result in an AVL tree, as shown in Figure 26.10h.
After inserting
10
, the tree is as shown in Figure 26.10i. The tree is not balanced. Perform
an LR rotation to result in an AVL tree, as shown in Figure 26.10j.
Search WWH ::
Custom Search