[][src]Struct slab_tree::NodeRef

pub struct NodeRef<'a, T> { /* fields omitted */ }

An immutable reference to a given Node's data and its relatives.

Methods

impl<'a, T> NodeRef<'a, T>[src]

pub fn node_id(&self) -> NodeId[src]

Returns the NodeId that identifies this Node in the tree.

use slab_tree::tree::TreeBuilder;

let mut tree = TreeBuilder::new().with_root(1).build();
let root_id = tree.root_id().expect("root doesn't exist?");
let root = tree.root_mut().expect("root doesn't exist?");

let root_id_again = root.as_ref().node_id();

assert_eq!(root_id_again, root_id);

pub fn data(&self) -> &'a T[src]

Returns a reference to the data contained by the given Node.

use slab_tree::tree::TreeBuilder;

let mut tree = TreeBuilder::new().with_root(1).build();

let root = tree.root().expect("root doesn't exist?");

assert_eq!(root.data(), &1);

pub fn parent(&self) -> Option<NodeRef<T>>[src]

Returns a NodeRef pointing to this Node's parent. Returns a Some-value containing the NodeRef if this Node has a parent; otherwise returns a None.

use slab_tree::tree::TreeBuilder;

let mut tree = TreeBuilder::new().with_root(1).build();

let root = tree.root().expect("root doesn't exist?");

assert!(root.parent().is_none());

pub fn prev_sibling(&self) -> Option<NodeRef<T>>[src]

Returns a NodeRef pointing to this Node's previous sibling. Returns a Some-value containing the NodeRef if this Node has a previous sibling; otherwise returns a None.

use slab_tree::tree::TreeBuilder;

let mut tree = TreeBuilder::new().with_root(1).build();

let root = tree.root().expect("root doesn't exist?");

assert!(root.prev_sibling().is_none());

pub fn next_sibling(&self) -> Option<NodeRef<T>>[src]

Returns a NodeRef pointing to this Node's next sibling. Returns a Some-value containing the NodeRef if this Node has a next sibling; otherwise returns a None.

use slab_tree::tree::TreeBuilder;

let mut tree = TreeBuilder::new().with_root(1).build();

let root = tree.root().expect("root doesn't exist?");

assert!(root.next_sibling().is_none());

pub fn first_child(&self) -> Option<NodeRef<T>>[src]

Returns a NodeRef pointing to this Node's first child. Returns a Some-value containing the NodeRef if this Node has a first child; otherwise returns a None.

use slab_tree::tree::TreeBuilder;

let mut tree = TreeBuilder::new().with_root(1).build();

let root = tree.root().expect("root doesn't exist?");

assert!(root.first_child().is_none());

pub fn last_child(&self) -> Option<NodeRef<T>>[src]

Returns a NodeRef pointing to this Node's last child. Returns a Some-value containing the NodeRef if this Node has a last child; otherwise returns a None.

use slab_tree::tree::TreeBuilder;

let mut tree = TreeBuilder::new().with_root(1).build();

let root = tree.root().expect("root doesn't exist?");

assert!(root.last_child().is_none());

pub fn ancestors(&self) -> Ancestors<'a, T>[src]

Returns a Iterator over the given Node's ancestors. Each call to Iterator::next() returns a NodeRef pointing to the current Node's parent.

use slab_tree::tree::TreeBuilder;

let mut tree = TreeBuilder::new().with_root(1).build();

let leaf_id = tree.root_mut().expect("root doesn't exist?")
    .append(2)
    .append(3)
    .append(4)
    .node_id();

let leaf = tree.get(leaf_id).unwrap();

let values = [3, 2, 1];
for (i, ancestor) in leaf.ancestors().enumerate() {
    assert_eq!(ancestor.data(), &values[i]);
}

pub fn children(&self) -> NextSiblings<'a, T>[src]

Returns a Iterator over the given Node's children. Each call to Iterator::next() returns a NodeRef pointing to the next child of the given Node.

use slab_tree::tree::TreeBuilder;

let mut tree = TreeBuilder::new().with_root(1).build();

let mut root = tree.root_mut().expect("root doesn't exist?");
root.append(2);
root.append(3);
root.append(4);

let root = root.as_ref();

let values = [2, 3, 4];
for (i, child) in root.children().enumerate() {
    assert_eq!(child.data(), &values[i]);
}

pub fn traverse_pre_order(&self) -> PreOrder<'a, T>[src]

Depth-first pre-order traversal.

use slab_tree::tree::TreeBuilder;

let mut tree = TreeBuilder::new().with_root(0i64).build();
let root_id = tree.root().unwrap().node_id();
let one_id = tree.get_mut(root_id).unwrap().append(1).node_id();
tree.get_mut(one_id).unwrap().append(2);
tree.get_mut(one_id).unwrap().append(3);
tree.get_mut(root_id).unwrap().append(4);
let pre_order = tree.root().unwrap().traverse_pre_order()
    .map(|node_ref| node_ref.data().clone()).collect::<Vec<i64>>();
assert_eq!(pre_order, vec![0, 1, 2, 3, 4]);

pub fn traverse_post_order(&self) -> PostOrder<'a, T>[src]

Depth-first post-order traversal.

use slab_tree::tree::TreeBuilder;

let mut tree = TreeBuilder::new().with_root(0i64).build();
let root_id = tree.root().unwrap().node_id();
let one_id = tree.get_mut(root_id).unwrap().append(1).node_id();
tree.get_mut(one_id).unwrap().append(2);
tree.get_mut(one_id).unwrap().append(3);
tree.get_mut(root_id).unwrap().append(4);
let post_order = tree.root().unwrap().traverse_post_order()
    .map(|node_ref| node_ref.data().clone()).collect::<Vec<i64>>();
assert_eq!(post_order, vec![2, 3, 1, 4, 0]);

pub fn traverse_level_order(&self) -> LevelOrder<'a, T>[src]

Depth-first level-order traversal.

use slab_tree::tree::TreeBuilder;

let mut tree = TreeBuilder::new().with_root(0i64).build();
let root_id = tree.root().unwrap().node_id();
let one_id = tree.get_mut(root_id).unwrap().append(1).node_id();
tree.get_mut(one_id).unwrap().append(2);
tree.get_mut(one_id).unwrap().append(3);
tree.get_mut(root_id).unwrap().append(4);
let level_order = tree.root().unwrap().traverse_level_order()
    .map(|node_ref| node_ref.data().clone()).collect::<Vec<i64>>();
assert_eq!(level_order, vec![0, 1, 4, 2, 3]);

Auto Trait Implementations

impl<'a, T> RefUnwindSafe for NodeRef<'a, T> where
    T: RefUnwindSafe

impl<'a, T> Send for NodeRef<'a, T> where
    T: Sync

impl<'a, T> Sync for NodeRef<'a, T> where
    T: Sync

impl<'a, T> Unpin for NodeRef<'a, T>

impl<'a, T> UnwindSafe for NodeRef<'a, T> where
    T: RefUnwindSafe

Blanket Implementations

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

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

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

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.