-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
64 lines (54 loc) · 1.83 KB
/
Main.java
File metadata and controls
64 lines (54 loc) · 1.83 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
import java.util.*;
public class Main {
public static void main(String[] args) {
//TODO: merge in Ariels random code
BST<Integer> searchTree = new BST<Integer>(input);
ArrayList<Integer> leaves = getLeaves(searchTree);
System.out.println("The height of the tree is: " + getHeight(searchTree));
System.out.println("The # of nodes: " + (searchTree.size()-leaves.size()));
System.out.println("The # of leaves: " + leaves.size());
//Ordered prints
System.out.print("Print the tree in inorder");
searchTree.inorder();
System.out.println();
System.out.print("Print the tree in preorder");
searchTree.preorder();
System.out.println();
System.out.print("Print the tree in postorder");
searchTree.postorder();
System.out.println();
//Print paths
System.out.print("Print the paths: [ ");
for (int leaf: leaves) {
searchTree.path(leaf);
System.out.print(" ");
}
System.out.println("]");
}
private static ArrayList<Integer> getLeaves(BST<Integer> tree) {
return leafCrawler(tree.getRoot());
}
//Crawls through tree and returns an ArrayList of the leaves beneath any given
//node
private static ArrayList<Integer> leafCrawler(BST.TreeNode<Integer> root) {
ArrayList<Integer> results = new ArrayList<Integer>();
if (root.left == null && root.right == null) {
results.add(root.element);
return results;
}
if (root.left != null) {
results.addAll(crawler(root.left));
}
if (root.right != null) {
results.addAll(crawler(root.right));
}
return results;
}
private static int getHeight(BST tree) {
return heightCrawler(tree.getRoot());
}
private static int heightCrawler(BST.TreeNode<Integer> root) {
if (root == null) return 1;
return Math.max(preorder(root.left), preorder(root.right)) + 1;
}
}