Ruby
Official website
The official website for the Ruby programming language is: https://www.ruby-lang.org/
Static typing
No, Ruby is not statically typed; it is a dynamically typed language. In Ruby, type checking happens at runtime, meaning that variables can reference objects of any type without explicit type declarations, and type errors are typically detected only when the problematic code is executed.
Example code
Below is a complete Ruby implementation of a binary search tree (BST) along with a method to find (search for) a value in the tree. The algorithm uses recursion to traverse the tree according to the BST property. You can run this code directly in a Ruby environment.
# Define a Node class for the binary search tree.
class Node
attr_accessor :value, :left, :right
def initialize(value)
@value = value
@left = nil
@right = nil
end
end
# Define a BST class that holds the root node.
class BinarySearchTree
attr_accessor :root
def initialize
@root = nil
end
# Insert a new value into the BST.
def insert(value)
@root = insert_recursively(@root, value)
end
# Recursive method to insert a new node.
def insert_recursively(current_node, value)
if current_node.nil?
return Node.new(value)
end
if value < current_node.value
current_node.left = insert_recursively(current_node.left, value)
elsif value > current_node.value
current_node.right = insert_recursively(current_node.right, value)
else
# Duplicate value, no insertion is done.
puts "Value #{value} already exists in the tree."
end
current_node
end
# Find a value in the BST. Returns the node if found, otherwise returns nil.
def find(value)
find_recursively(@root, value)
end
# Recursive search method.
def find_recursively(current_node, value)
return nil if current_node.nil?
if value == current_node.value
return current_node
elsif value < current_node.value
return find_recursively(current_node.left, value)
else
return find_recursively(current_node.right, value)
end
end
end
# Example Usage:
# Create a new binary search tree.
bst = BinarySearchTree.new
# Insert some values.
[15, 10, 20, 8, 12, 17, 25].each { |value| bst.insert(value) }
# Search for a value in the BST.
search_value = 17
found_node = bst.find(search_value)
if found_node
puts "Found value #{found_node.value} in the binary search tree."
else
puts "Value #{search_value} not found in the binary search tree."
end
Explanation
- Node Class:
- Each node stores a
valueand has pointers to itsleftandrightchild nodes.
- Each node stores a
- Binary Search Tree Class:
- The BST holds a reference to the
rootnode. - The
insertmethod inserts values maintaining BST properties, using a helper recursive method. - The
findmethod searches for a given value by recursively comparing the target value with the current node’s value. It follows the left subtree if the value is less and the right subtree if it’s greater.
- The BST holds a reference to the
- Execution:
- The code block creates a BST, inserts several values, and then searches for a specific value, reporting whether it was found.
This implementation is complete, runnable, and demonstrates the essential operations of a binary search tree in Ruby.