Skip to main content

BinarySearchTree

Struct BinarySearchTree 

Source
pub struct BinarySearchTree<T: Ord> { /* private fields */ }
Expand description

Binary Search Tree — BST qoidasi: chapdagi hamma qiymat tugundan kichik, o’ngdagi hamma qiymat tugundan katta.

         50
       /    \
     30      70
    /  \       \
  20    40      80      inorder → 20 30 40 50 70 80  (tartiblangan!)
AmalO’rtachaEng yomon
insert / contains / removeO(log n)O(n)

Eng yomon holat qachon? Tartiblangan ma’lumotni ketma-ket qo’shsangiz, daraxt bir tomonga cho’zilib, oddiy bog’langan ro’yxatga aylanadi:

 1 → 2 → 3 → 4 → 5      (balandlik n, qidiruv O(n))

Shu muammoning yechimi — o’zini balanslaydigan daraxtlar: AvlTree, Red-Black tree (Rustdagi BTreeMap esa B-daraxt).

§Misol

use rust_algorithms::tree::BinarySearchTree;

let mut t = BinarySearchTree::new();
for x in [50, 30, 70, 20, 40, 80] {
    t.insert(x);
}

assert!(t.contains(&40));
assert!(!t.contains(&45));
assert_eq!(t.min(), Some(&20));
assert_eq!(t.max(), Some(&80));
assert_eq!(t.to_sorted_vec(), vec![&20, &30, &40, &50, &70, &80]);

assert!(t.remove(&30));
assert_eq!(t.to_sorted_vec(), vec![&20, &40, &50, &70, &80]);

Implementations§

Source§

impl<T: Ord> BinarySearchTree<T>

Source

pub fn new() -> Self

Bo’sh daraxt.

Source

pub fn insert(&mut self, value: T) -> bool

Qiymat qo’shadi. Takroriy qiymat qo’shilmaydi (false qaytadi).

Iterativ — chuqur daraxtlarda ham stack to’lmaydi.

Source

pub fn contains(&self, value: &T) -> bool

Qiymat bormi? — ildizdan boshlab “kichik → chap, katta → o’ng”.

Source

pub fn min(&self) -> Option<&T>

Eng kichik qiymat — eng chapdagi tugun.

Source

pub fn max(&self) -> Option<&T>

Eng katta qiymat — eng o’ngdagi tugun.

Source

pub fn remove(&mut self, value: &T) -> bool

Qiymatni o’chiradi. Topilsa true.

Uchta holat:

  1. Barg — shunchaki o’chiramiz;
  2. Bitta farzand — farzandni o’z o’rniga qo’yamiz;
  3. Ikkita farzand — o’ng shoxdagi eng kichik qiymatni (inorder successor) o’rniga ko’chiramiz va uni o’ng shoxdan o’chiramiz. BST qoidasi buzilmaydi.
Source

pub fn to_sorted_vec(&self) -> Vec<&T>

Inorder aylanish — tartiblangan ketma-ketlik. O(n).

Source

pub fn len(&self) -> usize

Tugunlar soni.

Source

pub fn is_empty(&self) -> bool

Bo’shmi?

Source

pub fn height(&self) -> usize

Balandlik (qirralar bo’yicha). Bo’sh daraxtda 0.

Trait Implementations§

Source§

impl<T: Debug + Ord> Debug for BinarySearchTree<T>

Source§

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

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

impl<T: Ord> Default for BinarySearchTree<T>

Source§

fn default() -> Self

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

impl<T: Ord> FromIterator<T> for BinarySearchTree<T>

Source§

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

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<T> Freeze for BinarySearchTree<T>

§

impl<T> RefUnwindSafe for BinarySearchTree<T>
where T: RefUnwindSafe,

§

impl<T> Send for BinarySearchTree<T>
where T: Send,

§

impl<T> Sync for BinarySearchTree<T>
where T: Sync,

§

impl<T> Unpin for BinarySearchTree<T>

§

impl<T> UnsafeUnpin for BinarySearchTree<T>

§

impl<T> UnwindSafe for BinarySearchTree<T>
where T: UnwindSafe,

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.