Skip to main content

BinaryTree

Struct BinaryTree 

Source
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
UsulTartibNatijaQachon kerak
PreorderIldiz → Chap → O’ng1 2 4 5 3Daraxtni nusxalash/serializatsiya
InorderChap → Ildiz → O’ng4 2 5 1 3BST da tartiblangan ketma-ketlik
PostorderChap → O’ng → Ildiz4 5 2 3 1Daraxtni o’chirish, ifoda hisoblash
Level orderQavat-qavat1 / 2 3 / 4 5Eng 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>

Source

pub fn new() -> Self

Bo’sh daraxt.

Source

pub fn from_root(root: TreeNode<T>) -> Self

Ildiz tugunidan daraxt yasaydi.

Source

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

Ildiz → Chap → O’ng.

Source

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

Chap → Ildiz → O’ng.

Source

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

Chap → O’ng → Ildiz.

Source

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());
Source

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

Qavatma-qavat (BFS) — har bir qavat alohida vektor.

Source

pub fn height(&self) -> usize

Balandlik: ildizdan eng uzoq bargacha qirralar soni. Bo’sh daraxtda 0.

Source

pub fn size(&self) -> usize

Tugunlar soni.

Source

pub fn leaf_count(&self) -> usize

Barglar (farzandsiz tugunlar) soni.

Source

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]);
Source

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>

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for BinaryTree<T>

Source§

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

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

impl<T: Default> Default for BinaryTree<T>

Source§

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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.