Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added tree and btree data structure in rust lang #439

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions Rust/b_tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Define a binary tree structure in Rust
#[derive(Debug)]
struct BinaryTree<T> {
value: T,
left: Option<Box<BinaryTree<T>>>, // Left child
right: Option<Box<BinaryTree<T>>>, // Right child
}

// Implement basic functionality for the BinaryTree
impl<T> BinaryTree<T> {
// Create a new node (with no children)
pub fn new(value: T) -> Self {
BinaryTree {
value,
left: None,
right: None,
}
}

// Insert a left child
pub fn insert_left(&mut self, value: T) {
self.left = Some(Box::new(BinaryTree::new(value)));
}

// Insert a right child
pub fn insert_right(&mut self, value: T) {
self.right = Some(Box::new(BinaryTree::new(value)));
}
}

// Implement tree traversal methods
impl<T: std::fmt::Debug> BinaryTree<T> {
// Pre-order traversal (root, left, right)
pub fn preorder(&self) {
println!("{:?}", self.value); // Visit the root
if let Some(left) = &self.left {
left.preorder();
}
if let Some(right) = &self.right {
right.preorder();
}
}

// In-order traversal (left, root, right)
pub fn inorder(&self) {
if let Some(left) = &self.left {
left.inorder();
}
println!("{:?}", self.value); // Visit the root
if let Some(right) = &self.right {
right.inorder();
}
}

// Post-order traversal (left, right, root)
pub fn postorder(&self) {
if let Some(left) = &self.left {
left.postorder();
}
if let Some(right) = &self.right {
right.postorder();
}
println!("{:?}", self.value); // Visit the root
}
}

fn main() {
// Create the root of the binary tree
let mut root = BinaryTree::new(1);

// Insert children to the root
root.insert_left(2);
root.insert_right(3);

// Insert children to the left child of the root
if let Some(left_child) = root.left.as_mut() {
left_child.insert_left(4);
left_child.insert_right(5);
}

// Insert children to the right child of the root
if let Some(right_child) = root.right.as_mut() {
right_child.insert_left(6);
right_child.insert_right(7);
}

// Pre-order traversal
println!("Pre-order traversal:");
root.preorder();

// In-order traversal
println!("\nIn-order traversal:");
root.inorder();

// Post-order traversal
println!("\nPost-order traversal:");
root.postorder();
}
63 changes: 63 additions & 0 deletions Rust/tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Define a tree structure in Rust
#[derive(Debug)]
enum Tree<T> {
Node(T, Vec<Tree<T>>), // A node with a value and children
Leaf(T), // A leaf node with just a value
}

// Implementing basic functionality for the Tree
impl<T> Tree<T> {
// Create a new leaf node
pub fn new_leaf(value: T) -> Self {
Tree::Leaf(value)
}

// Create a new node with children
pub fn new_node(value: T, children: Vec<Tree<T>>) -> Self {
Tree::Node(value, children)
}

// Add a child to a node
pub fn add_child(&mut self, child: Tree<T>) {
if let Tree::Node(_, ref mut children) = self {
children.push(child);
} else {
println!("Cannot add a child to a leaf node.");
}
}
}

// Traversing the tree with a depth-first search (DFS)
impl<T: std::fmt::Debug> Tree<T> {
pub fn dfs(&self) {
match self {
Tree::Node(value, children) => {
println!("Node: {:?}", value);
for child in children {
child.dfs();
}
}
Tree::Leaf(value) => {
println!("Leaf: {:?}", value);
}
}
}
}

fn main() {
// Creating a tree with nodes and leaves
let mut root = Tree::new_node(1, vec![]); // Root node with value 1

let child1 = Tree::new_node(2, vec![
Tree::new_leaf(3), // A child node with two leaf children
Tree::new_leaf(4),
]);

let child2 = Tree::new_leaf(5); // A child with no children (leaf)

root.add_child(child1); // Add first child node
root.add_child(child2); // Add second child node

// Perform DFS traversal and print the tree
root.dfs();
}
Loading