From 3c9aac6ec0f4ba204a88537df5560f25e310bc00 Mon Sep 17 00:00:00 2001 From: Chiefpatwal Date: Sun, 27 Oct 2024 19:29:38 +0530 Subject: [PATCH 1/7] Add Longest Subarray With Sum Less Than or Equal to K algorithm and tests --- .../LongestSubarrayWithSumLessOrEqualToK.java | 48 +++++++++++++++++++ ...gestSubarrayWithSumLessOrEqualToKTest.java | 22 +++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/main/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToK.java create mode 100644 src/test/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToKTest.java diff --git a/src/main/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToK.java b/src/main/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToK.java new file mode 100644 index 000000000000..55c3f709b467 --- /dev/null +++ b/src/main/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToK.java @@ -0,0 +1,48 @@ +package com.thealgorithms.slidingwindow; + +/** + * The Longest Subarray with Sum Less Than or Equal to k algorithm finds the length + * of the longest subarray whose sum is less than or equal to a given value k. + * + *

+ * Worst-case performance O(n) + * Best-case performance O(n) + * Average performance O(n) + * Worst-case space complexity O(1) + * + * @author https://github.com/Chiefpatwal + */ +public final class LongestSubarrayWithSumLessOrEqualToK { + + // Prevent instantiation + private LongestSubarrayWithSumLessOrEqualToK() { + } + + /** + * This method finds the length of the longest subarray with a sum less than or equal to k. + * + * @param arr is the input array + * @param k is the maximum sum allowed + * @return the length of the longest subarray with sum less than or equal to k + */ + public static int longestSubarrayWithSumLEK(int[] arr, int k) { + int maxLength = 0; // To store the maximum length found + int currentSum = 0; // To store the current sum of the window + int left = 0; // Left index of the sliding window + + for (int right = 0; right < arr.length; right++) { + currentSum += arr[right]; // Expand the window to the right + + // Shrink the window from the left if the current sum exceeds k + while (currentSum > k && left <= right) { + currentSum -= arr[left]; // Remove the leftmost element + left++; // Move the left index to the right + } + + // Update maxLength if the current window is valid + maxLength = Math.max(maxLength, right - left + 1); + } + + return maxLength; // Return the maximum length found + } +} diff --git a/src/test/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToKTest.java b/src/test/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToKTest.java new file mode 100644 index 000000000000..da282ab35ef3 --- /dev/null +++ b/src/test/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToKTest.java @@ -0,0 +1,22 @@ +package com.thealgorithms.slidingwindow; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +/** + * Unit tests for the LongestSubarrayWithSumLessOrEqualToK algorithm. + */ +public class LongestSubarrayWithSumLessOrEqualToKTest { + + /** + * Tests for the longest subarray with a sum less than or equal to k. + */ + @Test + public void testLongestSubarrayWithSumLEK() { + assertEquals(3, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[] {1, 2, 3, 4}, 6)); // {1, 2, 3} + assertEquals(4, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[] {1, 2, 3, 4}, 10)); // {1, 2, 3, 4} + assertEquals(2, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[] {5, 1, 2, 3}, 5)); // {5} + assertEquals(0, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[] {1, 2, 3}, 0)); // No valid subarray + } +} From 9b18ee317e07e26ccee4fe2def7d1a5490e54930 Mon Sep 17 00:00:00 2001 From: Chiefpatwal Date: Mon, 28 Oct 2024 01:19:51 +0530 Subject: [PATCH 2/7] Updated BinaryTree and BinaryTreeTest with encapsulation and test cases --- .../datastructures/trees/BinaryTree.java | 244 +++--------------- .../datastructures/trees/BinaryTreeTest.java | 28 +- 2 files changed, 47 insertions(+), 225 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java b/src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java index cf0de4a92030..cc3f5d418c45 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java @@ -3,117 +3,60 @@ import java.util.LinkedList; import java.util.Queue; -/* - * This entire class is used to build a Binary Tree data structure. There is the - * Node Class and the Tree Class, both explained below. - */ /** - * A binary tree is a data structure in which an element has two - * successors(children). The left child is usually smaller than the parent, and - * the right child is usually bigger. + * A binary tree data structure where elements have two successors (children). + * The left child is smaller than the parent, and the right child is larger. * * @author Unknown */ public class BinaryTree { /** - * This class implements the nodes that will go on the Binary Tree. They - * consist of the data in them, the node to the left, the node to the right, - * and the parent from which they came from. - * - * @author Unknown + * Node class represents elements in the Binary Tree, with pointers to + * the left and right children and a reference to its parent node. */ static class Node { - - /** - * Data for the node - */ public int data; - /** - * The Node to the left of this one - */ - public Node left; - /** - * The Node to the right of this one - */ - public Node right; - /** - * The parent of this node - */ - public Node parent; + public Node left, right, parent; - /** - * Constructor of Node - * - * @param value Value to put in the node - */ - Node(int value) { + public Node(int value) { data = value; - left = null; - right = null; - parent = null; + left = right = parent = null; } } - /** - * The root of the Binary Tree - */ private Node root; - /** - * Constructor - */ public BinaryTree() { root = null; } - /** - * Parameterized Constructor - */ public BinaryTree(Node root) { this.root = root; } - /** - * Method to find a Node with a certain value - * - * @param key Value being looked for - * @return The node if it finds it, otherwise returns the parent - */ public Node find(int key) { Node current = root; while (current != null) { if (key < current.data) { - if (current.left == null) { - return current; // The key isn't exist, returns the parent - } + if (current.left == null) return current; current = current.left; } else if (key > current.data) { - if (current.right == null) { - return current; - } + if (current.right == null) return current; current = current.right; - } else { // If you find the value return it + } else { return current; } } return null; } - /** - * Inserts certain value into the Binary Tree - * - * @param value Value to be inserted - */ public void put(int value) { Node newNode = new Node(value); if (root == null) { root = newNode; } else { - // This will return the soon to be parent of the value you're inserting Node parent = find(value); - - // This if/else assigns the new node to be either the left or right child of the parent if (value < parent.data) { parent.left = newNode; parent.left.parent = parent; @@ -124,141 +67,59 @@ public void put(int value) { } } - /** - * Deletes a given value from the Binary Tree - * - * @param value Value to be deleted - * @return If the value was deleted - */ public boolean remove(int value) { - // temp is the node to be deleted Node temp = find(value); + if (temp == null || temp.data != value) return false; - // If the value doesn't exist - if (temp.data != value) { - return false; - } - - // No children - if (temp.right == null && temp.left == null) { - if (temp == root) { - root = null; - } // This if/else assigns the new node to be either the left or right child of the - // parent - else if (temp.parent.data < temp.data) { - temp.parent.right = null; - } else { - temp.parent.left = null; - } + if (temp.left == null && temp.right == null) { + if (temp == root) root = null; + else if (temp.parent.data < temp.data) temp.parent.right = null; + else temp.parent.left = null; return true; - } // Two children - else if (temp.left != null && temp.right != null) { + } else if (temp.left != null && temp.right != null) { Node successor = findSuccessor(temp); - - // The left tree of temp is made the left tree of the successor successor.left = temp.left; successor.left.parent = successor; - - // If the successor has a right child, the child's grandparent is it's new parent if (successor.parent != temp) { if (successor.right != null) { successor.right.parent = successor.parent; successor.parent.left = successor.right; - } else { - successor.parent.left = null; - } + } else successor.parent.left = null; successor.right = temp.right; successor.right.parent = successor; } - if (temp == root) { - successor.parent = null; root = successor; - } // If you're not deleting the root - else { + successor.parent = null; + } else { successor.parent = temp.parent; - - // This if/else assigns the new node to be either the left or right child of the - // parent - if (temp.parent.data < temp.data) { - temp.parent.right = successor; - } else { - temp.parent.left = successor; - } + if (temp.parent.data < temp.data) temp.parent.right = successor; + else temp.parent.left = successor; } return true; - } // One child - else { - // If it has a right child - if (temp.right != null) { - if (temp == root) { - root = temp.right; - return true; - } - - temp.right.parent = temp.parent; - - // Assigns temp to left or right child - if (temp.data < temp.parent.data) { - temp.parent.left = temp.right; - } else { - temp.parent.right = temp.right; - } - } // If it has a left child + } else { + Node child = (temp.right != null) ? temp.right : temp.left; + if (temp == root) root = child; else { - if (temp == root) { - root = temp.left; - return true; - } - - temp.left.parent = temp.parent; - - // Assigns temp to left or right side - if (temp.data < temp.parent.data) { - temp.parent.left = temp.left; - } else { - temp.parent.right = temp.left; - } + child.parent = temp.parent; + if (temp.data < temp.parent.data) temp.parent.left = child; + else temp.parent.right = child; } return true; } } - /** - * This method finds the Successor to the Node given. Move right once and go - * left down the tree as far as you can - * - * @param n Node that you want to find the Successor of - * @return The Successor of the node - */ public Node findSuccessor(Node n) { - if (n.right == null) { - return n; - } + if (n.right == null) return n; Node current = n.right; - Node parent = n.right; - while (current != null) { - parent = current; - current = current.left; - } - return parent; + while (current.left != null) current = current.left; + return current; } - /** - * Returns the root of the Binary Tree - * - * @return the root of the Binary Tree - */ public Node getRoot() { return root; } - /** - * Prints leftChild - root - rightChild This is the equivalent of a depth - * first search - * - * @param localRoot The local root of the binary tree - */ public void inOrder(Node localRoot) { if (localRoot != null) { inOrder(localRoot.left); @@ -267,11 +128,6 @@ public void inOrder(Node localRoot) { } } - /** - * Prints root - leftChild - rightChild - * - * @param localRoot The local root of the binary tree - */ public void preOrder(Node localRoot) { if (localRoot != null) { System.out.print(localRoot.data + " "); @@ -280,11 +136,6 @@ public void preOrder(Node localRoot) { } } - /** - * Prints leftChild - rightChild - root - * - * @param localRoot The local root of the binary tree - */ public void postOrder(Node localRoot) { if (localRoot != null) { postOrder(localRoot.left); @@ -293,38 +144,15 @@ public void postOrder(Node localRoot) { } } - /** - * Prints the tree in a breadth first search order This is similar to - * pre-order traversal, but instead of being implemented with a stack (or - * recursion), it is implemented with a queue - * - * @param localRoot The local root of the binary tree - */ public void bfs(Node localRoot) { - // Create a queue for the order of the nodes + if (localRoot == null) return; Queue queue = new LinkedList<>(); - - // If the give root is null, then we don't add to the queue - // and won't do anything - if (localRoot != null) { - queue.add(localRoot); - } - - // Continue until the queue is empty + queue.add(localRoot); while (!queue.isEmpty()) { - // Get the next node on the queue to visit - localRoot = queue.remove(); - - // Print the data from the node we are visiting - System.out.print(localRoot.data + " "); - - // Add the children to the queue if not null - if (localRoot.right != null) { - queue.add(localRoot.right); - } - if (localRoot.left != null) { - queue.add(localRoot.left); - } + Node node = queue.remove(); + System.out.print(node.data + " "); + if (node.left != null) queue.add(node.left); + if (node.right != null) queue.add(node.right); } } } diff --git a/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java b/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java index b153c5d667de..22e0ba425949 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java @@ -1,13 +1,12 @@ package com.thealgorithms.datastructures.trees; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import org.junit.jupiter.api.Test; public class BinaryTreeTest { - // checks that adding populating the tree and searching for data - // retrieves the expected data @Test void test1() { BinaryTree t = new BinaryTree(); @@ -17,12 +16,11 @@ void test1() { t.put(9); t.put(12); - assertEquals(t.find(5).data, 5); - assertEquals(t.find(7).data, 7); + assertNotNull(t.find(5)); + assertEquals(5, t.find(5).data); + assertEquals(7, t.find(7).data); } - // checks that removing data from the tree - // properly removes and makes the new root the expected new root @Test void test2() { BinaryTree t = new BinaryTree(); @@ -35,11 +33,10 @@ void test2() { t.remove(5); t.remove(7); - assertEquals(t.getRoot().data, 9); + assertNotNull(t.getRoot(), "Root should not be null after removals"); + assertEquals(9, t.getRoot().data); } - // checks that removing an unexistend node returns false - // as specified by the documentation of the function @Test void test3() { BinaryTree t = new BinaryTree(); @@ -49,13 +46,10 @@ void test3() { t.put(9); t.put(12); - assertEquals(t.remove(9), true); - assertEquals(t.remove(398745987), false); + assertEquals(true, t.remove(9)); + assertEquals(false, t.remove(398745987)); } - // check if the bfs, inOrder, preOrder and postOrder functions - // worg as expected, also increases the coverage measures in - // JaCoCo @Test void test4() { BinaryTree t = new BinaryTree(); @@ -65,12 +59,12 @@ void test4() { t.put(9); t.put(12); - t.bfs(t.find(12)); + t.bfs(t.getRoot()); t.inOrder(t.getRoot()); t.preOrder(t.getRoot()); t.postOrder(t.getRoot()); - assertEquals(t.remove(9), true); - assertEquals(t.remove(398745987), false); + assertEquals(true, t.remove(9)); + assertEquals(false, t.remove(398745987)); } } From fcaa064dd8a2daa31d0c505249af94d32b8f9ea2 Mon Sep 17 00:00:00 2001 From: Chiefpatwal Date: Mon, 28 Oct 2024 01:21:06 +0530 Subject: [PATCH 3/7] updated checks bianry --- .../datastructures/trees/BinaryTree.java | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java b/src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java index cc3f5d418c45..85530cb7a555 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java @@ -72,9 +72,12 @@ public boolean remove(int value) { if (temp == null || temp.data != value) return false; if (temp.left == null && temp.right == null) { - if (temp == root) root = null; - else if (temp.parent.data < temp.data) temp.parent.right = null; - else temp.parent.left = null; + if (temp == root) + root = null; + else if (temp.parent.data < temp.data) + temp.parent.right = null; + else + temp.parent.left = null; return true; } else if (temp.left != null && temp.right != null) { Node successor = findSuccessor(temp); @@ -84,7 +87,8 @@ public boolean remove(int value) { if (successor.right != null) { successor.right.parent = successor.parent; successor.parent.left = successor.right; - } else successor.parent.left = null; + } else + successor.parent.left = null; successor.right = temp.right; successor.right.parent = successor; } @@ -93,17 +97,22 @@ public boolean remove(int value) { successor.parent = null; } else { successor.parent = temp.parent; - if (temp.parent.data < temp.data) temp.parent.right = successor; - else temp.parent.left = successor; + if (temp.parent.data < temp.data) + temp.parent.right = successor; + else + temp.parent.left = successor; } return true; } else { Node child = (temp.right != null) ? temp.right : temp.left; - if (temp == root) root = child; + if (temp == root) + root = child; else { child.parent = temp.parent; - if (temp.data < temp.parent.data) temp.parent.left = child; - else temp.parent.right = child; + if (temp.data < temp.parent.data) + temp.parent.left = child; + else + temp.parent.right = child; } return true; } From 0e15f08ffe3f2b70422b7232c2a353217558dbea Mon Sep 17 00:00:00 2001 From: Chiefpatwal Date: Mon, 28 Oct 2024 01:31:53 +0530 Subject: [PATCH 4/7] again updated --- .../datastructures/trees/BinaryTree.java | 243 +++++++++++++++--- .../datastructures/trees/BinaryTreeTest.java | 26 +- 2 files changed, 217 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java b/src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java index 85530cb7a555..2a895abeaf24 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java @@ -3,60 +3,117 @@ import java.util.LinkedList; import java.util.Queue; +/* + * This entire class is used to build a Binary Tree data structure. There is the + * Node Class and the Tree Class, both explained below. + */ /** - * A binary tree data structure where elements have two successors (children). - * The left child is smaller than the parent, and the right child is larger. + * A binary tree is a data structure in which an element has two + * successors(children). The left child is usually smaller than the parent, and + * the right child is usually bigger. * * @author Unknown */ public class BinaryTree { /** - * Node class represents elements in the Binary Tree, with pointers to - * the left and right children and a reference to its parent node. + * This class implements the nodes that will go on the Binary Tree. They + * consist of the data in them, the node to the left, the node to the right, + * and the parent from which they came from. + * + * @author Unknown */ static class Node { + + /** + * Data for the node + */ public int data; - public Node left, right, parent; + /** + * The Node to the left of this one + */ + public Node left; + /** + * The Node to the right of this one + */ + public Node right; + /** + * The parent of this node + */ + public Node parent; - public Node(int value) { + /** + * Constructor of Node + * + * @param value Value to put in the node + */ + Node(int value) { data = value; - left = right = parent = null; + left = null; + right = null; + parent = null; } } + /** + * The root of the Binary Tree + */ private Node root; + /** + * Constructor + */ public BinaryTree() { root = null; } + /** + * Parameterized Constructor + */ public BinaryTree(Node root) { this.root = root; } + /** + * Method to find a Node with a certain value + * + * @param key Value being looked for + * @return The node if it finds it, otherwise returns the parent + */ public Node find(int key) { Node current = root; while (current != null) { if (key < current.data) { - if (current.left == null) return current; + if (current.left == null) { + return current; // The key isn't exist, returns the parent + } current = current.left; } else if (key > current.data) { - if (current.right == null) return current; + if (current.right == null) { + return current; + } current = current.right; - } else { + } else { // If you find the value return it return current; } } return null; } + /** + * Inserts certain value into the Binary Tree + * + * @param value Value to be inserted + */ public void put(int value) { Node newNode = new Node(value); if (root == null) { root = newNode; } else { + // This will return the soon to be parent of the value you're inserting Node parent = find(value); + + // This if/else assigns the new node to be either the left or right child of the parent if (value < parent.data) { parent.left = newNode; parent.left.parent = parent; @@ -67,68 +124,141 @@ public void put(int value) { } } + /** + * Deletes a given value from the Binary Tree + * + * @param value Value to be deleted + * @return If the value was deleted + */ public boolean remove(int value) { + // temp is the node to be deleted Node temp = find(value); - if (temp == null || temp.data != value) return false; - if (temp.left == null && temp.right == null) { - if (temp == root) + // If the value doesn't exist + if (temp.data != value) { + return false; + } + + // No children + if (temp.right == null && temp.left == null) { + if (temp == root) { root = null; - else if (temp.parent.data < temp.data) + } // This if/else assigns the new node to be either the left or right child of the + // parent + else if (temp.parent.data < temp.data) { temp.parent.right = null; - else + } else { temp.parent.left = null; + } return true; - } else if (temp.left != null && temp.right != null) { + } // Two children + else if (temp.left != null && temp.right != null) { Node successor = findSuccessor(temp); + + // The left tree of temp is made the left tree of the successor successor.left = temp.left; successor.left.parent = successor; + + // If the successor has a right child, the child's grandparent is it's new parent if (successor.parent != temp) { if (successor.right != null) { successor.right.parent = successor.parent; successor.parent.left = successor.right; - } else + } else { successor.parent.left = null; + } successor.right = temp.right; successor.right.parent = successor; } + if (temp == root) { - root = successor; successor.parent = null; - } else { + root = successor; + } // If you're not deleting the root + else { successor.parent = temp.parent; - if (temp.parent.data < temp.data) + + // This if/else assigns the new node to be either the left or right child of the + // parent + if (temp.parent.data < temp.data) { temp.parent.right = successor; - else + } else { temp.parent.left = successor; + } } return true; - } else { - Node child = (temp.right != null) ? temp.right : temp.left; - if (temp == root) - root = child; + } // One child + else { + // If it has a right child + if (temp.right != null) { + if (temp == root) { + root = temp.right; + return true; + } + + temp.right.parent = temp.parent; + + // Assigns temp to left or right child + if (temp.data < temp.parent.data) { + temp.parent.left = temp.right; + } else { + temp.parent.right = temp.right; + } + } // If it has a left child else { - child.parent = temp.parent; - if (temp.data < temp.parent.data) - temp.parent.left = child; - else - temp.parent.right = child; + if (temp == root) { + root = temp.left; + return true; + } + + temp.left.parent = temp.parent; + + // Assigns temp to left or right side + if (temp.data < temp.parent.data) { + temp.parent.left = temp.left; + } else { + temp.parent.right = temp.left; + } } return true; } } + /** + * This method finds the Successor to the Node given. Move right once and go + * left down the tree as far as you can + * + * @param n Node that you want to find the Successor of + * @return The Successor of the node + */ public Node findSuccessor(Node n) { - if (n.right == null) return n; + if (n.right == null) { + return n; + } Node current = n.right; - while (current.left != null) current = current.left; - return current; + Node parent = n.right; + while (current != null) { + parent = current; + current = current.left; + } + return parent; } + /** + * Returns the root of the Binary Tree + * + * @return the root of the Binary Tree + */ public Node getRoot() { return root; } + /** + * Prints leftChild - root - rightChild This is the equivalent of a depth + * first search + * + * @param localRoot The local root of the binary tree + */ public void inOrder(Node localRoot) { if (localRoot != null) { inOrder(localRoot.left); @@ -137,6 +267,11 @@ public void inOrder(Node localRoot) { } } + /** + * Prints root - leftChild - rightChild + * + * @param localRoot The local root of the binary tree + */ public void preOrder(Node localRoot) { if (localRoot != null) { System.out.print(localRoot.data + " "); @@ -145,6 +280,11 @@ public void preOrder(Node localRoot) { } } + /** + * Prints leftChild - rightChild - root + * + * @param localRoot The local root of the binary tree + */ public void postOrder(Node localRoot) { if (localRoot != null) { postOrder(localRoot.left); @@ -153,15 +293,38 @@ public void postOrder(Node localRoot) { } } + /** + * Prints the tree in a breadth first search order This is similar to + * pre-order traversal, but instead of being implemented with a stack (or + * recursion), it is implemented with a queue + * + * @param localRoot The local root of the binary tree + */ public void bfs(Node localRoot) { - if (localRoot == null) return; + // Create a queue for the order of the nodes Queue queue = new LinkedList<>(); - queue.add(localRoot); + + // If the give root is null, then we don't add to the queue + // and won't do anything + if (localRoot != null) { + queue.add(localRoot); + } + + // Continue until the queue is empty while (!queue.isEmpty()) { - Node node = queue.remove(); - System.out.print(node.data + " "); - if (node.left != null) queue.add(node.left); - if (node.right != null) queue.add(node.right); + // Get the next node on the queue to visit + localRoot = queue.remove(); + + // Print the data from the node we are visiting + System.out.print(localRoot.data + " "); + + // Add the children to the queue if not null + if (localRoot.right != null) { + queue.add(localRoot.right); + } + if (localRoot.left != null) { + queue.add(localRoot.left); + } } } -} +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java b/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java index 22e0ba425949..039d557e5c15 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java @@ -8,7 +8,7 @@ public class BinaryTreeTest { @Test - void test1() { + void testInsertAndFind() { BinaryTree t = new BinaryTree(); t.put(3); t.put(5); @@ -16,13 +16,13 @@ void test1() { t.put(9); t.put(12); - assertNotNull(t.find(5)); - assertEquals(5, t.find(5).data); - assertEquals(7, t.find(7).data); + assertNotNull(t.find(5), "Node with value 5 should exist"); + assertEquals(5, t.find(5).data, "Value of the found node should be 5"); + assertEquals(7, t.find(7).data, "Value of the found node should be 7"); } @Test - void test2() { + void testRemove() { BinaryTree t = new BinaryTree(); t.put(3); t.put(5); @@ -34,11 +34,11 @@ void test2() { t.remove(7); assertNotNull(t.getRoot(), "Root should not be null after removals"); - assertEquals(9, t.getRoot().data); + assertEquals(9, t.getRoot().data, "Root value should be 9 after removals"); } @Test - void test3() { + void testRemoveReturnValue() { BinaryTree t = new BinaryTree(); t.put(3); t.put(5); @@ -46,12 +46,12 @@ void test3() { t.put(9); t.put(12); - assertEquals(true, t.remove(9)); - assertEquals(false, t.remove(398745987)); + assertEquals(true, t.remove(9), "Removing existing node 9 should return true"); + assertEquals(false, t.remove(398745987), "Removing non-existing node should return false"); } @Test - void test4() { + void testTraversalMethods() { BinaryTree t = new BinaryTree(); t.put(3); t.put(5); @@ -59,12 +59,14 @@ void test4() { t.put(9); t.put(12); + // Testing traversal methods t.bfs(t.getRoot()); t.inOrder(t.getRoot()); t.preOrder(t.getRoot()); t.postOrder(t.getRoot()); - assertEquals(true, t.remove(9)); - assertEquals(false, t.remove(398745987)); + // Ensure removal functionality is still working + assertEquals(true, t.remove(9), "Removing existing node 9 should return true"); + assertEquals(false, t.remove(398745987), "Removing non-existing node should return false"); } } From e403e1f115c83631c5021e5a809e6b2ceee5f71b Mon Sep 17 00:00:00 2001 From: Chiefpatwal Date: Mon, 28 Oct 2024 01:33:43 +0530 Subject: [PATCH 5/7] tired --- .../java/com/thealgorithms/datastructures/trees/BinaryTree.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java b/src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java index 2a895abeaf24..ff02fe38970b 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java @@ -327,4 +327,4 @@ public void bfs(Node localRoot) { } } } -} \ No newline at end of file +} From 29aeb482cfa74616eb5083d1e7c2fcf828b87eaf Mon Sep 17 00:00:00 2001 From: Chiefpatwal Date: Mon, 28 Oct 2024 01:53:51 +0530 Subject: [PATCH 6/7] one more time --- .../datastructures/trees/BinaryTreeTest.java | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java b/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java index 039d557e5c15..bd62cd0dbf88 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java @@ -1,14 +1,16 @@ package com.thealgorithms.datastructures.trees; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; +/** + * Unit tests for the BinaryTree class. + */ public class BinaryTreeTest { @Test - void testInsertAndFind() { + public void testInsertAndFind() { BinaryTree t = new BinaryTree(); t.put(3); t.put(5); @@ -22,7 +24,7 @@ void testInsertAndFind() { } @Test - void testRemove() { + public void testRemove() { BinaryTree t = new BinaryTree(); t.put(3); t.put(5); @@ -34,11 +36,17 @@ void testRemove() { t.remove(7); assertNotNull(t.getRoot(), "Root should not be null after removals"); - assertEquals(9, t.getRoot().data, "Root value should be 9 after removals"); + + // Check if root is not null before accessing its data + if (t.getRoot() != null) { + assertEquals(9, t.getRoot().data, "Root value should be 9 after removals"); + } else { + fail("Root should not be null after removals, but it is."); + } } @Test - void testRemoveReturnValue() { + public void testRemoveReturnValue() { BinaryTree t = new BinaryTree(); t.put(3); t.put(5); @@ -46,12 +54,12 @@ void testRemoveReturnValue() { t.put(9); t.put(12); - assertEquals(true, t.remove(9), "Removing existing node 9 should return true"); - assertEquals(false, t.remove(398745987), "Removing non-existing node should return false"); + assertTrue(t.remove(9), "Removing existing node 9 should return true"); + assertFalse(t.remove(398745987), "Removing non-existing node should return false"); } @Test - void testTraversalMethods() { + public void testTraversalMethods() { BinaryTree t = new BinaryTree(); t.put(3); t.put(5); @@ -66,7 +74,10 @@ void testTraversalMethods() { t.postOrder(t.getRoot()); // Ensure removal functionality is still working - assertEquals(true, t.remove(9), "Removing existing node 9 should return true"); - assertEquals(false, t.remove(398745987), "Removing non-existing node should return false"); + assertTrue(t.remove(9), "Removing existing node 9 should return true"); + assertFalse(t.remove(398745987), "Removing non-existing node should return false"); + + // Check that the root is not null + assertNotNull(t.getRoot(), "Root should not be null after operations"); } } From 47f3c31c2a5e9c081871f17e7643ba194bbdfe32 Mon Sep 17 00:00:00 2001 From: Chiefpatwal Date: Mon, 28 Oct 2024 01:59:35 +0530 Subject: [PATCH 7/7] updated --- .../datastructures/trees/BinaryTreeTest.java | 93 +++++++++---------- 1 file changed, 44 insertions(+), 49 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java b/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java index bd62cd0dbf88..d6581fb8c4e8 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java @@ -1,7 +1,6 @@ package com.thealgorithms.datastructures.trees; -import static org.junit.jupiter.api.Assertions.*; - +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; /** @@ -11,73 +10,69 @@ public class BinaryTreeTest { @Test public void testInsertAndFind() { - BinaryTree t = new BinaryTree(); - t.put(3); - t.put(5); - t.put(7); - t.put(9); - t.put(12); + BinaryTree tree = new BinaryTree(); + tree.put(3); + tree.put(5); + tree.put(7); + tree.put(9); + tree.put(12); - assertNotNull(t.find(5), "Node with value 5 should exist"); - assertEquals(5, t.find(5).data, "Value of the found node should be 5"); - assertEquals(7, t.find(7).data, "Value of the found node should be 7"); + Assertions.assertNotNull(tree.find(5), "Node with value 5 should exist"); + Assertions.assertEquals(5, tree.find(5).data, "Value of the found node should be 5"); + Assertions.assertEquals(7, tree.find(7).data, "Value of the found node should be 7"); } @Test public void testRemove() { - BinaryTree t = new BinaryTree(); - t.put(3); - t.put(5); - t.put(7); - t.put(9); - t.put(12); - t.remove(3); - t.remove(5); - t.remove(7); - - assertNotNull(t.getRoot(), "Root should not be null after removals"); + BinaryTree tree = new BinaryTree(); + tree.put(3); + tree.put(5); + tree.put(7); + tree.put(9); + tree.put(12); + tree.remove(3); + tree.remove(5); + tree.remove(7); - // Check if root is not null before accessing its data - if (t.getRoot() != null) { - assertEquals(9, t.getRoot().data, "Root value should be 9 after removals"); + Assertions.assertNotNull(tree.getRoot(), "Root should not be null after removals"); + if (tree.getRoot() != null) { + Assertions.assertEquals(9, tree.getRoot().data, "Root value should be 9 after removals"); } else { - fail("Root should not be null after removals, but it is."); + Assertions.fail("Root should not be null after removals, but it is."); } } @Test public void testRemoveReturnValue() { - BinaryTree t = new BinaryTree(); - t.put(3); - t.put(5); - t.put(7); - t.put(9); - t.put(12); + BinaryTree tree = new BinaryTree(); + tree.put(3); + tree.put(5); + tree.put(7); + tree.put(9); + tree.put(12); - assertTrue(t.remove(9), "Removing existing node 9 should return true"); - assertFalse(t.remove(398745987), "Removing non-existing node should return false"); + Assertions.assertTrue(tree.remove(9), "Removing existing node 9 should return true"); + Assertions.assertFalse(tree.remove(398745987), "Removing non-existing node should return false"); } @Test public void testTraversalMethods() { - BinaryTree t = new BinaryTree(); - t.put(3); - t.put(5); - t.put(7); - t.put(9); - t.put(12); + BinaryTree tree = new BinaryTree(); + tree.put(3); + tree.put(5); + tree.put(7); + tree.put(9); + tree.put(12); // Testing traversal methods - t.bfs(t.getRoot()); - t.inOrder(t.getRoot()); - t.preOrder(t.getRoot()); - t.postOrder(t.getRoot()); + tree.bfs(tree.getRoot()); + tree.inOrder(tree.getRoot()); + tree.preOrder(tree.getRoot()); + tree.postOrder(tree.getRoot()); - // Ensure removal functionality is still working - assertTrue(t.remove(9), "Removing existing node 9 should return true"); - assertFalse(t.remove(398745987), "Removing non-existing node should return false"); + Assertions.assertTrue(tree.remove(9), "Removing existing node 9 should return true"); + Assertions.assertFalse(tree.remove(398745987), "Removing non-existing node should return false"); - // Check that the root is not null - assertNotNull(t.getRoot(), "Root should not be null after operations"); + Assertions.assertNotNull(tree.getRoot(), "Root should not be null after operations"); } }