Logo

dev-resources.site

for different kinds of informations.

Tree data structures in Rust with tree-ds (#2: Tree Operations)

Published at
6/29/2024
Categories
rust
algorithms
datastructures
tree
Author
clementwanjau
Author
13 person written this
clementwanjau
open
Tree data structures in Rust with tree-ds (#2: Tree Operations)

In the previous part, we explored how to get started with the tree-ds crate to work with tree data structures in rust. In this part, we are going to explore the various features and operations offered by the Tree struct.

Exploring the Features

tree-ds offers a variety of functionalities for working with your tree. Here are some key features:

  • Node Insertion: Insert nodes as descendants of a particular node. This is achieved by the add_node method:
use tree_ds::prelude::*;

let mut tree = Tree::new(Some("Tree Name"));
tree.add_node(Node::new(1, Some(20_000)), None)?;
Enter fullscreen mode Exit fullscreen mode

The above snippet creates a tree and adds a root node.

  • Node(s) Removal: Remove nodes based on their node id. You can also specify how you want to handle the children of the node in question. If you decide to remove the node and its children, it is considered a pruning action. You can also decide to just remove the single node and attach the children to their grandparent.
use tree_ds::prelude::*;

let mut tree = Tree:new(Some("Tree name"));
let node_id = tree.add_node(Node::new(1, Some(20_000)), None)?;
tree.remove_node(&node_id, NodeRemovalStrategy::RemoveNodeAndChildren)?;
Enter fullscreen mode Exit fullscreen mode
  • Traversal: The crate provides methods for in-order, pre-order, and post-order tree traversals, allowing you to visit and process nodes in a specific order.
use tree_ds::prelude::{Node, Tree, TraversalStrategy};

let mut tree: Tree<i32, i32> = Tree::new(Some("Sample Tree"));
let node_1 = tree.add_node(Node::new(1, Some(2)), None)?;
let node_2 = tree.add_node(Node::new(2, Some(3)), Some(&node_1))?;
let node_3 = tree.add_node(Node::new(3, Some(6)), Some(&node_2))?;

let preordered_nodes = tree.traverse(TraversalStrategy::PreOrder, &node_1)?;
Enter fullscreen mode Exit fullscreen mode
  • Getting a whole subsection of a tree. A subsection is a list of nodes that are descendants to a node. Think of it as a branch with nodes.
use tree_ds::prelude::{Node, Tree}

let mut tree: Tree<i32, i32> = Tree::new(Some("Sample Tree"));

let node_1 = tree.add_node(Node::new(1, Some(2)), None)?;
let node_2 = tree.add_node(Node::new(2, Some(3)), Some(&node_1))?;
let node_3 = tree.add_node(Node::new(3, Some(6)), Some(&node_2))?;

let subsection = tree.get_subtree(&node_2, None)?;
Enter fullscreen mode Exit fullscreen mode
  • Grafting: Adding whole section of a tree or another tree onto a tree at a specified node.
use tree_ds::prelude::{Node, Tree, SubTree};

let mut tree: Tree<i32, i32> = Tree::new(Some("Sample Tree"));
let node_id = tree.add_node(Node::new(1, Some(2)), None)?;
let mut subtree = SubTree::new(Some("Sample Tree"));
let node_2 = subtree.add_node(Node::new(2, Some(3)), None)?;  subtree.add_node(Node::new(3, Some(6)), Some(&node_2))?;
tree.add_subtree(&node_id, subtree)?;
Enter fullscreen mode Exit fullscreen mode
  • Searching: Search for nodes based on their value and retrieve them if found.
  • Enumerating the children and the ancestors of a node.
  • Getting the height, degree and depth of a node.

These features, along with many more, make tree-ds a well-equipped solution for building and manipulating trees in your Rust projects.

Conclusion

In this section we went through some of the features of the tree data structure offered by the tree-ds crate. The crate is actively maintained so it is a good option to consider when working with trees in rust.

Read The Next Post

tree Article's
30 articles in total
Favicon
House robber III
Favicon
Inorder traversal of a binary tree
Favicon
Comprehensive Tree Care Solutions in Gig Harbor and Tacoma with Pablo Tree Services
Favicon
Tree data structures in Rust with tree-ds (#2: Tree Operations)
Favicon
The Benefits of Hiring Professional Tree Trimmers in Christchurch
Favicon
Tree data structures in Rust with tree-ds (#1: Getting Started)
Favicon
DFS Traversal Guide: Easy way to remember DFS Traversel Path
Favicon
Chain - a Goofy, Functional, Tree-backed List
Favicon
Demystifying Tree Lopping vs. Tree Chipping: Which is Right for Your Landscape?
Favicon
Ergonomic Trees in Go
Favicon
Generating Dynamic Breadcrumb Menus Using Tree Table & Recursive CTE
Favicon
Types of decision tree in machine learning
Favicon
Tree Service Tips: Keeping Your Property Safe in Tinley Park
Favicon
What is tree data structure? 🌳
Favicon
Golang multinode tree with parallel search
Favicon
Implementing Nested Filters using React and Tree Data Structure
Favicon
814. Binary Tree Pruning
Favicon
A hierarchical tree component for React in Typescript
Favicon
C++ - Basic Tree Traversal(Recursive vs Queue)
Favicon
C++ - Basic Tree Traversal(Recursive vs Stack)
Favicon
Generate files and folder structures of your code
Favicon
Converting materialized paths into a tree with generics: a Golang kata
Favicon
wishing3 - if you'd 3 wishes, what'd they be?
Favicon
Represent a directory tree in a Github README.md
Favicon
Segment Trees - Part I
Favicon
A Nibble of Quadtrees in Rust
Favicon
What is AST?
Favicon
Finding all children of a node in a tree
Favicon
5 Reasons You Need Tree Doctor to Keep Your Plants Healthy
Favicon
Binary Tree Pruning

Featured ones: