[−][src]Struct gbdt::binary_tree::BinaryTree
The binary tree.
Methods
impl<T> BinaryTree<T>
[src]
pub fn new() -> Self
[src]
Build a new empty binary tree
pub fn is_empty(&self) -> bool
[src]
Returns true when the binary tree is empty
pub fn add_root(&mut self, root: BinaryTreeNode<T>) -> TreeIndex
[src]
Add a node as the root node. Return the index of the root node.
Example
use gbdt::binary_tree::{BinaryTree, BinaryTreeNode}; let mut tree: BinaryTree<f32> = BinaryTree::new(); let root = BinaryTreeNode::new(10.0); let root_index = tree.add_root(root); assert_eq!(0, root_index); println!("{}", root_index)
pub fn get_root_index(&self) -> TreeIndex
[src]
Return the index of the root node. Call this API after inserting root node.
Example
use gbdt::binary_tree::{BinaryTree, BinaryTreeNode}; let mut tree: BinaryTree<f32> = BinaryTree::new(); let root = BinaryTreeNode::new(10.0); assert_eq!(0, tree.get_root_index());
pub fn get_left_child(
&self,
node: &BinaryTreeNode<T>
) -> Option<&BinaryTreeNode<T>>
[src]
&self,
node: &BinaryTreeNode<T>
) -> Option<&BinaryTreeNode<T>>
Return the left child of the given node
Example
use gbdt::binary_tree::{BinaryTree, BinaryTreeNode}; let mut tree: BinaryTree<f32> = BinaryTree::new(); let root = BinaryTreeNode::new(10.0); let root_index = tree.add_root(root); let left_node = BinaryTreeNode::new(5.0); let _ = tree.add_left_node(root_index, left_node); let root = tree.get_node(root_index).expect("Didn't find root node"); let left_node = tree.get_left_child(root).expect("Didn't find left child"); println!("{}", left_node.value); assert!((left_node.value - 5.0) < 0.001)
pub fn get_right_child(
&self,
node: &BinaryTreeNode<T>
) -> Option<&BinaryTreeNode<T>>
[src]
&self,
node: &BinaryTreeNode<T>
) -> Option<&BinaryTreeNode<T>>
Return the right child of the given node
Example
use gbdt::binary_tree::{BinaryTree, BinaryTreeNode}; let mut tree: BinaryTree<f32> = BinaryTree::new(); let root = BinaryTreeNode::new(10.0); let root_index = tree.add_root(root); let right_node = BinaryTreeNode::new(5.0); let _ = tree.add_right_node(root_index, right_node); let root = tree.get_node(root_index).expect("Didn't find root node"); let right_node = tree.get_right_child(root).expect("Didn't find right child"); println!("{}", right_node.value); assert!((right_node.value - 5.0) < 0.001)
pub fn get_node(&self, index: TreeIndex) -> Option<&BinaryTreeNode<T>>
[src]
Return the node with the given index
Example
use gbdt::binary_tree::{BinaryTree, BinaryTreeNode}; let mut tree: BinaryTree<i32> = BinaryTree::new(); let root = BinaryTreeNode::new(10); let _ = tree.add_root(root); let root_index = tree.get_root_index(); let root = tree.get_node(root_index).expect("Didn't find root node"); assert_eq!(10, root.value);
pub fn get_node_mut(
&mut self,
index: TreeIndex
) -> Option<&mut BinaryTreeNode<T>>
[src]
&mut self,
index: TreeIndex
) -> Option<&mut BinaryTreeNode<T>>
Return the muttable reference of a node with the given index
Example
use gbdt::binary_tree::{BinaryTree, BinaryTreeNode}; let mut tree: BinaryTree<i32> = BinaryTree::new(); let root = BinaryTreeNode::new(10); let _ = tree.add_root(root); let root_index = tree.get_root_index(); let root = tree.get_node_mut(root_index).expect("Didn't find root node"); root.value = 11; assert_eq!(11, root.value);
pub fn add_left_node(
&mut self,
parent: TreeIndex,
child: BinaryTreeNode<T>
) -> TreeIndex
[src]
&mut self,
parent: TreeIndex,
child: BinaryTreeNode<T>
) -> TreeIndex
Add a node as the left child of a given parent
node. Return the index of the added node.
Example
use gbdt::binary_tree::{BinaryTree, BinaryTreeNode}; let mut tree: BinaryTree<f32> = BinaryTree::new(); let root = BinaryTreeNode::new(10.0); let root_index = tree.add_root(root); let left_node = BinaryTreeNode::new(5.0); let _ = tree.add_left_node(root_index, left_node); let root = tree.get_node(root_index).expect("Didn't find root node"); let left_node = tree.get_left_child(root).expect("Didn't find left child"); println!("{}", left_node.value); assert!((left_node.value - 5.0) < 0.001)
pub fn add_right_node(
&mut self,
parent: TreeIndex,
child: BinaryTreeNode<T>
) -> TreeIndex
[src]
&mut self,
parent: TreeIndex,
child: BinaryTreeNode<T>
) -> TreeIndex
Add a node as the right child of a given parent
node. Return the index of the added node.
Example
use gbdt::binary_tree::{BinaryTree, BinaryTreeNode}; let mut tree: BinaryTree<f32> = BinaryTree::new(); let root = BinaryTreeNode::new(10.0); let root_index = tree.add_root(root); let right_node = BinaryTreeNode::new(5.0); let _ = tree.add_right_node(root_index, right_node); let root = tree.get_node(root_index).expect("Didn't find root node"); let right_node = tree.get_right_child(root).expect("Didn't find right child"); println!("{}", right_node.value); assert!((right_node.value - 5.0) < 0.001)
pub fn print(&self) where
T: Debug,
[src]
T: Debug,
For debug use. This API will print the whole tree.
Example
use gbdt::binary_tree::{BinaryTree, BinaryTreeNode}; let mut tree: BinaryTree<f32> = BinaryTree::new(); let root = BinaryTreeNode::new(10.0); let root_index = tree.add_root(root); let n1 = BinaryTreeNode::new(5.0); let n2 = BinaryTreeNode::new(6.0); let n1_index = tree.add_left_node(root_index, n1); let n2_index = tree.add_right_node(root_index, n2); let n3 = BinaryTreeNode::new(7.0); let n4 = BinaryTreeNode::new(8.0); tree.add_left_node(n2_index, n3); tree.add_right_node(n2_index, n4); let n5 = BinaryTreeNode::new(9.0); tree.add_left_node(n1_index, n5); tree.print(); // Output: //----10.0 // ----5.0 // ----9.0 // ----6.0 // ----7.0 // ----8.0
pub fn len(&self) -> usize
[src]
Get the amount of the nodes in this tree.
Example
use gbdt::binary_tree::{BinaryTree, BinaryTreeNode}; let mut tree: BinaryTree<f32> = BinaryTree::new(); let root = BinaryTreeNode::new(10.0); let root_index = tree.add_root(root); let n1 = BinaryTreeNode::new(5.0); let n2 = BinaryTreeNode::new(6.0); let n1_index = tree.add_left_node(root_index, n1); let n2_index = tree.add_right_node(root_index, n2); assert_eq!(3, tree.len());
Trait Implementations
impl<T> Default for BinaryTree<T>
[src]
impl<T: Debug> Debug for BinaryTree<T>
[src]
impl<T> Serialize for BinaryTree<T> where
T: Serialize,
[src]
T: Serialize,
fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error> where
__S: Serializer,
[src]
__S: Serializer,
impl<'de, T> Deserialize<'de> for BinaryTree<T> where
T: Deserialize<'de>,
[src]
T: Deserialize<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
[src]
__D: Deserializer<'de>,
Auto Trait Implementations
impl<T> Send for BinaryTree<T> where
T: Send,
T: Send,
impl<T> Sync for BinaryTree<T> where
T: Sync,
T: Sync,
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> DeserializeOwned for T where
T: Deserialize<'de>,
[src]
T: Deserialize<'de>,