Trie

Struct Trie 

Source
pub struct Trie { /* private fields */ }
Expand description

Recursively-defined prefix trie. Typically constructed with Trie::new() for the root node, then insert()ed into many times. Querying for simple existence of a word in the Trie can be performed with contains().

More advanced usage includes using find() to find sub-Trie structures to efficiently search for existince of many words with similar prefixes. See https://github.com/glennhartmann/aoc24/blob/fa21b5787382765a598381c3a0583258b645dc86/src/days/day_19.rs for an example.

Empty strings are considered to exist in every Trie by default.

Implementations§

Source§

impl Trie

Source

pub fn new() -> Self

Create a new Trie root node containing the empty string.

Source

pub fn with_prefix(s: &str, is_terminal: bool) -> Self

Create a sub-node with an existing prefix, whether terminal or not, or can be used to create a root node which does not contain the empty string.

Source

pub fn insert(&mut self, word: &str)

Insert a word into the Trie.

Source

pub fn find(&self, word: &str) -> Option<&Self>

Find the node within the Trie described by word. Note that the returned node may not be terminal. Can be used as any other Trie, for example to continue searching for many suffixes that share the same prefix.

Source

pub fn contains(&self, word: &str) -> bool

Whether the Trie contains word.

Source

pub fn is_terminal(&self) -> bool

Source

pub fn prefix(&self) -> &str

Trait Implementations§

Source§

impl Default for Trie

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Trie

§

impl RefUnwindSafe for Trie

§

impl Send for Trie

§

impl Sync for Trie

§

impl Unpin for Trie

§

impl UnwindSafe for Trie

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.