Java Reference
In-Depth Information
1
// Print tree rooted at current node using preorder traversal.
2
public void printPreOrder( )
3
{
4
System.out.println( element ); // Node
5
if( left != null )
6
left.printPreOrder( ); // Left
7
if( right != null )
8
right.printPreOrder( ); // Right
9
}
10
11
// Print tree rooted at current node using postorder traversal.
12
public void printPostOrder( )
13
{
14
if( left != null ) // Left
15
left.printPostOrder( );
16
if( right != null ) // Right
17
right.printPostOrder( );
18
System.out.println( element ); // Node
19
}
20
21
// Print tree rooted at current node using inorder traversal.
22
public void printInOrder( )
23
{
24
if( left != null ) // Left
25
left.printInOrder( );
26
System.out.println( element ); // Node
27
if( right != null )
28
right.printInOrder( ); // Right
29
}
figure 18.22
Routines for printing
nodes in preorder,
postorder, and inorder
figure 18.23
(a) Preorder,
(b) postorder, and
(c) inorder visitation
routes
1
7
2
2
3
1
6
1
5
4
6
3
5
3
7
5
7
2
4
4
6
(a)
(b)
(c)
Search WWH ::
Custom Search