Skip to content
Open
Show file tree
Hide file tree
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
146 changes: 146 additions & 0 deletions Dictionary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package logic;


import java.util.Scanner;

import logic.Trie;

public class Dictionary
{
public static void main(String [] args)
{

int choice = 0;
Trie Dictionary=new Trie();
Scanner sc=new Scanner(System.in);
int n = 0;

do
{

do
{

System.out.println("\n\tM E N U");
System.out.println("*******************************");
System.out.println("1. INSERT A WORD");
System.out.println("2. SEARCH A WORD");
System.out.println("3. DELETE A WORD");
System.out.println("4. UPDATE MEANING OF A WORD");
System.out.println("5. DISPLAY WITH A PREFIX");
System.out.println("6. DISPLAY ALL THE WORDS");
System.out.println("0. EXIT");
System.out.println("*******************************");
System.out.print("Enter choice : ");
if(sc.hasNextInt())
{
choice = sc.nextInt();
}

}while(choice<0 || choice>6);

switch(choice)
{
case 1 :
System.out.print("\n Enter the number of words you want to insert : ");

n =sc.nextInt();
for(int i=0;i<n;i++)
{
System.out.print("\n Enter the word: ");
String word;
word=sc.next();
System.out.print("\n Enter the meaning: ");
String meaning;
meaning=sc.next();
Dictionary.insert(word,meaning);
System.out.println("\n-----------------------------");
}

break;

case 2 :

System.out.print("\n Enter the word you want to search : ");
String searchWord;
searchWord=sc.next();
Boolean search = false;
search = Dictionary.search(searchWord);
if(!search)
{
System.out.println("\n Word doesn't exist");
}
System.out.println("\n-----------------------------");
break;

case 3 :
System.out.print("\n Enter the word you want to delete: ");
String deleteWord;
deleteWord=sc.next();
Boolean delete = false;
delete = Dictionary.delete(deleteWord);
if(delete)
{
System.out.println(" Word deleted successfully!!");
}
else
{
System.out.println("\n Word doesn't exist!");
}
System.out.println("\n-----------------------------");
break;

case 4 :
String updateWord;
System.out.print("\n Enter the word to be updated : ");
updateWord = sc.next();
Dictionary.update(Dictionary.getRoot(), updateWord);
System.out.println("\n-----------------------------");
break;

case 5 :
String prefixWord;
if(Dictionary.getRoot() != null)
{
System.out.println("\n Enter the prefix :");
prefixWord = sc.next();
Dictionary.displayWithPrefix(Dictionary.getRoot(), prefixWord);
}
else
{
System.out.println("\n Dictionary is empty!");
}
System.out.println("\n-----------------------------");
break;

case 6 :
String word=" ";
if(Dictionary.getRoot() != null)
{
Dictionary.display(Dictionary.getRoot(),word);
}
else
{
System.out.println(" No words in the dictionary");
}
System.out.println("\n-----------------------------");
break;

case 0 :
break;

default :
System.out.println("Invalid input");
System.out.println("\n-----------------------------");
break;

}

}while(choice != 0);

sc.close();

}

}

22 changes: 22 additions & 0 deletions Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package logic;

public class Node
{
char c;
boolean isWord = false;
Node[] children;
String meaning;

public Node(char c)
{
this.c=c;
isWord=false;
children = new Node[26];
}

public void setMeaning(String newMeaning)
{
this.meaning=newMeaning;
}
}

70 changes: 67 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,69 @@
# Buffer_2.0
Buffer_2.0 repo to submit your projects to.Repository containing folder-wise code for all submissions to Buffer : A Project Series, conducted between April, 2021 - June, 2020.
# Dictionary in Java
The following project implements a dictionary using Java.
Functionality available:
1. Adding a word to dictionary along with its meaning
2. updating a word's meaning
3. Delete a word and its meaning
4. search for a word's meaning

Each folder should contain a separate README to describe the files and project objective

### **Data Structure Used:**

The data structure used to add words is **Trie**. Also known as **Prefix Trees**. Tries have the unique feature that the time to insert or to delete or find is almost identical because the code paths followed are most identical. It is an efficient information reTRIEval data structure.
There are other alternative data structures that can be used to build and perform these operations on a dictionary. The most significant one being BST.
We used Trie over BST for the following reasons:
* In trie looking up a key of length m takes worst case O(m) time. On the other hand a well balanced BST performs O((logn)) comparisons of keys where n is the number of elements in the tree.Hence is the worst case a BST will need time proportional to M * log N, where M is maximum string
length and N is number of keys in tree. Using Trie, we can search the key in O(M) time.
* Another huge advantage of Trie over the other considerable Data Structure HashTable is printing out all the words in alphabetical order.
* An efficient prefix search is also possible through Tries.
Since a dictionary is often used to search meanings of words, we chose to favor time complexity instead of space and went ahead with
tries.

### **Code Design Parameters:**

The trie node looks like this:
```
public class Node
{
char c;
boolean isWord;
Node[] children;
String meaning;

public Node(char c)
{
this.c=c;
isWord=false;
children=new Node[26];
}
public void setMeaning(String newMeaning)
{
this.meaning=newMeaning;
}

}
```
Here, each node has 26 children! thats a lot of children for a node. And a lot of space. But this much space REALLY helps us reduce
that searching time complexity from O(M * log N) down to O(M) time, where M is length of word and N is number of keys. Plus,
Tries are more space efficient when they contain a large number of short keys, because nodes are shared between keys with common
initial subsequences. So, when it comes to handling large amounts of data, trie is our obvious winner.

### **Outcomes:**

1. A new Data Structure- Trie - As students who have predominantly coded in C throughout the first two years of college, picking up a new language and a data structure that we weren't acquainted with was exciting and challenging. At a glance while Trie appeared to be a way more complex tree than the other trees, as we digged deeper we discovered the varied functionality of this data structure and why it is used more so for projects where large datasets are used. It makes their access and serach easier.
2. Understanding time and space complexitites better- We made a head to head comparsion of time complexities of varied data structures that could perform the same operations and then made an informed choice based on our observation. This has helped us greatly in understanding the paramount importance of time complexity in projects like these. The project has made us more intuitive and familair with the role of space and time complexity.
3. Working in a team along with fellow classmated beyond the classroom also has been a great experience.

### **What's next for the project**

We aim to improvise on the code and make in user friendly by incorporating Java Swing to enhance the real time effect. We also mean to import datasets using SQL and make the dictionary user-ready

### **References:**

1. Data Structures and Algorithms Made Easy in Java- Book

2. https://thenextcode.wordpress.com/2015/04/12/trie-vs-bst-vs-hashtable/
3. https://www.geeksforgeeks.org/trie-insert-and-search/
4. https://www.baeldung.com/trie-java#:~:text=In%20a%20trie%2C%20every%20node,of%20the%20trie%20as%20well
5. https://gist.github.com/prettymuchbryce/3719435/868ae3231e98b3bde667a9e899157e20dd58c366
6. https://www.geeksforgeeks.org/implement-a-dictionary-using-trie/
Loading