26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1120 Data Structures Chapter 19<br />

138 // output node data<br />

139 System.out.print( node.data + " " );<br />

140 }<br />

141<br />

142 } // end class Tree<br />

Fig. Fig. 19.17 19.17 Definitions of TreeNode and Tree for a binary search tree (part 4 of 4).<br />

Let us walk through the binary tree program. Method main of class TreeTest<br />

(Fig. 19.18) begins by instantiating an empty Tree object and s<strong>to</strong>ring it in reference tree<br />

(line 11). The program randomly generates 10 integers, each of which is inserted in<strong>to</strong> the<br />

binary tree through a call <strong>to</strong> synchronized method insertNode (line 21). The program<br />

then performs preorder, inorder and pos<strong>to</strong>rder traversals (these will be explained<br />

shortly) of tree.<br />

1 // Fig. 19.18: TreeTest.java<br />

2 // This program tests class Tree.<br />

3 import com.deitel.jhtp4.ch19.Tree;<br />

4<br />

5 // Class TreeTest definition<br />

6 public class TreeTest {<br />

7<br />

8 // test class Tree<br />

9 public static void main( String args[] )<br />

10 {<br />

11 Tree tree = new Tree();<br />

12 int value;<br />

13<br />

14 System.out.println( "Inserting the following values: " );<br />

15<br />

16 // insert 10 random integers from 0-99 in tree<br />

17 for ( int i = 1; i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!