-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIntTree.java
More file actions
73 lines (58 loc) · 2.26 KB
/
IntTree.java
File metadata and controls
73 lines (58 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Binary tree class includes methods to construct a
// tree of ints, to print its structure, and print the data
// using inorder traversal. The trees
// built have nodes numbered starting with 1 and numbered
// sequentially level by level with no gaps in the tree, aka as "sequential trees."
public class IntTree {
private IntTreeNode overallRoot;
// Cnstruct a sequential tree with given number of nodes
public IntTree(int max) {
if (max <= 0) {
throw new IllegalArgumentException("max: " + max);
}
overallRoot = buildTree(1, max);
}
// Return a sequential tree with n as its root unless
// n is greater than max, in which case it returns an empty tree
private IntTreeNode buildTree(int n, int max) {
if (n > max) {
return null;
} else {
return new IntTreeNode(n, buildTree(2 * n, max),
buildTree(2 * n + 1, max));
}
}
// Print the tree contents using a inorder traversal
public void printInorder() {
System.out.print("inorder:");
printInorder(overallRoot);
System.out.println();
}
// post: prints in inorder the tree with given root
private void printInorder(IntTreeNode root) {
if (root != null) {
printInorder(root.left);
System.out.print(" " + root.data);
printInorder(root.right);
}
}
// Print the tree contents, one per line, following an
// inorder traversal and using indentation to indicate
// node depth, from right to left so that it looks
// correct when the output is rotated.
public void printTree() {
printTree(overallRoot, 0);
}
// post: prints in reversed preorder the tree with given
// root, indenting each line to the given level
private void printTree(IntTreeNode root, int level) {
if (root != null) {
printTree(root.right, level + 1);
for (int i = 0; i < level; i++) {
System.out.print(" ");
}
System.out.println(root.data);
printTree(root.left, level + 1);
}
}
}