diff --git a/Area.java b/Area.java new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/first-contributions b/first-contributions new file mode 160000 index 000000000000..d58c69ca9ad0 --- /dev/null +++ b/first-contributions @@ -0,0 +1 @@ +Subproject commit d58c69ca9ad0641a155f3a6c95efb484dc0a5abe diff --git a/src/main/java/com/thealgorithms/datastructures/trees/KthSmallestElementInBST.java b/src/main/java/com/thealgorithms/datastructures/trees/KthSmallestElementInBST.java new file mode 100644 index 000000000000..d7aba6660be1 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/trees/KthSmallestElementInBST.java @@ -0,0 +1,41 @@ +package com.thealgorithms.datastructures.trees; + +/** + * Finds the kth smallest element in a Binary Search Tree. + * + * Time Complexity: O(n) + * Space Complexity: O(h) + */ +public final class KthSmallestElementInBST { + + private KthSmallestElementInBST() { + } + + public static int kthSmallest(BinaryTree.Node root, int k) { + Counter counter = new Counter(); + BinaryTree.Node result = inorder(root, k, counter); + return result != null ? result.data : -1; + } + + private static BinaryTree.Node inorder(BinaryTree.Node node, int k, Counter counter) { + if (node == null) { + return null; + } + + BinaryTree.Node left = inorder(node.left, k, counter); + if (left != null) { + return left; + } + + counter.count++; + if (counter.count == k) { + return node; + } + + return inorder(node.right, k, counter); + } + + private static class Counter { + int count = 0; + } +} diff --git a/src/test/java/com/thealgorithms/datastructures/trees/KthSmallestElementBSTTest.java b/src/test/java/com/thealgorithms/datastructures/trees/KthSmallestElementBSTTest.java new file mode 100644 index 000000000000..814b9f7bccc6 --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/trees/KthSmallestElementBSTTest.java @@ -0,0 +1,56 @@ +package com.thealgorithms.datastructures.trees; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + +class KthSmallestElementInBSTTest { + + private BinaryTree.Node createSampleTree() { + /* + 5 + / \ + 3 7 + / \ / \ + 2 4 6 8 + */ + BinaryTree.Node root = new BinaryTree.Node(5); + root.left = new BinaryTree.Node(3); + root.right = new BinaryTree.Node(7); + root.left.left = new BinaryTree.Node(2); + root.left.right = new BinaryTree.Node(4); + root.right.left = new BinaryTree.Node(6); + root.right.right = new BinaryTree.Node(8); + + return root; + } + + @Test + void testSmallestElement() { + BinaryTree.Node root = createSampleTree(); + assertEquals(2, KthSmallestElementInBST.kthSmallest(root, 1)); + } + + @Test + void testRootElement() { + BinaryTree.Node root = createSampleTree(); + assertEquals(5, KthSmallestElementInBST.kthSmallest(root, 4)); + } + + @Test + void testRightSubtreeElement() { + BinaryTree.Node root = createSampleTree(); + assertEquals(8, KthSmallestElementInBST.kthSmallest(root, 7)); + } + + @Test + void testSingleNodeTree() { + BinaryTree.Node root = new BinaryTree.Node(10); + assertEquals(10, KthSmallestElementInBST.kthSmallest(root, 1)); + } + + @Test + void testInvalidK() { + BinaryTree.Node root = createSampleTree(); + assertEquals(-1, KthSmallestElementInBST.kthSmallest(root, 10)); + } +}