diff --git a/src/Practice.java b/src/Practice.java index ca8e22b..0a8e3cf 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,6 +1,9 @@ +import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.Stack; public class Practice { /** @@ -12,7 +15,13 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + int sum = 0; + if(nums == null) return sum; + for(int num : nums) { + if(num % 2 != 0) + sum += num; + } + return sum; } /** @@ -27,7 +36,23 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + if(words == null) { + throw new NullPointerException(); + } + if(words.size() == 0) { + throw new IllegalArgumentException(); + } + String shortestWord = null; + for(String word : words) { + if(shortestWord == null) { + shortestWord = word; + continue; + } + if(word.length() < shortestWord.length() || (word.length() == shortestWord.length() && word.compareTo(shortestWord) < 0) ) { + shortestWord = word; + } + } + return shortestWord; } /** @@ -40,7 +65,16 @@ 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 eighteenOrOlder = new HashSet<>(); + for(Map.Entry person : ages.entrySet()) { + if(person.getValue() >= 18) { + eighteenOrOlder.add(person.getKey()); + } + } + return eighteenOrOlder; } /** @@ -51,7 +85,18 @@ 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 biggestNum = -Integer.MIN_VALUE; + while(current != null) { + if(current.data > biggestNum) { + biggestNum = current.data; + } + current = current.next; + } + return biggestNum; } /** @@ -68,7 +113,20 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + Map frequencyMap = new HashMap<>(); + if(head == null) { + return frequencyMap; + } + + ListNode current = head; + + while(current != null) { + T value = current.data; + frequencyMap.put(value, frequencyMap.getOrDefault(value, 0)+1); + current = current.next; + } + + return frequencyMap; } @@ -81,7 +139,11 @@ 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; + } + + return 1 + Math.max(levelCount(root.left), levelCount(root.right)); } @@ -109,7 +171,14 @@ 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 || level < 1) { + return 0; + } + if(level == 1) { + return root.data; + } + + return sumAtLevel(root.left, level-1) + sumAtLevel(root.right, level-1); } @@ -124,7 +193,33 @@ 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; + Stack> stk = new Stack<>(); + if(root != null) { + stk.push(root); + } + + int treeSum = 0; + int listSum = 0; + + while(!stk.isEmpty()) { + BinaryTreeNode node = stk.pop(); + treeSum += node.data; + + if(node.left != null) { + stk.push(node.left); + } + if(node.right != null) { + stk.push(node.right); + } + } + + ListNode current = head; + while(current != null) { + listSum += current.data; + current = current.next; + } + + return treeSum == listSum; } /** @@ -136,7 +231,17 @@ 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 +273,20 @@ 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; + if(root.children.size() == 1) { + count++; + } + + for(TreeNode child : root.children) { + count += onlyChildCount(child); + } + + return count; } /** @@ -206,6 +324,28 @@ public static int onlyChildCount(TreeNode root) { * @return the depth of the tree, or 0 if the tree is null or the root is not present in the tree */ public static int maxDepth(Map> tree, T root) { - return 0; + if(tree == null || root == null) return 0; + + //find root and if its actually in the tree + boolean rootInTree = tree.containsKey(root); + if (!rootInTree) { + for (List children : tree.values()) { + if (children.contains(root)) { + rootInTree = true; + break; + } + } + } + if(!rootInTree) return 0; + //check if its a leaf + if(!tree.containsKey(root)) return 1; + + int maxDepth = 0; + for(T child : tree.get(root)) { + int depth = maxDepth(tree, child); + maxDepth = Math.max(depth, maxDepth); + } + + return 1+maxDepth; } } \ No newline at end of file