Skip to main content

Trie

Struct Trie 

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

Trie — har bir qirra bitta harf, ildizdan tugungacha bo’lgan yo’l — prefiks.

       (root)
       /    \
     o        k
     |        |
     l        i
    / \       |
   m   t      t
   |   |      |
  [a] [i]    [o]      ← [ ] = so'z shu yerda tugaydi
       |
      [n]
 → "olma", "olti", "oltin", "kito"
AmalMurakkablikIzoh
insert / containsO(m)m — so’z uzunligi, lug’at hajmiga bog’liq emas!
starts_withO(m)prefiks bormi
words_with_prefixO(m + natija)avtoto’ldirish

Qayerda ishlatiladi: qidiruv qatoridagi avtoto’ldirish, telefondagi T9, imlo tekshirgich, IP marshrutlash jadvallari, so’z o’yinlari.

HashSet dan farqi: HashSet “bu so’z bormi?” ga javob beradi, Trie esa prefiks bo’yicha ham qidira oladi va xotirada umumiy prefikslarni bir marta saqlaydi.

§Misol

use rust_algorithms::tree::Trie;

let mut t = Trie::new();
for so_z in ["olma", "olti", "oltin", "kitob"] {
    t.insert(so_z);
}

assert!(t.contains("olma"));
assert!(!t.contains("ol"));        // "ol" — so'z emas, faqat prefiks
assert!(t.starts_with("ol"));

let mut topilgan = t.words_with_prefix("olt");
topilgan.sort();
assert_eq!(topilgan, vec!["olti", "oltin"]);
assert_eq!(t.len(), 4);

Implementations§

Source§

impl Trie

Source

pub fn new() -> Self

Bo’sh trie.

Source

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

So’z qo’shadi. Yangi so’z bo’lsa true.

Source

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

Aynan shu so’z lug’atda bormi?

Source

pub fn starts_with(&self, prefix: &str) -> bool

Shu prefiks bilan boshlanadigan so’z bormi?

Source

pub fn words_with_prefix(&self, prefix: &str) -> Vec<String>

Prefiks bilan boshlanadigan barcha so’zlar (avtoto’ldirish).

Source

pub fn words(&self) -> Vec<String>

Lug’atdagi barcha so’zlar.

Source

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

So’zni o’chiradi (tugunlar qoladi, faqat belgisi olib tashlanadi).

Source

pub fn len(&self) -> usize

So’zlar soni.

Source

pub fn is_empty(&self) -> bool

Bo’shmi?

Trait Implementations§

Source§

impl Debug for Trie

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Trie

Source§

fn default() -> Trie

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

impl<S: AsRef<str>> FromIterator<S> for Trie

Source§

fn from_iter<I: IntoIterator<Item = S>>(iter: I) -> Self

Creates a value from an iterator. 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 UnsafeUnpin 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.