Skip to content

Instantly share code, notes, and snippets.

View sachanganesh's full-sized avatar

Sachandhan Ganesh sachanganesh

View GitHub Profile
@sachanganesh
sachanganesh / tree_test.rs
Created June 2, 2020 18:04
arena-allocated preorder traversal in rust (tree_test)
fn main() {
let mut tree = Tree::new();
let a = tree.add_node(TreeNode::new(4, None, None));
let b = tree.add_node(TreeNode::new(5, None, None));
let c = tree.add_node(TreeNode::new(2, Some(a), Some(b)));
let d = tree.add_node(TreeNode::new(3, None, None));
let e = tree.add_node(TreeNode::new(1, Some(c), Some(d)));
@sachanganesh
sachanganesh / tree_iter.rs
Created June 2, 2020 18:04
arena-allocated preorder traversal in rust (tree_iter)
pub struct PreorderIter {
stack: Vec<TreeIndex>
}
impl PreorderIter {
pub fn new(root: Option<TreeIndex>) -> Self {
if let Some(index) = root {
PreorderIter {
stack: vec![index]
}
@sachanganesh
sachanganesh / tree.rs
Created June 2, 2020 18:02
arena-allocated preorder traversal in rust (tree)
pub struct Tree {
arena: Vec<Option<TreeNode>>,
root: Option<TreeIndex>
}
impl Tree {
pub fn new() -> Self {
Tree {
arena: Vec::new(),
root: None
@sachanganesh
sachanganesh / tree_node.rs
Created June 2, 2020 18:00
arena-allocated preorder traversal in rust (tree_node)
pub type TreeIndex = usize;
pub struct TreeNode {
pub value: usize,
pub left: Option<TreeIndex>,
pub right: Option<TreeIndex>
}
impl TreeNode {
pub fn new(
@sachanganesh
sachanganesh / tree_iter.rs
Created June 2, 2020 17:55
python-ported mutable preorder traversal in rust (tree_iter)
pub struct PreorderIter<'a> {
stack: Vec<&'a mut TreeNode>
}
impl<'a> PreorderIter<'a> {
pub fn new(root: Option<&'a mut TreeNode>) -> Self {
if let Some(node) = root {
PreorderIter {
stack: vec![node]
}
@sachanganesh
sachanganesh / tree.rs
Created June 2, 2020 17:54
python-ported mutable preorder traversal in rust (tree)
pub struct Tree {
root: Option<TreeNode>
}
impl Tree {
pub fn new(root: Option<TreeNode>) -> Self {
Tree {
root
}
}
@sachanganesh
sachanganesh / tree_test.rs
Created June 2, 2020 17:53
python-ported immutable preorder traversal with lifetimes in rust (tree_test)
fn main() {
let a = TreeNode::new(4, None, None);
let b = TreeNode::new(5, None, None);
let c = TreeNode::new(2, Some(Box::from(a)), Some(Box::from(b)));
let d = TreeNode::new(3, None, None);
let e = TreeNode::new(1, Some(Box::from(c)), Some(Box::from(d)));
let tree = Tree::new(Some(e));
@sachanganesh
sachanganesh / tree_iter.rs
Created June 2, 2020 17:49
python-ported immutable preorder traversal with lifetimes in rust (tree_iter)
pub struct PreorderIter<'a> { // explicitly annotate the lifetime of the struct
stack: Vec<&'a TreeNode> // specify that the borrow lives for as long as the struct lives
}
impl<'a> PreorderIter<'a> { // declare the lifetime and apply as lifetime of the struct
pub fn new(root: Option<&'a TreeNode>) -> Self { // annotate lifetime of the borrow
if let Some(node) = root {
PreorderIter {
stack: vec![node]
}
@sachanganesh
sachanganesh / tree_iter.rs
Created June 2, 2020 17:45
python-ported immutable preorder traversal in rust (tree_iter)
pub struct PreorderIter {
stack: Vec<&TreeNode>
}
impl PreorderIter {
pub fn new(root: Option<&TreeNode>) -> Self {
if let Some(node) = root {
PreorderIter {
stack: vec![node]
}
@sachanganesh
sachanganesh / tree.rs
Last active June 2, 2020 17:45
python-ported immutable preorder traversal in rust (tree)
pub struct Tree {
root: Option<TreeNode>
}
impl Tree {
pub fn new(root: Option<TreeNode>) -> Self {
Tree {
root
}
}