diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/balancedtree/AVLTreeRecursive.java b/src/main/java/com/williamfiset/algorithms/datastructures/balancedtree/AVLTreeRecursive.java index 03b034538..d90acf047 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/balancedtree/AVLTreeRecursive.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/balancedtree/AVLTreeRecursive.java @@ -327,6 +327,7 @@ public T next() { Node node = stack.pop(); + // Optional is recommended to avoid explicit null checks if (node.right != null) { stack.push(node.right); trav = node.right; @@ -354,6 +355,7 @@ public boolean validateBSTInvarient(Node node) { if (node == null) return true; T val = node.value; boolean isValid = true; + // Optional is recommended to avoid explicit null checks if (node.left != null) isValid = isValid && node.left.value.compareTo(val) < 0; if (node.right != null) isValid = isValid && node.right.value.compareTo(val) > 0; return isValid && validateBSTInvarient(node.left) && validateBSTInvarient(node.right); diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/balancedtree/AVLTreeRecursiveOptimized.java b/src/main/java/com/williamfiset/algorithms/datastructures/balancedtree/AVLTreeRecursiveOptimized.java index ddac468c3..11b2f3c41 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/balancedtree/AVLTreeRecursiveOptimized.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/balancedtree/AVLTreeRecursiveOptimized.java @@ -335,6 +335,7 @@ public T next() { Node node = stack.pop(); + // Optional is recommended to avoid explicit null checks if (node.right != null) { stack.push(node.right); trav = node.right; @@ -357,6 +358,7 @@ boolean validateBSTInvarient(Node node) { if (node == null) return true; T val = node.value; boolean isValid = true; + // Optional is recommended to avoid explicit null checks if (node.left != null) isValid = isValid && node.left.value.compareTo(val) < 0; if (node.right != null) isValid = isValid && node.right.value.compareTo(val) > 0; return isValid && validateBSTInvarient(node.left) && validateBSTInvarient(node.right); diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/binarysearchtree/BinarySearchTree.java b/src/main/java/com/williamfiset/algorithms/datastructures/binarysearchtree/BinarySearchTree.java index d48e7f1a6..ae8b99a0b 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/binarysearchtree/BinarySearchTree.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/binarysearchtree/BinarySearchTree.java @@ -237,6 +237,7 @@ public boolean hasNext() { public T next() { if (expectedNodeCount != nodeCount) throw new java.util.ConcurrentModificationException(); Node node = stack.pop(); + // Optional is recommended to avoid explicit null checks if (node.right != null) stack.push(node.right); if (node.left != null) stack.push(node.left); return node.data; @@ -279,6 +280,8 @@ public T next() { Node node = stack.pop(); // Try moving down right once + + // Optional is recommended to avoid explicit null checks if (node.right != null) { stack.push(node.right); trav = node.right; @@ -301,6 +304,7 @@ private java.util.Iterator postOrderTraversal() { final java.util.Stack stack2 = new java.util.Stack<>(); stack1.push(root); while (!stack1.isEmpty()) { + // Optional is recommended to avoid explicit null checks Node node = stack1.pop(); if (node != null) { stack2.push(node); @@ -346,6 +350,7 @@ public boolean hasNext() { public T next() { if (expectedNodeCount != nodeCount) throw new java.util.ConcurrentModificationException(); Node node = queue.poll(); + // Optional is recommended to avoid explicit null checks if (node.left != null) queue.offer(node.left); if (node.right != null) queue.offer(node.right); return node.data; diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/bloomfilter/AnagramSet.java b/src/main/java/com/williamfiset/algorithms/datastructures/bloomfilter/AnagramSet.java index c20007f93..d1b9972aa 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/bloomfilter/AnagramSet.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/bloomfilter/AnagramSet.java @@ -52,6 +52,7 @@ public AnagramSet(int[] mods) { bloomFilter = new BloomFilter(mods); // // Assuming all mods are primes each mod value will have a modular inverse + // It is recommended to use for-each loops instead of traditional for loops. for (int i = 0; i < N_HASHES; i++) { java.math.BigInteger mod = java.math.BigInteger.valueOf(MODS[i]); for (int j = 0; j < PRIMES.length; j++) { diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/bloomfilter/BloomFilter.java b/src/main/java/com/williamfiset/algorithms/datastructures/bloomfilter/BloomFilter.java index fe8c0c6bb..48b16e724 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/bloomfilter/BloomFilter.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/bloomfilter/BloomFilter.java @@ -51,6 +51,7 @@ public void add(long[] hashes) { // Checks if a particular key is found within the bloom filter public boolean contains(long[] hashes) { + // It is recommended to use for-each loops instead of traditional for loops. for (int i = 0; i < hashes.length; i++) { int block = (int) (hashes[i] >> DIV64_SHIFT); long MASK = 1L << (hashes[i] & MOD64_MASK); diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/hashtable/DoubleHashingTestObject.java b/src/main/java/com/williamfiset/algorithms/datastructures/hashtable/DoubleHashingTestObject.java index 59a66ddd7..2a8ad483e 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/hashtable/DoubleHashingTestObject.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/hashtable/DoubleHashingTestObject.java @@ -53,6 +53,7 @@ private void intHash() { } private void vectorHash() { + // It is recommended to use for-each loops instead of traditional for loops. for (int i = 0; i < vectorData.length; i++) hash2 += randomVector[i] * vectorData[i]; } diff --git a/src/test/java/com/williamfiset/algorithms/datastructures/binarysearchtree/BinarySearchTreeTest.java b/src/test/java/com/williamfiset/algorithms/datastructures/binarysearchtree/BinarySearchTreeTest.java index 71705f7e1..2a2caa1c7 100644 --- a/src/test/java/com/williamfiset/algorithms/datastructures/binarysearchtree/BinarySearchTreeTest.java +++ b/src/test/java/com/williamfiset/algorithms/datastructures/binarysearchtree/BinarySearchTreeTest.java @@ -43,6 +43,7 @@ static void preOrder(List lst, TestTreeNode node) { if (node == null) return; lst.add(node.data); + // Optional is recommended to avoid explicit null checks if (node.left != null) preOrder(lst, node.left); if (node.right != null) preOrder(lst, node.right); } @@ -51,6 +52,7 @@ static void inOrder(List lst, TestTreeNode node) { if (node == null) return; + // Optional is recommended to avoid explicit null checks if (node.left != null) inOrder(lst, node.left); lst.add(node.data); if (node.right != null) inOrder(lst, node.right); @@ -60,6 +62,7 @@ static void postOrder(List lst, TestTreeNode node) { if (node == null) return; + // Optional is recommended to avoid explicit null checks if (node.left != null) postOrder(lst, node.left); if (node.right != null) postOrder(lst, node.right); lst.add(node.data); @@ -72,6 +75,7 @@ static void levelOrder(List lst, TestTreeNode node) { while (!q.isEmpty()) { + // Optional is recommended to avoid explicit null checks node = q.poll(); lst.add(node.data); if (node.left != null) q.offer(node.left); diff --git a/src/test/java/com/williamfiset/algorithms/datastructures/hashtable/DoubleHashingTestObject.java b/src/test/java/com/williamfiset/algorithms/datastructures/hashtable/DoubleHashingTestObject.java index a65c6c85d..8b35617df 100644 --- a/src/test/java/com/williamfiset/algorithms/datastructures/hashtable/DoubleHashingTestObject.java +++ b/src/test/java/com/williamfiset/algorithms/datastructures/hashtable/DoubleHashingTestObject.java @@ -53,6 +53,7 @@ private void intHash() { } private void vectorHash() { + // It is recommended to use for-each loops instead of traditional for loops. for (int i = 0; i < vectorData.length; i++) hash2 += randomVector[i] * vectorData[i]; }