forked from wesleymg/GroupProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
99 lines (82 loc) · 3.47 KB
/
Main.java
File metadata and controls
99 lines (82 loc) · 3.47 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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 to store the random numbers to find min and max
ArrayList<Integer> storeRandomNumbers = new ArrayList<Integer>();
Random rand = new Random();
//use populateBST function to populate BST and arraylist with the random numbers
populateBST(searchTree, rand, storeRandomNumbers);
System.out.println();
System.out.println("The min node is: " + findMin(storeRandomNumbers));
System.out.println("The max node is: " + findMax(storeRandomNumbers));
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;
}
//function to populate BST and arraylist with the random numbers
public static void populateBST(BST randomBST, Random rand, ArrayList<Integer> storeRandomNumbers) {
System.out.print("Generate a random BST with 10 nodes:");
for (int i = 0; i < 10; i++) {
int random = rand.nextInt(50) + 1;
randomBST.insert(random);
storeRandomNumbers.add(random);
System.out.print("\t" + random);
}
}
//function to find min number in arraylist
public static int findMin(ArrayList<Integer> storeRandomNumbers){
int min = Collections.min(storeRandomNumbers);
return min;
}
//function to find max number in arraylist
public static int findMax(ArrayList<Integer> storeRandomNumbers){
int max = Collections.max(storeRandomNumbers);
return max;
}
}