mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-19 17:54:42 +08:00
docs: update the whole repository
* fix some bugs * delete duplicate files * format code
This commit is contained in:
@ -1,10 +1,11 @@
|
||||
//Trie Data structure implementation without any libraries */
|
||||
package DataStructures.Trees;
|
||||
|
||||
/**
|
||||
* Trie Data structure implementation without any libraries
|
||||
*
|
||||
* @author Dheeraj Kumar Barnwal (https://github.com/dheeraj92)
|
||||
*
|
||||
*/
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class TrieImp {
|
||||
@ -13,34 +14,37 @@ public class TrieImp {
|
||||
TrieNode[] child;
|
||||
boolean end;
|
||||
|
||||
public TrieNode(){
|
||||
public TrieNode() {
|
||||
child = new TrieNode[26];
|
||||
end = false;
|
||||
}
|
||||
}
|
||||
|
||||
private final TrieNode root;
|
||||
public TrieImp(){
|
||||
|
||||
public TrieImp() {
|
||||
root = new TrieNode();
|
||||
}
|
||||
|
||||
public void insert(String word){
|
||||
public void insert(String word) {
|
||||
TrieNode currentNode = root;
|
||||
for(int i=0; i < word.length();i++){
|
||||
TrieNode node = currentNode.child[word.charAt(i)-'a'];
|
||||
if(node == null){
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
TrieNode node = currentNode.child[word.charAt(i) - 'a'];
|
||||
if (node == null) {
|
||||
node = new TrieNode();
|
||||
currentNode.child[word.charAt(i)-'a']=node;
|
||||
currentNode.child[word.charAt(i) - 'a'] = node;
|
||||
}
|
||||
currentNode = node;
|
||||
}
|
||||
currentNode.end = true;
|
||||
}
|
||||
public boolean search(String word){
|
||||
|
||||
public boolean search(String word) {
|
||||
TrieNode currentNode = root;
|
||||
for(int i=0;i<word.length();i++){
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
char ch = word.charAt(i);
|
||||
TrieNode node = currentNode.child[ch-'a'];
|
||||
if(node == null){
|
||||
TrieNode node = currentNode.child[ch - 'a'];
|
||||
if (node == null) {
|
||||
return false;
|
||||
}
|
||||
currentNode = node;
|
||||
@ -48,29 +52,31 @@ public class TrieImp {
|
||||
return currentNode.end;
|
||||
}
|
||||
|
||||
public boolean delete(String word){
|
||||
public boolean delete(String word) {
|
||||
TrieNode currentNode = root;
|
||||
for(int i=0;i<word.length();i++){
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
char ch = word.charAt(i);
|
||||
TrieNode node = currentNode.child[ch-'a'];
|
||||
if(node == null){
|
||||
TrieNode node = currentNode.child[ch - 'a'];
|
||||
if (node == null) {
|
||||
return false;
|
||||
}
|
||||
currentNode = node;
|
||||
}
|
||||
if(currentNode.end == true){
|
||||
if (currentNode.end == true) {
|
||||
currentNode.end = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void sop(String print){
|
||||
public static void sop(String print) {
|
||||
System.out.println(print);
|
||||
}
|
||||
|
||||
//Regex to check if word contains only a-z character
|
||||
public static boolean isValid(String word){
|
||||
/**
|
||||
* Regex to check if word contains only a-z character
|
||||
*/
|
||||
public static boolean isValid(String word) {
|
||||
return word.matches("^[a-z]+$");
|
||||
}
|
||||
|
||||
@ -80,40 +86,40 @@ public class TrieImp {
|
||||
@SuppressWarnings("resource")
|
||||
Scanner scan = new Scanner(System.in);
|
||||
sop("string should contain only a-z character for all operation");
|
||||
while(true){
|
||||
while (true) {
|
||||
sop("1. Insert\n2. Search\n3. Delete\n4. Quit");
|
||||
try{
|
||||
try {
|
||||
int t = scan.nextInt();
|
||||
switch (t) {
|
||||
case 1:
|
||||
word = scan.next();
|
||||
if(isValid(word))
|
||||
if (isValid(word))
|
||||
obj.insert(word);
|
||||
else
|
||||
sop("Invalid string: allowed only a-z");
|
||||
break;
|
||||
case 2:
|
||||
word = scan.next();
|
||||
boolean resS=false;
|
||||
if(isValid(word))
|
||||
boolean resS = false;
|
||||
if (isValid(word))
|
||||
resS = obj.search(word);
|
||||
else
|
||||
sop("Invalid string: allowed only a-z");
|
||||
if(resS)
|
||||
if (resS)
|
||||
sop("word found");
|
||||
else
|
||||
sop("word not found");
|
||||
break;
|
||||
case 3:
|
||||
word = scan.next();
|
||||
boolean resD=false;
|
||||
if(isValid(word))
|
||||
boolean resD = false;
|
||||
if (isValid(word))
|
||||
resD = obj.delete(word);
|
||||
else
|
||||
sop("Invalid string: allowed only a-z");
|
||||
if(resD){
|
||||
if (resD) {
|
||||
sop("word got deleted successfully");
|
||||
}else{
|
||||
} else {
|
||||
sop("word not found");
|
||||
}
|
||||
break;
|
||||
@ -125,7 +131,7 @@ public class TrieImp {
|
||||
sop("Input int from 1-4");
|
||||
break;
|
||||
}
|
||||
}catch(Exception e){
|
||||
} catch (Exception e) {
|
||||
String badInput = scan.next();
|
||||
sop("This is bad input: " + badInput);
|
||||
}
|
||||
|
Reference in New Issue
Block a user