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

Updated tree_sort.rs #96

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 17 additions & 3 deletions src/sorting/tree_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// Define the structure for the BST Node
#[derive(Debug)]
struct Node {

Check warning on line 6 in src/sorting/tree_sort.rs

View workflow job for this annotation

GitHub Actions / Check

struct `Node` is never constructed

Check warning on line 6 in src/sorting/tree_sort.rs

View workflow job for this annotation

GitHub Actions / Test Suite

struct `Node` is never constructed
key: i32,
left: Option<Rc<RefCell<Node>>>,
right: Option<Rc<RefCell<Node>>>,
Expand All @@ -12,7 +12,8 @@
pub struct TreeSort;

// Function to create a new BST Node
// This function returns a reference-counted pointer to a newly created Node wrapped in a RefCell
fn new_node(key: i32) -> Rc<RefCell<Node>> {

Check warning on line 16 in src/sorting/tree_sort.rs

View workflow job for this annotation

GitHub Actions / Check

function `new_node` is never used

Check warning on line 16 in src/sorting/tree_sort.rs

View workflow job for this annotation

GitHub Actions / Test Suite

function `new_node` is never used
Rc::new(RefCell::new(Node {
key,
left: None,
Expand All @@ -20,43 +21,56 @@
}))
}

// Function to insert a new Node with given key in BST
// Function to insert a new Node with the given key in the BST
// Recursively finds the correct position for the new key and inserts it
// If the tree is empty, it creates and returns a new Node
fn insert(node: &Option<Rc<RefCell<Node>>>, key: i32) -> Option<Rc<RefCell<Node>>> {

Check warning on line 27 in src/sorting/tree_sort.rs

View workflow job for this annotation

GitHub Actions / Check

function `insert` is never used

Check warning on line 27 in src/sorting/tree_sort.rs

View workflow job for this annotation

GitHub Actions / Test Suite

function `insert` is never used
match node {
Some(current) => {
let mut current_borrow = current.borrow_mut();
match key.cmp(&current_borrow.key) {
// If the key is less, insert in the left subtree
std::cmp::Ordering::Less => {
current_borrow.left = insert(&current_borrow.left, key);
}
// If the key is greater, insert in the right subtree
std::cmp::Ordering::Greater => {
current_borrow.right = insert(&current_borrow.right, key);
}
// If the key is equal, do nothing (BST doesn't allow duplicates)
std::cmp::Ordering::Equal => {}
}
drop(current_borrow);
Some(Rc::clone(current))
}
// If the node is None, create a new Node with the given key
None => Some(new_node(key)),
}
}

// Function to perform inorder traversal and store in a vector
// Function to perform an inorder traversal of the BST and store the keys in a vector
// Inorder traversal ensures that the keys are stored in sorted order
fn store_sorted(node: &Option<Rc<RefCell<Node>>>, arr: &mut Vec<i32>) {

Check warning on line 53 in src/sorting/tree_sort.rs

View workflow job for this annotation

GitHub Actions / Check

function `store_sorted` is never used

Check warning on line 53 in src/sorting/tree_sort.rs

View workflow job for this annotation

GitHub Actions / Test Suite

function `store_sorted` is never used
if let Some(current) = node {
// Traverse the left subtree
store_sorted(&current.borrow().left, arr);
// Store the current node's key
arr.push(current.borrow().key);
// Traverse the right subtree
store_sorted(&current.borrow().right, arr);
}
}

// Function to sort an array using Tree Sort
// Builds a BST from the array and then performs an inorder traversal to sort it
fn tree_sort(arr: &mut Vec<i32>) {

Check warning on line 66 in src/sorting/tree_sort.rs

View workflow job for this annotation

GitHub Actions / Check

function `tree_sort` is never used
let mut root = None;
// Insert all elements of the array into the BST
for &item in arr.iter() {
root = insert(&root, item);
}
arr.clear();
arr.clear(); // Clear the array to store the sorted elements
// Perform an inorder traversal to store sorted elements back in the array
store_sorted(&root, arr);
}

Expand Down
Loading