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!)| Amal | O’rtacha | Eng yomon |
|---|---|---|
insert / contains / remove | O(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>
impl<T: Ord> BinarySearchTree<T>
Sourcepub fn insert(&mut self, value: T) -> bool
pub fn insert(&mut self, value: T) -> bool
Qiymat qo’shadi. Takroriy qiymat qo’shilmaydi (false qaytadi).
Iterativ — chuqur daraxtlarda ham stack to’lmaydi.
Sourcepub fn contains(&self, value: &T) -> bool
pub fn contains(&self, value: &T) -> bool
Qiymat bormi? — ildizdan boshlab “kichik → chap, katta → o’ng”.
Sourcepub fn remove(&mut self, value: &T) -> bool
pub fn remove(&mut self, value: &T) -> bool
Qiymatni o’chiradi. Topilsa true.
Uchta holat:
- Barg — shunchaki o’chiramiz;
- Bitta farzand — farzandni o’z o’rniga qo’yamiz;
- Ikkita farzand — o’ng shoxdagi eng kichik qiymatni (inorder successor) o’rniga ko’chiramiz va uni o’ng shoxdan o’chiramiz. BST qoidasi buzilmaydi.
Sourcepub fn to_sorted_vec(&self) -> Vec<&T>
pub fn to_sorted_vec(&self) -> Vec<&T>
Inorder aylanish — tartiblangan ketma-ketlik. O(n).
Trait Implementations§
Source§impl<T: Ord> Default for BinarySearchTree<T>
impl<T: Ord> Default for BinarySearchTree<T>
Source§impl<T: Ord> FromIterator<T> for BinarySearchTree<T>
impl<T: Ord> FromIterator<T> for BinarySearchTree<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 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> 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