Skip to content

Trie Data structure #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Trie Data structure
Trie Data structure implementation without any libraries
  • Loading branch information
dheerajbarnwal authored Oct 3, 2017
commit 077b76336ec8e9dc7a6a7e4ea0905f300a7512cb
135 changes: 135 additions & 0 deletions data_structures/Trees/TrieImp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
//Trie Data structure implementation without any libraries */

/**
*
* @author Dheeraj Kumar Barnwal (https://github.com/dheeraj92)
*
*/
import java.util.Scanner;

public class TrieImp {

public class TrieNode {
TrieNode[] child;
boolean end;

public TrieNode(){
child = new TrieNode[26];
end = false;
}
}
private final TrieNode root;
public TrieImp(){
root = new TrieNode();
}

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){
node = new TrieNode();
currentNode.child[word.charAt(i)-'a']=node;
}
currentNode = node;
}
currentNode.end = true;
}
public boolean search(String word){
TrieNode currentNode = root;
for(int i=0;i<word.length();i++){
char ch = word.charAt(i);
TrieNode node = currentNode.child[ch-'a'];
if(node == null){
return false;
}
currentNode = node;
}
return currentNode.end;
}

public boolean delete(String word){
TrieNode currentNode = root;
for(int i=0;i<word.length();i++){
char ch = word.charAt(i);
TrieNode node = currentNode.child[ch-'a'];
if(node == null){
return false;
}
currentNode = node;
}
if(currentNode.end == true){
currentNode.end = false;
return true;
}
return false;
}

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){
return word.matches("^[a-z]+$");
}

public static void main(String[] args) {
TrieImp obj = new TrieImp();
String word;
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
sop("string should contain only a-z character for all operation");
while(true){
sop("1. Insert\n2. Search\n3. Delete\n4. Quit");
try{
int t = scan.nextInt();
switch (t) {
case 1:
word = scan.next();
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))
resS = obj.search(word);
else
sop("Invalid string: allowed only a-z");
if(resS)
sop("word found");
else
sop("word not found");
break;
case 3:
word = scan.next();
boolean resD=false;
if(isValid(word))
resD = obj.delete(word);
else
sop("Invalid string: allowed only a-z");
if(resD){
sop("word got deleted successfully");
}else{
sop("word not found");
}
break;
case 4:
sop("Quit successfully");
System.exit(1);
break;
default:
sop("Input int from 1-4");
break;
}
}catch(Exception e){
String badInput = scan.next();
sop("This is bad input: " + badInput);
}
}
}

}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy