-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTernarySearchTree.java
More file actions
133 lines (110 loc) · 3.03 KB
/
TernarySearchTree.java
File metadata and controls
133 lines (110 loc) · 3.03 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
package gfg.ds.advanced.trie;
import java.util.HashSet;
import java.util.Set;
/** @noinspection WeakerAccess */
public class TernarySearchTree {
private TernaryTreeNode root;
/**
* t=O(M); M is the key length The comparisons to find the next position for a character in string
* will be constant and bounded by the ALPHABET_SIZE
*/
public TernarySearchTree insert(String key) {
TernaryTreeNode prev = null;
char prevCh = '0';
TernaryTreeNode curr = root;
int length = key.length();
for (int i = 0; i < length; ) {
char ch = key.charAt(i);
boolean isEndOfWord = i == length - 1;
if (curr == null) {
TernaryTreeNode newNode = new TernaryTreeNode(ch, isEndOfWord);
if (prev == null) {
curr = root = newNode;
} else if (prevCh == prev.data) {
curr = prev.equal = newNode;
} else if (prevCh < prev.data) {
curr = prev.left = newNode;
} else {
curr = prev.right = newNode;
}
}
if (ch == curr.data) {
if (isEndOfWord) {
curr.isEndOfWord = true;
}
prev = curr;
curr = curr.equal;
i++;
} else if (ch < curr.data) {
prev = curr;
curr = curr.left;
} else {
prev = curr;
curr = curr.right;
}
prevCh = ch;
}
return this;
}
public Set<String> keys() {
Set<String> keys = new HashSet<>();
keysUtil(root, new StringBuilder(), keys);
return keys;
}
private void keysUtil(TernaryTreeNode root, StringBuilder partialKey, Set<String> keys) {
if (root == null) {
return;
}
keysUtil(root.left, new StringBuilder(), keys);
partialKey.append(root.data);
if (root.isEndOfWord) {
keys.add(partialKey.toString());
}
keysUtil(root.equal, partialKey, keys);
keysUtil(root.right, new StringBuilder(), keys);
}
/**
* t=O(M); M is the key length. The comparisons to find the next position for a character in
* string will be constant and bounded by the ALPHABET_SIZE
*/
public boolean search(String key) {
if (isEmpty() || key.isEmpty()) {
return false;
}
TernaryTreeNode prev = null;
TernaryTreeNode curr = root;
int length = key.length();
for (int i = 0; i < length; ) {
if (curr == null) {
return false;
}
char ch = key.charAt(i);
if (ch < curr.data) {
prev = curr;
curr = curr.left;
} else if (ch > curr.data) {
prev = curr;
curr = curr.right;
} else {
prev = curr;
curr = curr.equal;
i++;
}
}
return prev.isEndOfWord;
}
public boolean isEmpty() {
return root == null;
}
public static class TernaryTreeNode {
private TernaryTreeNode left;
private TernaryTreeNode right;
private TernaryTreeNode equal;
private boolean isEndOfWord;
private char data;
public TernaryTreeNode(char data, boolean isEndOfWord) {
this.data = data;
this.isEndOfWord = isEndOfWord;
}
}
}