-
Notifications
You must be signed in to change notification settings - Fork 21.1k
Expand file tree
/
Copy pathTernarySearchTree.java
More file actions
191 lines (164 loc) · 5.42 KB
/
TernarySearchTree.java
File metadata and controls
191 lines (164 loc) · 5.42 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.thealgorithms.datastructures.trees;
/**
* Ternary Search Tree implementation for efficient string storage and retrieval.
*
* A Ternary Search Tree (TST) is a data structure that combines the time efficiency
* of digital tries with the space efficiency of binary search trees.
*
* Time Complexity:
* - Insert: O(log n) average, O(n) worst case
* - Search: O(log n) average, O(n) worst case
* - Delete: O(log n) average, O(n) worst case
*
* Space Complexity: O(n) where n is the number of characters
*
* @see <a href="https://en.wikipedia.org/wiki/Ternary_search_tree">Ternary Search Tree</a>
* @author JeevanYewale
*/
public final class TernarySearchTree {
private Node root;
private static class Node {
char data;
boolean isEnd;
Node left, middle, right;
Node(char data) {
this.data = data;
this.isEnd = false;
}
}
/**
* Inserts a word into the ternary search tree.
*
* @param word the word to insert
* @throws IllegalArgumentException if word is null or empty
*/
public void insert(String word) {
if (word == null || word.isEmpty()) {
throw new IllegalArgumentException("Word cannot be null or empty");
}
root = insert(root, word, 0);
}
private Node insert(Node node, String word, int index) {
char c = word.charAt(index);
if (node == null) {
node = new Node(c);
}
if (c < node.data) {
node.left = insert(node.left, word, index);
} else if (c > node.data) {
node.right = insert(node.right, word, index);
} else {
if (index < word.length() - 1) {
node.middle = insert(node.middle, word, index + 1);
} else {
node.isEnd = true;
}
}
return node;
}
/**
* Searches for a word in the ternary search tree.
*
* @param word the word to search for
* @return true if the word exists, false otherwise
* @throws IllegalArgumentException if word is null or empty
*/
public boolean search(String word) {
if (word == null || word.isEmpty()) {
throw new IllegalArgumentException("Word cannot be null or empty");
}
return search(root, word, 0);
}
private boolean search(Node node, String word, int index) {
if (node == null) {
return false;
}
char c = word.charAt(index);
if (c < node.data) {
return search(node.left, word, index);
} else if (c > node.data) {
return search(node.right, word, index);
} else {
if (index == word.length() - 1) {
return node.isEnd;
}
return search(node.middle, word, index + 1);
}
}
/**
* Checks if any word in the tree starts with the given prefix.
*
* @param prefix the prefix to search for
* @return true if any word starts with the prefix, false otherwise
* @throws IllegalArgumentException if prefix is null or empty
*/
public boolean startsWith(String prefix) {
if (prefix == null || prefix.isEmpty()) {
throw new IllegalArgumentException("Prefix cannot be null or empty");
}
return startsWith(root, prefix, 0);
}
private boolean startsWith(Node node, String prefix, int index) {
if (node == null) {
return false;
}
if (index == prefix.length()) {
return true;
}
char c = prefix.charAt(index);
if (c < node.data) {
return startsWith(node.left, prefix, index);
} else if (c > node.data) {
return startsWith(node.right, prefix, index);
} else {
return startsWith(node.middle, prefix, index + 1);
}
}
/**
* Deletes a word from the ternary search tree.
*
* @param word the word to delete
* @return true if the word was deleted, false if it didn't exist
* @throws IllegalArgumentException if word is null or empty
*/
public boolean delete(String word) {
if (word == null || word.isEmpty()) {
throw new IllegalArgumentException("Word cannot be null or empty");
}
if (!search(word)) {
return false;
}
root = delete(root, word, 0);
return true;
}
private Node delete(Node node, String word, int index) {
if (node == null) {
return null;
}
char c = word.charAt(index);
if (c < node.data) {
node.left = delete(node.left, word, index);
} else if (c > node.data) {
node.right = delete(node.right, word, index);
} else {
if (index == word.length() - 1) {
node.isEnd = false;
} else {
node.middle = delete(node.middle, word, index + 1);
}
}
// Remove node if it's not needed
if (!node.isEnd && node.left == null && node.middle == null && node.right == null) {
return null;
}
return node;
}
/**
* Checks if the tree is empty.
*
* @return true if the tree is empty, false otherwise
*/
public boolean isEmpty() {
return root == null;
}
}