Skip to content

Commit 7a3e190

Browse files
add implementation for TST
1 parent a8e963e commit 7a3e190

File tree

3 files changed

+119
-7
lines changed

3 files changed

+119
-7
lines changed

HolczerUdemyCourses/src/tree/prefix/Trie.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ public Trie() {
1414
this.root = new Node("");
1515
}
1616

17-
public void insert(String word, int value) {
17+
public void insert(String key, int value) {
1818
Node temp = root;
1919

20-
for (int i = 0; i < word.length(); i++) {
21-
char c = word.charAt(i);
20+
for (int i = 0; i < key.length(); i++) {
21+
char c = key.charAt(i);
2222
int index = c - 'a';
2323
if (temp.getChildren()[index] == null) {
2424
Node node = new Node(valueOf(c));
@@ -32,12 +32,12 @@ public void insert(String word, int value) {
3232
temp.setLeaf(true);
3333
}
3434

35-
// O(m) running time complexity - where m is the length of the word
36-
public boolean search(String word) {
35+
// O(m) running time complexity - where m is the length of the key
36+
public boolean search(String key) {
3737
Node temp = root;
3838

39-
for (int i = 0; i < word.length(); i++) {
40-
char c = word.charAt(i);
39+
for (int i = 0; i < key.length(); i++) {
40+
char c = key.charAt(i);
4141
int index = c - 'a';
4242
if (temp.getChildren()[index] == null) {
4343
return false;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package tree.ternarysearch;
2+
3+
public class Node {
4+
private char c;
5+
private Node left;
6+
private Node middle;
7+
private Node right;
8+
private int value;
9+
10+
public Node(char c) {
11+
this.c = c;
12+
}
13+
14+
public char getCharacter() {
15+
return c;
16+
}
17+
18+
public void setCharacter(char c) {
19+
this.c = c;
20+
}
21+
22+
public Node getLeft() {
23+
return left;
24+
}
25+
26+
public void setLeft(Node left) {
27+
this.left = left;
28+
}
29+
30+
public Node getMiddle() {
31+
return middle;
32+
}
33+
34+
public void setMiddle(Node middle) {
35+
this.middle = middle;
36+
}
37+
38+
public Node getRight() {
39+
return right;
40+
}
41+
42+
public void setRight(Node right) {
43+
this.right = right;
44+
}
45+
46+
public int getValue() {
47+
return value;
48+
}
49+
50+
public void setValue(int value) {
51+
this.value = value;
52+
}
53+
54+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package tree.ternarysearch;
2+
3+
public class TST {
4+
private Node root;
5+
6+
public void put(String key, int value) {
7+
root = put(root, key, value, 0);
8+
}
9+
10+
public Integer get(String key) {
11+
Node node = get(root, key, 0);
12+
13+
if (node == null) {
14+
return null;
15+
}
16+
17+
return node.getValue();
18+
}
19+
20+
private Node put(Node node, String key, int value, int index) {
21+
char c = key.charAt(index);
22+
23+
if (node == null) {
24+
node = new Node(c);
25+
}
26+
if (c < node.getCharacter()) {
27+
node.setLeft(put(node.getLeft(), key, value, index));
28+
} else if (c > node.getCharacter()) {
29+
node.setRight(put(node.getRight(), key, value, index));
30+
} else if (index < key.length() - 1){
31+
node.setMiddle(put(node.getMiddle(), key, value, index + 1));
32+
} else {
33+
node.setValue(value);
34+
}
35+
36+
return node;
37+
}
38+
39+
// running time complexity is sub-linear in case of search misses
40+
private Node get(Node node, String key, int index) {
41+
if (node == null) {
42+
return null;
43+
}
44+
45+
char c = key.charAt(index);
46+
47+
if (c < node.getCharacter()) {
48+
return get(node.getLeft(), key, index);
49+
} else if (c > node.getCharacter()) {
50+
return get(node.getRight(), key, index);
51+
} else if (index < key.length() - 1) {
52+
return get(node.getMiddle(), key, index + 1);
53+
} else {
54+
return node;
55+
}
56+
}
57+
58+
}

0 commit comments

Comments
 (0)