pub struct Node<K, V> { /* private fields */ }Expand description
A single building block to represent an element within a Tree. Itself can contain a list of more Nodes. It should eventually be part of a Tree and it shouldn’t be necessary to keep it around on its own.
Implementations§
Source§impl<K, V> Node<K, V>where
K: PartialEq,
impl<K, V> Node<K, V>where
K: PartialEq,
Sourcepub fn new(key: K, value: V) -> Self
pub fn new(key: K, value: V) -> Self
Creates a new Node.
§Examples
use generic_tree::Node;
let node = Node::new("some_key".to_owned(), 42);
assert_eq!(node.key(), "some_key");
assert_eq!(node.value(), &42);Sourcepub fn add_child(&mut self, child: Node<K, V>) -> Option<Box<Node<K, V>>>
pub fn add_child(&mut self, child: Node<K, V>) -> Option<Box<Node<K, V>>>
Add a child Node to the current Node. If there already is a child with the same key, that
child will be returned.
§Examples
use generic_tree::Node;
let mut parent = Node::new("parent".to_owned(), 2);
let child = Node::new("child".to_owned(), 3);
assert!(parent.add_child(child).is_none());
let child = Node::new("child".to_owned(), 4);
let prev_child = parent.add_child(child).unwrap();
assert_eq!(prev_child.value(), &3);Sourcepub fn add_child_at_path<Q>(
&mut self,
path: &[Q],
child: Node<K, V>,
) -> Result<Option<Box<Node<K, V>>>, Error>where
Q: PartialEq<K>,
pub fn add_child_at_path<Q>(
&mut self,
path: &[Q],
child: Node<K, V>,
) -> Result<Option<Box<Node<K, V>>>, Error>where
Q: PartialEq<K>,
Add a child Node at a specific descendent path. If there was already such a child, it will be returned.
§Errors
Will return Err if path can’t be resolved.
§Examples
use generic_tree::Node;
let mut grandparent = Node::new("grandparent".to_owned(), 1);
let mut parent = Node::new("parent".to_owned(), 2);
grandparent.add_child(parent);
// Tree structure:
// grandparent -> parent
assert!(grandparent.get_child(&["parent", "child"]).is_err());
let child = Node::new("child".to_owned(), 3);
assert!(grandparent.add_child_at_path(&["parent"], child).is_ok());
assert!(grandparent.get_child(&["parent", "child"]).is_ok());Sourcepub fn get_child<Q>(&self, path: &[Q]) -> Result<&Node<K, V>, Error>where
Q: PartialEq<K>,
pub fn get_child<Q>(&self, path: &[Q]) -> Result<&Node<K, V>, Error>where
Q: PartialEq<K>,
Get a child node based on a list of keys.
§Errors
Will return Err if path can’t be resolved.
§Examples
use generic_tree::Node;
let mut grandparent = Node::new("grandparent".to_owned(), 1);
let mut parent = Node::new("parent".to_owned(), 2);
let child = Node::new("child".to_owned(), 3);
assert!(parent.add_child(child).is_none());
assert!(grandparent.add_child(parent).is_none());
// Tree structure:
// grandparent -> parent -> child
assert!(grandparent.get_child(&["parent", "child"]).is_ok());Sourcepub fn get_mut_child<Q>(&mut self, path: &[Q]) -> Result<&mut Node<K, V>, Error>where
Q: PartialEq<K>,
pub fn get_mut_child<Q>(&mut self, path: &[Q]) -> Result<&mut Node<K, V>, Error>where
Q: PartialEq<K>,
Sourcepub fn remove_child<Q>(&mut self, path: &[Q]) -> Result<Box<Node<K, V>>, Error>where
Q: PartialEq<K>,
pub fn remove_child<Q>(&mut self, path: &[Q]) -> Result<Box<Node<K, V>>, Error>where
Q: PartialEq<K>,
Remove a child node based on a list of keys. A boxed Node is returned if found, None
otherwise.
§Errors
Will return Err if path can’t be resolved.
§Examples
use generic_tree::Node;
let mut grandparent = Node::new("grandparent".to_owned(), 1);
let mut parent = Node::new("parent".to_owned(), 2);
let child = Node::new("child".to_owned(), 3);
parent.add_child(child);
grandparent.add_child(parent);
// Tree structure:
// grandparent -> parent -> child
assert!(grandparent.remove_child(&["parent", "child"]).is_ok());
assert!(grandparent.remove_child(&["parent", "child"]).is_err());Sourcepub fn value(&self) -> &V
pub fn value(&self) -> &V
Get a reference the value contained within the node.
§Examples
use generic_tree::Node;
let node = Node::new("some_key".to_owned(), 42);
assert_eq!(node.value(), &42);