forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKthSmallestElementBSTTest.java
More file actions
56 lines (47 loc) · 1.56 KB
/
KthSmallestElementBSTTest.java
File metadata and controls
56 lines (47 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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));
}
}