[][src]Struct gbdt::binary_tree::BinaryTree

pub struct BinaryTree<T> { /* fields omitted */ }

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]

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]

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]

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]

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]

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]

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]

impl<'de, T> Deserialize<'de> for BinaryTree<T> where
    T: Deserialize<'de>, 
[src]

Auto Trait Implementations

impl<T> Send for BinaryTree<T> where
    T: Send

impl<T> Sync for BinaryTree<T> where
    T: Sync

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: Deserialize<'de>, 
[src]