pub struct BinaryTree<T> { /* private fields */ }
Expand description

The binary tree.

Implementations

Build a new empty binary tree

Returns true when the binary tree is empty

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)

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

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)

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)

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

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

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)

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)

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

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

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Deserialize this value from the given Serde deserializer. Read more
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.