Skip to main content

AvlTree

Struct AvlTree 

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

AVL daraxti: har bir tugunda chap va o’ng shox balandliklari farqi ≤ 1.

Shu qat’iy shart tufayli balandlik har doim ~1.44·log₂(n) dan oshmaydi — ya’ni insert, contains, remove kafolatlangan O(log n).

§Balansni qanday tiklaydi: 4 ta burilish (rotation)

 1) LL (chapga qiyshaygan)          o'ngga burish
       z                                y
      /                               /   \
     y            ──────────►        x     z
    /
   x

 2) RR (o'ngga qiyshaygan)          chapga burish  (1 ning ko'zgusi)

 3) LR: avval chap shoxni chapga burish → LL holatiga keladi → o'ngga burish
 4) RL: avval o'ng shoxni o'ngga burish → RR holatiga keladi → chapga burish

Har bir burilish — bir nechta ko’rsatkichni almashtirish, ya’ni O(1). Qo’shishdan keyin ko’pi bilan 1 ta balanslash yetadi.

§BST bilan solishtirish

1..10 sonlarini ketma-ket qo'shsak:
  BST balandligi = 9   (bir tomonlama zanjir)
  AVL balandligi = 3   (balanslangan)

§Misol

use rust_algorithms::tree::AvlTree;

let mut t = AvlTree::new();
for x in 1..=1000 {
    t.insert(x); // tartiblangan kirish — BST uchun eng yomon holat
}
assert_eq!(t.len(), 1000);
assert!(t.height() <= 14); // log2(1000) ≈ 10, AVL kafolati ≈ 1.44·log2(n)
assert!(t.contains(&777));
assert_eq!(t.to_sorted_vec().first(), Some(&&1));

Implementations§

Source§

impl<T: Ord> AvlTree<T>

Source

pub fn new() -> Self

Bo’sh daraxt.

Source

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

Qiymat qo’shadi va kerak bo’lsa daraxtni balanslaydi. O(log n). Takror qiymat qo’shilmaydi.

Source

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

Qiymat bormi? O(log n).

Source

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

Tartiblangan ketma-ketlik (inorder).

Source

pub fn len(&self) -> usize

Tugunlar soni.

Source

pub fn is_empty(&self) -> bool

Bo’shmi?

Source

pub fn height(&self) -> i32

Balandlik (tugunlar bo’yicha; bo’sh daraxtda 0).

Source

pub fn is_balanced(&self) -> bool

Har bir tugunda AVL sharti bajarilganini tekshiradi (testlar uchun).

Trait Implementations§

Source§

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

Source§

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

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

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

Source§

fn default() -> Self

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

impl<T: Ord> FromIterator<T> for AvlTree<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 AvlTree<T>

§

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

§

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

§

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

§

impl<T> Unpin for AvlTree<T>

§

impl<T> UnsafeUnpin for AvlTree<T>

§

impl<T> UnwindSafe for AvlTree<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.