From bc112f4f37838248133a6b3e499889dfae9e7ca1 Mon Sep 17 00:00:00 2001 From: astevens2001 <60630786+astevens2001@users.noreply.github.com> Date: Wed, 3 Nov 2021 19:32:44 -0400 Subject: [PATCH] Add files via upload --- Main.java | 131 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 99 insertions(+), 32 deletions(-) diff --git a/Main.java b/Main.java index c949ac1..3723119 100644 --- a/Main.java +++ b/Main.java @@ -1,32 +1,99 @@ -import java.util.*; - -public class Main { - public static void main(String[] args) { - //For now, N == 10 - Integer[] input = new Integer[10]; - for (int i = 0; i < input.length; i++) { - input[i] = (int)(Math.random() * 100); - } - BST searchTree = new BST(input); - } - - private static ArrayList getLeaves(BST tree) { - BST.TreeNode root = tree.getRoot(); - return crawler(root); - } - - private static ArrayList crawler(BST.TreeNode root) { - ArrayList results = new ArrayList(); - 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; - } -} +import java.util.*; + +public class Main { + public static void main(String[] args) { + //TODO: merge in Ariels random code + BST searchTree = new BST(input); + + //arraylist to store the random numbers to find min and max + ArrayList storeRandomNumbers = new ArrayList(); + + 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 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 getLeaves(BST tree) { + return leafCrawler(tree.getRoot()); + } + + //Crawls through tree and returns an ArrayList of the leaves beneath any given + //node + private static ArrayList leafCrawler(BST.TreeNode root) { + ArrayList results = new ArrayList(); + 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 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 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 storeRandomNumbers){ + int min = Collections.min(storeRandomNumbers); + return min; + } + + //function to find max number in arraylist + public static int findMax(ArrayList storeRandomNumbers){ + int max = Collections.max(storeRandomNumbers); + return max; + } +} \ No newline at end of file