Skip to content

Commit 5abba1b

Browse files
committed
Trie Update
Trie Update
1 parent 55d3082 commit 5abba1b

File tree

6 files changed

+102
-2
lines changed

6 files changed

+102
-2
lines changed
2.22 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*Copyright (c) Dec 22, 2014 CareerMonk Publications and others.
2+
* E-Mail : info@careermonk.com
3+
* Creation Date : 2015-01-10 06:15:46
4+
* Last modification : 2006-05-31
5+
by : Narasimha Karumanchi
6+
* File Name : Trie.java
7+
* Book Title : Data Structures And Algorithms Made In Java
8+
* Warranty : This software is provided "as is" without any
9+
* warranty; without even the implied warranty of
10+
* merchantability or fitness for a particular purpose.
11+
*
12+
*/
13+
14+
15+
package chapter15stringalgorithms;
16+
17+
public class Trie {
18+
19+
private static final int ALPHABETS = 26;
20+
private Trie[] links;
21+
private boolean isEndOfString;
22+
23+
public Trie() {
24+
links = new Trie[ALPHABETS];
25+
isEndOfString = false;
26+
}
27+
28+
public void addString(String s) {
29+
Trie t = this;
30+
int k;
31+
int limit = s.length();
32+
for (k = 0; k < limit; k++) {
33+
int index = s.charAt(k) - 'a';
34+
if (t.links[index] == null)
35+
t.links[index] = new Trie();
36+
t = t.links[index];
37+
}
38+
t.isEndOfString = true;
39+
}
40+
41+
void print(String s, Trie t) {
42+
if (t != null) {
43+
if (t.isEndOfString)
44+
System.out.println(s);
45+
int k;
46+
for (k = 0; k < ALPHABETS; k++)
47+
if (t.links[k] != null)
48+
print(s + (char) (k + 'a'), t.links[k]);
49+
}
50+
}
51+
52+
public boolean isEndOfString(String s) {
53+
Trie t = this;
54+
int k;
55+
int limit = s.length();
56+
for (k = 0; k < limit; k++) {
57+
int index = s.charAt(k) - 'a';
58+
if (t.links[index] == null)
59+
return false;
60+
t = t.links[index];
61+
}
62+
return t.isEndOfString;
63+
}
64+
65+
public boolean isEndOfString() {
66+
return isEndOfString;
67+
}
68+
69+
Trie childAt(char ch) {
70+
return links[ch - 'a'];
71+
}
72+
73+
public static void main(String[] args) {
74+
Trie t = new Trie();
75+
t.addString("car"); t.addString("care"); t.addString("caree");
76+
t.addString("career"); t.addString("careerm"); t.addString("careermonk");
77+
System.out.println("Is careermo included? " + t.isEndOfString("careermo"));
78+
System.out.println("Is career included? " + t.isEndOfString("career"));
79+
}
80+
81+
}
82+

src/chapter3linkedlists/RemoveDuplicatesBruteForce.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package chapter3linkedlists;
1616

1717
public class RemoveDuplicatesBruteForce {
18-
public static void removeDuplicates2(ListNode head) {
18+
public static void removeDuplicates(ListNode head) {
1919
ListNode curr = head;
2020
if(curr == null || curr.getNext() == null) {
2121
return; // 0 or 1 nodes in the list so no duplicates

src/chapter3linkedlists/RemoveDuplicatesHashing.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@
1414

1515
package chapter3linkedlists;
1616

17-
public class RemoveDuplicatesHashing {
17+
import java.util.HashMap;
18+
import java.util.Map;
1819

20+
public class RemoveDuplicatesHashing {
21+
// using a temporary buffer O(n)
22+
public static void removeDuplicates(ListNode head) {
23+
Map<Integer, Boolean> mapper = new HashMap<Integer, Boolean>();
24+
ListNode curr = head;
25+
ListNode next;
26+
while (curr.getNext() != null) {
27+
next = curr.getNext();
28+
if(mapper.get(next.getData()) != null) {
29+
// already seen it, so delete
30+
curr.setNext(next.getNext());
31+
} else {
32+
mapper.put(next.getData(), true);
33+
curr = curr.getNext();
34+
}
35+
}
36+
}
1937
}

0 commit comments

Comments
 (0)