https://github.com/arunkumar9t2/trie
A Java implementation of the Trie data structure
https://github.com/arunkumar9t2/trie
java java-library search search-trees trie tries
Last synced: 6 months ago
JSON representation
A Java implementation of the Trie data structure
- Host: GitHub
- URL: https://github.com/arunkumar9t2/trie
- Owner: arunkumar9t2
- License: apache-2.0
- Created: 2017-03-28T06:07:38.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-05-13T07:10:57.000Z (over 7 years ago)
- Last Synced: 2025-03-24T17:52:42.606Z (6 months ago)
- Topics: java, java-library, search, search-trees, trie, tries
- Language: Java
- Homepage:
- Size: 70.3 KB
- Stars: 22
- Watchers: 3
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Trie
A java implementation of the Prefix Trie data structure.# Introduction
Tries are map like ordered tree based data structures that provide fast searching of the order `O(k)` where `k` is the length of key. Read more about trie [here](https://en.wikipedia.org/wiki/Trie).# Motivation
This was initially built to use in my Android app, **T9 App Launcher** to quickly search through list of installed applications and launch them.# Public APIs
Currently there are 3 public implementations that can be chosen from.- **`MapTrie`:** `HashMap` backed trie implementation.
- **`SortedTrie`:** `TreeMap` backed trie implementation which returns suggestions and value is ascending sort order.
- **`T9Trie`:** Helper implementation to store and retrieve suggestions for T9 sequence.## Example usage
Creating a simple trie and getting suggestions.final MapTrie trie = new MapTrie<>();
trie.insert("Hello", "Hello");
trie.insert("Help", "Help");
trie.insert("Has", "Has");
trie.insert("Have", "Have");
trie.insert("Had", "Had");
trie.insert("Hadn't", "Hadn't");
// Print to stdout
trie.print();The above `print()` call would print the tree structure like this.
└── h
├── e
│ └── l
│ ├── l
│ │ └── o'Hello
│ └── p'Help
└── a
├── s'Has
├── v
│ └── e'Have
└── d'Had
└── n
└── '
└── t'Hadn't
Now let's try to find all the words starting with `ha`. Remember that, by default the keys are **case insensitive.**List suggestions = trie.getValueSuggestions("ha");
System.out.print(suggestions.toString());
This will print `[Has, Have, Had, Hadn't]` to the console. **The value is left untouched.**# Contribution
If you are a developer and would like to contribute please consider making a pull request. I would appreciate any criticisms with regards to code or implementation in general.# License
This project is licensed under Apache 2.0 license.