Class MaterialTrie
java.lang.Object
com.university.bookstore.search.MaterialTrie
Trie (Prefix Tree) data structure for efficient prefix-based material searching.
Provides O(m) time complexity for prefix searches where m is the length of the prefix.
This implementation stores materials at each node along the path, allowing for efficient prefix-based lookups and autocomplete functionality.
- Since:
- 2024-09-15
- Version:
- 3.0
- Author:
- Navid Mohaghegh
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all materials from the trie.Gets all materials in the trie.booleanChecks if any materials exist with titles starting with the given prefix.voidInserts a material into the trie using its title as the key.booleanisEmpty()Checks if the trie is empty.booleanRemoves a material from the trie.searchByPrefix(String prefix) Searches for materials with titles that start with the given prefix.searchByPrefixWithLimit(String prefix, int limit) Searches for materials with titles that start with the given prefix, limited to a maximum number of results.intsize()Gets the total number of materials in the trie.toString()
-
Constructor Details
-
MaterialTrie
public MaterialTrie()Creates a new empty material trie.
-
-
Method Details
-
insert
Inserts a material into the trie using its title as the key.- Parameters:
material- the material to insert- Throws:
IllegalArgumentException- if material is null
-
searchByPrefix
-
searchByPrefixWithLimit
Searches for materials with titles that start with the given prefix, limited to a maximum number of results.- Parameters:
prefix- the prefix to search forlimit- the maximum number of results to return- Returns:
- list of materials matching the prefix, limited to the specified count
-
hasPrefix
Checks if any materials exist with titles starting with the given prefix.- Parameters:
prefix- the prefix to check- Returns:
- true if materials exist with the prefix
-
getAllMaterials
-
remove
Removes a material from the trie.- Parameters:
material- the material to remove- Returns:
- true if the material was removed, false if not found
-
clear
public void clear()Clears all materials from the trie. -
size
public int size()Gets the total number of materials in the trie.- Returns:
- the number of materials
-
isEmpty
public boolean isEmpty()Checks if the trie is empty.- Returns:
- true if no materials are stored
-
toString
-