Java Reference
In-Depth Information
1 // BinaryNode class; stores a node in a tree.
2 //
3 // CONSTRUCTION: with no parameters, or an Object,
4 // left child, and right child.
5 //
6 // *******************PUBLIC OPERATIONS**********************
7 // int size( ) --> Return size of subtree at node
8 // int height( ) --> Return height of subtree at node
9 // void printPostOrder( ) --> Print a postorder tree traversal
10 // void printInOrder( ) --> Print an inorder tree traversal
11 // void printPreOrder( ) --> Print a preorder tree traversal
12 // BinaryNode duplicate( )--> Return a duplicate tree
13
14 class BinaryNode<AnyType>
15 {
16 public BinaryNode( )
17 { this( null, null, null ); }
18 public BinaryNode( AnyType theElement,
19 BinaryNode<AnyType> lt, BinaryNode<AnyType> rt )
20 { element = theElement; left = lt; right = rt; }
21
22 public AnyType getElement( )
23 { return element; }
24 public BinaryNode<AnyType> getLeft( )
25 { return left; }
26 public BinaryNode<AnyType> getRight( )
27 { return right; }
28 public void setElement( AnyType x )
29 { element = x; }
30 public void setLeft( BinaryNode<AnyType> t )
31 { left = t; }
32 public void setRight( BinaryNode<AnyType> t )
33 { right = t; }
34
35 public static <AnyType> int size( BinaryNode<AnyType> t )
36 { /* Figure 18.19 */ }
37 public static <AnyType> int height( BinaryNode<AnyType> t )
38 { /* Figure 18.21 */ }
39 public BinaryNode<AnyType> duplicate( )
40 { /* Figure 18.17 */ }
41
42 public void printPreOrder( )
43 { /* Figure 18.22 */ }
44 public void printPostOrder( )
45 { /* Figure 18.22 */ }
46 public void printInOrder( )
47 { /* Figure 18.22 */ }
48
49 private AnyType element;
50 private BinaryNode<AnyType> left;
51 private BinaryNode<AnyType> right;
52 }
figure 18.12
The BinaryNode class
skeleton
 
Search WWH ::




Custom Search