Struct gbdt::binary_tree::BinaryTree
source · [−]pub struct BinaryTree<T> { /* private fields */ }
Expand description
The binary tree.
Implementations
sourceimpl<T> BinaryTree<T>
impl<T> BinaryTree<T>
sourcepub fn add_root(&mut self, root: BinaryTreeNode<T>) -> TreeIndex
pub fn add_root(&mut self, root: BinaryTreeNode<T>) -> TreeIndex
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)
sourcepub fn get_root_index(&self) -> TreeIndex
pub fn get_root_index(&self) -> TreeIndex
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());
sourcepub fn get_left_child(
&self,
node: &BinaryTreeNode<T>
) -> Option<&BinaryTreeNode<T>>
pub fn get_left_child(
&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)
sourcepub fn get_right_child(
&self,
node: &BinaryTreeNode<T>
) -> Option<&BinaryTreeNode<T>>
pub fn get_right_child(
&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)
sourcepub fn get_node(&self, index: TreeIndex) -> Option<&BinaryTreeNode<T>>
pub fn get_node(&self, index: TreeIndex) -> Option<&BinaryTreeNode<T>>
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);
sourcepub fn get_node_mut(
&mut self,
index: TreeIndex
) -> Option<&mut BinaryTreeNode<T>>
pub fn get_node_mut(
&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);
sourcepub fn add_left_node(
&mut self,
parent: TreeIndex,
child: BinaryTreeNode<T>
) -> TreeIndex
pub fn add_left_node(
&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)
sourcepub fn add_right_node(
&mut self,
parent: TreeIndex,
child: BinaryTreeNode<T>
) -> TreeIndex
pub fn add_right_node(
&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)
sourcepub fn print(&self)where
T: Debug,
pub fn print(&self)where
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
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
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
sourceimpl<T: Debug> Debug for BinaryTree<T>
impl<T: Debug> Debug for BinaryTree<T>
sourceimpl<T> Default for BinaryTree<T>
impl<T> Default for BinaryTree<T>
sourceimpl<'de, T> Deserialize<'de> for BinaryTree<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for BinaryTree<T>where
T: Deserialize<'de>,
sourcefn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations
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>where
T: Unpin,
impl<T> UnwindSafe for BinaryTree<T>where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more