Java Reference
In-Depth Information
29
return
root;
30 }
31
32 // post: returns true if overall tree contains value
33
public boolean
contains(
int
value) {
34
return
contains(overallRoot, value);
35 }
36
37 // post: returns true if given tree contains value
38
private boolean
contains(IntTreeNode root,
int
value) {
39
if
(root == null) {
40
return
false;
41 }
else if
(value == root.data) {
42
return
true;
43 }
else if
(value < root.data) {
44
return
contains(root.left, value);
45 }
else
{ // value > root.data
46
return
contains(root.right, value);
47 }
48 }
49
50
51 // post: prints the tree contents using an inorder traversal
52
public void
print() {
53 printInorder(overallRoot);
54 System.out.println();
55 }
56
57 // post: prints contents of the tree with given root using an
58 // inorder traversal
59
private void
printInorder(IntTreeNode root) {
60
if
(root != null) {
61 printInorder(root.left);
62 System.out.print(root.data + " ");
63 printInorder(root.right);
64 }
65 }
66
67 // post: prints the tree contents, one per line, following an
68 // inorder traversal and using indentation to indicate
69 // node depth; prints right to left so that it looks
70 // correct when the output is rotated.
71
public void
printSideways() {
72 printSideways(overallRoot, 0);
Search WWH ::
Custom Search