# Class MaterialTrie

java.lang.Object
com.university.bookstore.search.MaterialTrie

public class MaterialTrie extends Object
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
  Constructor
  Description
  `MaterialTrie()`
  Creates a new empty material trie.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `clear()`
  Clears all materials from the trie.
  `List<Material>`
  `getAllMaterials()`
  Gets all materials in the trie.
  `boolean`
  `hasPrefix(String prefix)`
  Checks if any materials exist with titles starting with the given prefix.
  `void`
  `insert(Material material)`
  Inserts a material into the trie using its title as the key.
  `boolean`
  `isEmpty()`
  Checks if the trie is empty.
  `boolean`
  `remove(Material material)`
  Removes a material from the trie.
  `List<Material>`
  `searchByPrefix(String prefix)`
  Searches for materials with titles that start with the given prefix.
  `List<Material>`
  `searchByPrefixWithLimit(String prefix, int limit)`
  Searches for materials with titles that start with the given prefix, limited to a maximum number of results.
  `int`
  `size()`
  Gets the total number of materials in the trie.
  `String`
  `toString()`

  ### Methods inherited from class java.lang.Object

  `equals, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### MaterialTrie

    public MaterialTrie()
    Creates a new empty material trie.

- ## Method Details

  - ### insert

    public void insert(Material material)
    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

    public List<Material> searchByPrefix(String prefix)
    Searches for materials with titles that start with the given prefix.
    Parameters:
      `prefix` - the prefix to search for
    Returns:
      list of materials matching the prefix

  - ### searchByPrefixWithLimit

    public List<Material> searchByPrefixWithLimit(String prefix, int limit)
    Searches for materials with titles that start with the given prefix, limited to a maximum number of results.
    Parameters:
      `prefix` - the prefix to search for
      `limit` - the maximum number of results to return
    Returns:
      list of materials matching the prefix, limited to the specified count

  - ### hasPrefix

    public boolean hasPrefix(String prefix)
    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

    public List<Material> getAllMaterials()
    Gets all materials in the trie.
    Returns:
      list of all materials

  - ### remove

    public boolean remove(Material material)
    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

    public String toString()
    Overrides:
      `toString` in class `Object`