pub struct BinaryTree<T> {
pub root: Option<Box<TreeNode<T>>>,
}Expand description
Binar daraxt — har bir tugunning ko’pi bilan 2 ta farzandi bor.
§Aylanib chiqishning 4 usuli
1
/ \
2 3
/ \
4 5| Usul | Tartib | Natija | Qachon kerak |
|---|---|---|---|
| Preorder | Ildiz → Chap → O’ng | 1 2 4 5 3 | Daraxtni nusxalash/serializatsiya |
| Inorder | Chap → Ildiz → O’ng | 4 2 5 1 3 | BST da tartiblangan ketma-ketlik |
| Postorder | Chap → O’ng → Ildiz | 4 5 2 3 1 | Daraxtni o’chirish, ifoda hisoblash |
| Level order | Qavat-qavat | 1 / 2 3 / 4 5 | Eng qisqa yo’l, qavatlar bilan ishlash |
Dastlabki uchtasi — DFS (chuqurlikka), oxirgisi — BFS (kenglikka, navbat bilan).
§Misol
use rust_algorithms::tree::{BinaryTree, TreeNode};
// 1
// / \
// 2 3
// / \
// 4 5
let daraxt = BinaryTree::from_root(TreeNode::new(
1,
Some(TreeNode::new(2, Some(TreeNode::leaf(4)), Some(TreeNode::leaf(5)))),
Some(TreeNode::leaf(3)),
));
assert_eq!(daraxt.preorder(), vec![&1, &2, &4, &5, &3]);
assert_eq!(daraxt.inorder(), vec![&4, &2, &5, &1, &3]);
assert_eq!(daraxt.postorder(), vec![&4, &5, &2, &3, &1]);
assert_eq!(daraxt.level_order(), vec![vec![&1], vec![&2, &3], vec![&4, &5]]);
assert_eq!(daraxt.height(), 2);
assert_eq!(daraxt.size(), 5);Fields§
§root: Option<Box<TreeNode<T>>>Daraxt ildizi.
Implementations§
Source§impl<T> BinaryTree<T>
impl<T> BinaryTree<T>
Sourcepub fn inorder_iterative(&self) -> Vec<&T>
pub fn inorder_iterative(&self) -> Vec<&T>
Inorderning rekursiyasiz varianti — stack bilan.
Chuqur daraxtlarda rekursiya stackni to’ldirishi mumkin; bu variant xavfsiz.
§Misol
use rust_algorithms::tree::{BinaryTree, TreeNode};
let t = BinaryTree::from_root(TreeNode::new(
2, Some(TreeNode::leaf(1)), Some(TreeNode::leaf(3)),
));
assert_eq!(t.inorder_iterative(), t.inorder());Sourcepub fn level_order(&self) -> Vec<Vec<&T>>
pub fn level_order(&self) -> Vec<Vec<&T>>
Qavatma-qavat (BFS) — har bir qavat alohida vektor.
Sourcepub fn height(&self) -> usize
pub fn height(&self) -> usize
Balandlik: ildizdan eng uzoq bargacha qirralar soni. Bo’sh daraxtda 0.
Sourcepub fn leaf_count(&self) -> usize
pub fn leaf_count(&self) -> usize
Barglar (farzandsiz tugunlar) soni.
Sourcepub fn invert(&mut self)
pub fn invert(&mut self)
Daraxtni “ko’zguga” aylantiradi: chap va o’ng shoxlarni almashtiradi.
§Misol
use rust_algorithms::tree::{BinaryTree, TreeNode};
let mut t = BinaryTree::from_root(TreeNode::new(
1, Some(TreeNode::leaf(2)), Some(TreeNode::leaf(3)),
));
t.invert();
assert_eq!(t.inorder(), vec![&3, &1, &2]);Sourcepub fn is_balanced(&self) -> bool
pub fn is_balanced(&self) -> bool
Daraxt balanslanganmi? (har bir tugunda chap/o’ng balandlik farqi ≤ 1)
Trait Implementations§
Source§impl<T: Clone> Clone for BinaryTree<T>
impl<T: Clone> Clone for BinaryTree<T>
Source§fn clone(&self) -> BinaryTree<T>
fn clone(&self) -> BinaryTree<T>
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T: Debug> Debug for BinaryTree<T>
impl<T: Debug> Debug for BinaryTree<T>
Source§impl<T: Default> Default for BinaryTree<T>
impl<T: Default> Default for BinaryTree<T>
Source§fn default() -> BinaryTree<T>
fn default() -> BinaryTree<T>
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl<T> Freeze for BinaryTree<T>
impl<T> RefUnwindSafe for BinaryTree<T>where
T: RefUnwindSafe,
impl<T> Send for BinaryTree<T>where
T: Send,
impl<T> Sync for BinaryTree<T>where
T: Sync,
impl<T> Unpin for BinaryTree<T>
impl<T> UnsafeUnpin for BinaryTree<T>
impl<T> UnwindSafe for BinaryTree<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