Struct T

Source
pub struct T(pub TreeHandle);
Expand description

Zero cost wrapper for Option<Rc<RefCell<TreeNode>>>, also for bypassing the orphan rule. There are many useful methods for operating on the binary tree as well.

Tuple Fields§

§0: TreeHandle

Implementations§

Source§

impl T

Source

pub fn height(&self) -> usize

Get the height for the binary tree.

Returns the height of the binary tree. The empty tree has height 0.

§Examples
use leetcode_test_utils::btree;
use leetcode_test_utils::tree::T;

let tree1 = btree!(1, 2, 3);
assert_eq!(T(tree1).height(), 2);
assert_eq!(T(None).height(), 0);
Source

pub fn pre_order(&self) -> Vec<i32>

Returns the pre-order of the binary tree.

Source

pub fn in_order(&self) -> Vec<i32>

Returns the in-order of the binary tree.

Source

pub fn post_order(&self) -> Vec<i32>

Returns the post-order of the binary tree.

Source

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

Returns the level order of the binary tree.

Source

pub fn re_owned(&mut self)

Launder the binary tree.

Replace the current binary tree with a new representation, in which the structure and values is preserved respectively, but every reachable Rc will only have 1 strong count.

This is helpful if you do not want the value in your tree changed through Rc<RefCell<TreeNode>> elsewhere.

§Examples
use leetcode_test_utils::btree;
use leetcode_test_utils::tree::T;
use std::rc::Rc;

let tree = T(btree!(3));
let evil = Rc::clone(tree.0.as_ref().unwrap());
// the action below changes the value handled in `tree`, which may be unexpected
evil.borrow_mut().val = 42;
assert_ne!(tree.0.unwrap().borrow().val, 3);
Source

pub fn detach(&self) -> Self

Get the mirror tree.

Returns a binary tree sharing the same structure and values handled by self except that every reachable Rc will only have 1 strong count.

This is helpful if you want to get the tree structure without worrying about the values be soon changed by code elsewhere.

§Examples
use leetcode_test_utils::btree;
use leetcode_test_utils::tree::T;
use std::rc::Rc;

let tree = T(btree!(3));
let cannot_invade = tree.detach();
cannot_invade.0.as_ref().unwrap().borrow_mut().val = 42;
assert_eq!(tree.0.unwrap().borrow().val, 3);
Source

pub fn is_balanced(&self) -> bool

Test if the binary tree is balanced.

§Example
use leetcode_test_utils::btree;
use leetcode_test_utils::tree::T;

let tree1 = T(btree!(4, 2));
assert!(tree1.is_balanced());

let tree2 = T(btree!(4, 2, null, 1));
assert!(!tree2.is_balanced())
Source

pub fn is_binary_search_tree(&self) -> bool

Test if the binary tree is a BST(binary search tree).

§Examples
use leetcode_test_utils::btree;
use leetcode_test_utils::tree::T;

let tree = T(btree!(5, 2, 9, 1));
assert!(tree.is_binary_search_tree());
Source

pub fn to_leetcode_raw(&self) -> String

Returns the leetcode representation of the handled binary tree.

§Examples
use leetcode_test_utils::tree::T;
use leetcode_test_utils::btree;

let tree = T(btree!(2, 4, null, 9));
assert_eq!(tree.to_leetcode_raw(), "[2,4,null,9]");
Source

pub fn to_leetcode(&self) -> Vec<Option<i32>>

Returns the parsed leetcode representation of the handled binary tree.

§Examples
use leetcode_test_utils::tree::T;
use leetcode_test_utils::btree;

let tree = T(btree!(2, 4, null, 9));
assert_eq!(tree.to_leetcode(), vec![Some(2), Some(4), None, Some(9)]);
Source

pub fn len(&self) -> usize

Returns the len of the binary tree.

§Examples
use leetcode_test_utils::tree::T;
use leetcode_test_utils::btree;

let tree = T(btree!(2, 4, null, 9));
assert_eq!(tree.len(), 3);

Trait Implementations§

Source§

impl Clone for T

Source§

fn clone(&self) -> T

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for T

Source§

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

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

impl Display for T

Source§

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

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

impl PartialEq for T

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl Freeze for T

§

impl !RefUnwindSafe for T

§

impl !Send for T

§

impl !Sync for T

§

impl Unpin for T

§

impl !UnwindSafe for T

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.