diff --git a/src/Practice.java b/src/Practice.java index ca8e22b..71634cd 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,5 +1,9 @@ +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Queue; import java.util.Set; public class Practice { @@ -12,7 +16,14 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + if(nums == null) return 0; + int sum = 0; + for(int num: nums) { + if (num % 2 != 0) { + sum += num; + } + } + return sum; } /** @@ -27,7 +38,24 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + //"kumquat", "date", "fig", "grape", "cherry" + if (words == null) { throw new NullPointerException(); } + if (words.isEmpty()) { throw new IllegalArgumentException(); } + + int length = Integer.MAX_VALUE; + String shortest = ""; + for(String word: words){ + if (word.length() < length) { + shortest = word; + length = word.length(); + } else if (word.length() == length) { + if (word.compareTo(shortest) < 0) { + shortest = word; + } + } + } + + return shortest; } /** @@ -40,7 +68,14 @@ public static String shortestWord(Set words) { * @throws NullPointerException if ages is null */ public static Set adults(Map ages) { - return null; + if (ages == null) { throw new NullPointerException(); } + Set adult = new HashSet<>(); + for(String age: ages.keySet()){ + if (ages.get(age) >= 18) { + adult.add(age); + } + } + return adult; } /** @@ -51,7 +86,14 @@ public static Set adults(Map ages) { * @throws IllegalArgumentException if head is null */ public static int biggestNumber(ListNode head) { - return 0; + if (head == null) { throw new IllegalArgumentException();} + ListNode current = head; + int max = head.data; + while (current != null) { + max = Math.max(max, current.data); + current = current.next; + } + return max; } /** @@ -68,7 +110,18 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + if (head == null) { return new HashMap<>();} + Map frequency = new HashMap<>(); + ListNode current = head; + while (current != null) { + if (!frequency.containsKey(current.data)) { + frequency.put(current.data, 1); + } else { + frequency.put(current.data, frequency.get(current.data) + 1); + } + current = current.next; + } + return frequency; } @@ -81,7 +134,10 @@ public static Map frequencies(ListNode head) { * @return the number of levels in the tree */ public static int levelCount(BinaryTreeNode root) { - return 0; + if (root == null) { return 0; } + int leftTree = levelCount(root.left) + 1; + int leftRight = levelCount(root.right) + 1; + return Math.max(leftTree, leftRight); } @@ -109,7 +165,26 @@ public static int levelCount(BinaryTreeNode root) { * @return the sum of the nodes at the given level */ public static int sumAtLevel(BinaryTreeNode root, int level) { - return 0; + if (root == null) { return 0;} + + Queue> q = new LinkedList<>(); // 1 + int sum = 0; // 7 + 9 + 2 + int L = 1; // 3 + q.add(root); + + while(!q.isEmpty()){ + for(int i = q.size(); i > 0; i--){ + BinaryTreeNode current = q.poll(); // 7 9 2 + if (L == level) { + sum += current.data; + } + + if (current.left != null){ q.add(current.left);} + if (current.right != null){ q.add(current.right);} + } + L += 1; + } + return sum; } @@ -124,7 +199,27 @@ public static int sumAtLevel(BinaryTreeNode root, int level) { * @return true if the sums are equal, false otherwise */ public static boolean sumMatch(BinaryTreeNode root, ListNode head) { - return false; + int rootSum = sumMatchHelper(root); + int listSum = sumMatchHelper(head); + return rootSum == listSum ? true : false; + } + + public static int sumMatchHelper(BinaryTreeNode root){ + if (root == null) return 0; + int leftTree = sumMatchHelper(root.left); + int rightTree = sumMatchHelper(root.right); + return root.data + leftTree + rightTree ; + } + + public static int sumMatchHelper(ListNode head){ + if(head == null) return 0; + ListNode current = head; + int sum = 0; + while (current != null) { + sum += current.data; + current = current.next; + } + return sum; } /** @@ -136,7 +231,12 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h * @return the sum of all the tree's values */ public static int nbSum(TreeNode root) { - return 0; + if (root == null) return 0; + int sum = root.data; + for(TreeNode child: root.children){ + sum += nbSum(child); + } + return sum; } /** @@ -168,7 +268,15 @@ public static int nbSum(TreeNode root) { * @return the count of nodes that do not have siblings, EXCLUDING THE ROOT */ public static int onlyChildCount(TreeNode root) { - return 0; + if(root == null) return 0; + int count = 0; + + for(TreeNode child: root.children){ + if (root.children.size() == 1) count++; + count += onlyChildCount(child); + } + + return count; } /** @@ -205,7 +313,32 @@ public static int onlyChildCount(TreeNode root) { * @param root the root value of the tree * @return the depth of the tree, or 0 if the tree is null or the root is not present in the tree */ + /* + /* + Map: Tree: + A -> [B, E, C] A + B -> [E] / | \ + E -> [] B E C + C -> [D, Q] | / \ + D -> [Z] E D Q + Q -> [] \ + Z -> [] Z + */ + public static int maxDepth(Map> tree, T root) { - return 0; + if (root == null || tree == null || !tree.containsKey(root)) return 0; + int depth = maxDepthHelper(tree, root); + return depth; + } + + public static int maxDepthHelper(Map> tree, T root){ + if(!tree.containsKey(root)) return 1; + int depth = 1; + List children = tree.get(root); + + for(T child: children) { + depth = Math.max(maxDepthHelper(tree, child) + 1, depth); + } + return depth; } } \ No newline at end of file