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 burishHar 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>
impl<T: Ord> AvlTree<T>
Sourcepub fn insert(&mut self, value: T) -> bool
pub fn insert(&mut self, value: T) -> bool
Qiymat qo’shadi va kerak bo’lsa daraxtni balanslaydi. O(log n). Takror qiymat qo’shilmaydi.
Sourcepub fn to_sorted_vec(&self) -> Vec<&T>
pub fn to_sorted_vec(&self) -> Vec<&T>
Tartiblangan ketma-ketlik (inorder).
Sourcepub fn is_balanced(&self) -> bool
pub fn is_balanced(&self) -> bool
Har bir tugunda AVL sharti bajarilganini tekshiradi (testlar uchun).
Trait Implementations§
Source§impl<T: Ord> FromIterator<T> for AvlTree<T>
impl<T: Ord> FromIterator<T> for AvlTree<T>
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more