Ruby Program to Implement Tree Sort

Ruby Program to Implement Tree Sort

Sorting is one of the core concepts in programming because it allows us to organize data efficiently, making tasks like searching, analyzing, and processing much easier. Among various sorting techniques, Tree Sort stands out as a method that leverages the Binary Search Tree (BST) data structure. Tree Sort builds a BST from the dataset, and then an in-order traversal of the tree produces the sorted array. This makes the algorithm elegant and closely tied to fundamental data structures.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Tree Sort is particularly useful when we want a naturally ordered structure during the sorting process or when working with datasets where insertions and searches are frequent. Learning Tree Sort in Ruby not only teaches beginners about sorting but also provides a practical introduction to trees and recursion. In this article, we will explore multiple ways to implement Tree Sort in Ruby, including standard recursive approaches, iterative insertion, and optimized versions. By the end, beginners will clearly understand how Tree Sort works and how to apply it effectively.

Program 1: Basic Tree Sort Using Recursive BST Insertion

This program demonstrates a simple Tree Sort implementation using a recursive binary search tree to sort a predefined array of integers.

# Program 1: Basic Tree Sort using recursive BST
class Node

  attr_accessor :data, :left, :right

  def initialize(data)

    @data = data
    @left = nil
    @right = nil

  end

end

def insert(root, data)

  return Node.new(data) if root.nil?

  if data < root.data
    root.left = insert(root.left, data)
  else
    root.right = insert(root.right, data)
  end

  root

end

def inorder_traversal(root, result)

  return if root.nil?

  inorder_traversal(root.left, result)

  result << root.data

  inorder_traversal(root.right, result)

end

def tree_sort(array)

  root = nil

  array.each { |num| root = insert(root, num) }

  result = []

  inorder_traversal(root, result)

  result

end

numbers = [5, 2, 9, 1, 5, 6]

sorted_array = tree_sort(numbers)

puts "Sorted array: #{sorted_array}"

In this program, we first build a BST by recursively inserting each element. The in-order traversal of the BST then produces a sorted array. Beginners can see that Tree Sort combines the concepts of trees and recursion to create a naturally ordered structure from an unsorted array.

Program 2: Tree Sort with Iterative BST Insertion

Instead of recursive insertion, Tree Sort can also use iterative insertion into the BST. This approach avoids deep recursion for large datasets.

# Program 2: Tree Sort using iterative BST insertion
class Node

  attr_accessor :data, :left, :right

  def initialize(data)

    @data = data
    @left = nil
    @right = nil

  end

end

def insert_iterative(root, data)

  new_node = Node.new(data)

  return new_node if root.nil?

  current = root
  parent = nil

  while current

    parent = current
    current = data < current.data ? current.left : current.right

  end

  if data < parent.data
    parent.left = new_node
  else
    parent.right = new_node
  end

  root

end

def inorder_traversal(root, result)

  return if root.nil?

  inorder_traversal(root.left, result)

  result << root.data

  inorder_traversal(root.right, result)

end

def tree_sort_iterative(array)

  root = nil

  array.each { |num| root = insert_iterative(root, num) }

  result = []

  inorder_traversal(root, result)

  result  # return the sorted result

end

numbers = [12, 4, 7, 9, 2, 5]
sorted_array = tree_sort_iterative(numbers)

puts "Sorted array (iterative insertion): #{sorted_array}"

Using iterative insertion avoids potential stack overflow for large arrays, and beginners can understand how BST insertion logic works without recursion while still producing the same sorted result.

Program 3: Tree Sort for Floating-Point Numbers

Tree Sort is not limited to integers. It can handle floating-point numbers as long as comparisons are defined.

# Program 3: Tree Sort for floating-point numbers
class Node

  attr_accessor :data, :left, :right

  def initialize(data)

    @data = data
    @left = nil
    @right = nil

  end

end

def insert(root, data)

  return Node.new(data) if root.nil?

  if data < root.data
    root.left = insert(root.left, data)
  else
    root.right = insert(root.right, data)
  end

  root

end

def inorder_traversal(root, result)

  return if root.nil?

  inorder_traversal(root.left, result)

  result << root.data

  inorder_traversal(root.right, result)

end

def tree_sort(array)

  root = nil

  array.each { |num| root = insert(root, num) }

  result = []

  inorder_traversal(root, result)

  result

end

numbers = [3.4, 1.2, 5.7, 2.1, 0.5]

sorted_array = tree_sort(numbers)

puts "Sorted floats: #{sorted_array}"

Since Tree Sort relies on comparisons rather than arithmetic operations, it works naturally with floating-point numbers. Beginners can see that Tree Sort is versatile and applicable to various numeric datasets.

Program 4: Optimized Tree Sort Using Balanced BST

A common optimization in Tree Sort is using a balanced BST to ensure O(n log n) time complexity instead of the worst-case O(n²) for unbalanced trees. This can be done by first sorting the array partially and then inserting elements to keep the tree balanced.

# Program 4: Optimized Tree Sort with balanced BST
class Node

  attr_accessor :data, :left, :right

  def initialize(data)

    @data = data
    @left = nil
    @right = nil

  end

end

def inorder_traversal(root, result)

  return if root.nil?

  inorder_traversal(root.left, result)

  result << root.data

  inorder_traversal(root.right, result)

end

def build_balanced_bst(array, start_idx, end_idx)

  return nil if start_idx > end_idx

  mid = (start_idx + end_idx) / 2

  node = Node.new(array[mid])
  node.left = build_balanced_bst(array, start_idx, mid - 1)
  node.right = build_balanced_bst(array, mid + 1, end_idx)

  node

end

def tree_sort_balanced(array)

  sorted_array = array.sort
  root = build_balanced_bst(sorted_array, 0, sorted_array.length - 1)
  result = []

  inorder_traversal(root, result)

  result

end

numbers = [10, 20, 5, 15, 25, 2]

sorted_array = tree_sort_balanced(numbers)

puts "Optimized balanced BST sorted array: #{sorted_array}"

By constructing a balanced BST, the algorithm avoids skewed trees that can degrade performance. Beginners can understand that careful structuring of the BST improves efficiency while maintaining the simplicity of in-order traversal for sorting.

Frequently Asked Questions (FAQ)

Tree Sort can raise many questions for beginners. Here are some common ones:

Q1: What is the time complexity of Tree Sort?
The average time complexity is O(n log n) when using a balanced BST, but the worst case for an unbalanced BST is O(n²).

Q2: Is Tree Sort stable?
No, standard Tree Sort is not stable because equal elements may change their relative order during BST insertion.

Q3: Can Tree Sort handle negative numbers and floating-point numbers?
Yes, as long as comparisons can be made between elements, Tree Sort works for negative numbers and floating-point numbers.

Q4: Why use Tree Sort over other sorting algorithms?
Tree Sort is useful when you want a naturally ordered tree structure during sorting or when frequent insertions and deletions are needed while maintaining order.

Q5: How does Tree Sort relate to Binary Search Trees?
Tree Sort builds a BST from the dataset and uses in-order traversal to produce the sorted array, demonstrating a practical use of BSTs in sorting.

Conclusion

In this article, we explored how to implement Tree Sort in Ruby using recursive and iterative BST insertion, handling floating-point numbers, and optimizing with a balanced BST. Tree Sort is a versatile algorithm that combines the concepts of trees and sorting, making it both educational and practical. By practicing these examples, beginners will understand BST insertion, in-order traversal, and how careful tree structure can improve efficiency. Tree Sort is a valuable tool for sorting datasets while also learning about binary search trees and recursion.

Scroll to Top