Skip to content

Commit a06abb4

Browse files
committed
Added Ternary Search Tree (TST) implementation with tests
1 parent 8a4a210 commit a06abb4

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package main.java.com.thealgorithms.datastructures.trees;
2+
3+
/**
4+
* Ternary Search Tree (TST) implementation.
5+
* Stores strings and supports insertion, search, and prefix checking.
6+
*
7+
* Time Complexity:
8+
* - Insert: O(m), where m = word length
9+
* - Search: O(m)
10+
*/
11+
public class TernarySearchTree {
12+
private Node root;
13+
14+
private static class Node {
15+
char character;
16+
boolean isEndOfWord;
17+
Node left, middle, right;
18+
19+
Node(char character) {
20+
this.character = character;
21+
}
22+
}
23+
24+
/** Insert a word into the TST */
25+
public void insert(String word) {
26+
root = insert(root, word, 0);
27+
}
28+
29+
private Node insert(Node node, String word, int index) {
30+
char ch = word.charAt(index);
31+
if (node == null) {
32+
node = new Node(ch);
33+
}
34+
35+
if (ch < node.character) {
36+
node.left = insert(node.left, word, index);
37+
} else if (ch > node.character) {
38+
node.right = insert(node.right, word, index);
39+
} else {
40+
if (index + 1 < word.length()) {
41+
node.middle = insert(node.middle, word, index + 1);
42+
} else {
43+
node.isEndOfWord = true;
44+
}
45+
}
46+
return node;
47+
}
48+
49+
/** Search if a word exists in TST */
50+
public boolean search(String word) {
51+
return search(root, word, 0);
52+
}
53+
54+
private boolean search(Node node, String word, int index) {
55+
if (node == null) return false;
56+
57+
char ch = word.charAt(index);
58+
if (ch < node.character) {
59+
return search(node.left, word, index);
60+
} else if (ch > node.character) {
61+
return search(node.right, word, index);
62+
} else {
63+
if (index + 1 == word.length()) {
64+
return node.isEndOfWord;
65+
}
66+
return search(node.middle, word, index + 1);
67+
}
68+
}
69+
70+
// Example usage
71+
public static void main(String[] args) {
72+
TernarySearchTree tst = new TernarySearchTree();
73+
tst.insert("cat");
74+
tst.insert("cap");
75+
tst.insert("bat");
76+
77+
System.out.println(tst.search("cat")); // true
78+
System.out.println(tst.search("cap")); // true
79+
System.out.println(tst.search("can")); // false
80+
}
81+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main.java.com.thealgorithms.datastructures.trees;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
/**
7+
* Unit tests for TernarySearchTree.
8+
*/
9+
class TernarySearchTreeTest {
10+
11+
@Test
12+
void testInsertAndSearchWords() {
13+
TernarySearchTree tst = new TernarySearchTree();
14+
15+
tst.insert("cat");
16+
tst.insert("cap");
17+
tst.insert("bat");
18+
19+
assertTrue(tst.search("cat"));
20+
assertTrue(tst.search("cap"));
21+
assertTrue(tst.search("bat"));
22+
23+
assertFalse(tst.search("can"));
24+
assertFalse(tst.search("dog"));
25+
}
26+
27+
@Test
28+
void testSingleCharacterWord() {
29+
TernarySearchTree tst = new TernarySearchTree();
30+
31+
tst.insert("a");
32+
tst.insert("b");
33+
34+
assertTrue(tst.search("a"));
35+
assertTrue(tst.search("b"));
36+
assertFalse(tst.search("c"));
37+
}
38+
39+
@Test
40+
void testPrefixNotWholeWord() {
41+
TernarySearchTree tst = new TernarySearchTree();
42+
43+
tst.insert("hello");
44+
45+
assertFalse(tst.search("he")); // prefix only
46+
assertFalse(tst.search("hell")); // prefix only
47+
assertTrue(tst.search("hello")); // complete word
48+
}
49+
50+
@Test
51+
void testMultipleInsertionsOfSameWord() {
52+
TernarySearchTree tst = new TernarySearchTree();
53+
54+
tst.insert("java");
55+
tst.insert("java");
56+
tst.insert("java");
57+
58+
assertTrue(tst.search("java"));
59+
assertFalse(tst.search("javac"));
60+
}
61+
}

0 commit comments

Comments
 (0)