How to resolve the algorithm Tree traversal step by step in the Ruby programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Tree traversal step by step in the Ruby programming language

Table of Contents

Problem Statement

Implement a binary tree where each node carries an integer,   and implement:

Use those traversals to output the following tree: The correct output should look like this:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Tree traversal step by step in the Ruby programming language

The provided code is a Ruby implementation of a binary tree data structure and various traversal algorithms. A binary tree is a hierarchical data structure that consists of nodes, where each node has at most two child nodes.

  1. Struct Definition: The code starts by defining a BinaryTreeNode struct, which represents a node in the binary tree. It has three fields: value (the value stored in the node), left (the left child node), and right (the right child node).

  2. from_array Class Method: The from_array class method is used to create a binary tree from a nested list representation. It takes a nested list as an argument. The nested list represents the structure of the tree, where each sublist represents a node and its children. The method recursively constructs the tree by creating nodes for each sublist, using the value, left, and right fields of the BinaryTreeNode struct.

  3. walk_nodes Instance Method: The walk_nodes instance method performs a tree traversal using a specified order. It takes an array of symbols (order) as an argument and a block to execute on each node. The order array specifies the order in which the nodes should be visited. The method uses case statements to determine the node to visit based on the order and recursively calls itself for the child nodes.

  4. Traversal Methods: The code defines several traversal methods based on different orders:

    • each_preorder: Performs a preorder traversal, visiting the root node first, followed by the left subtree, and then the right subtree.
    • each_inorder: Performs an inorder traversal, visiting the left subtree first, followed by the root node, and then the right subtree.
    • each_postorder: Performs a postorder traversal, visiting the left subtree first, followed by the right subtree, and then the root node.
    • each_levelorder: Performs a level-order traversal, visiting the nodes from left to right at each level.
  5. Example: The code creates a binary tree using the from_array method with a specific nested list representation. It then uses the traversal methods to print the elements of the tree in different orders.

The output of the BinaryTreeNode.instance_methods.select{|m| m=~/.+order/}.each do |mthd| block will be:

preorder  : 1 2 4 7 5 3 6 8 9 
inorder   : 4 2 7 1 5 3 8 6 9 
postorder : 4 7 2 5 1 8 6 9 3 
levelorder: 1 2 3 4 5 6 7 8 9 

Source code in the ruby programming language

BinaryTreeNode = Struct.new(:value, :left, :right) do
  def self.from_array(nested_list)
    value, left, right = nested_list
    if value 
      self.new(value, self.from_array(left), self.from_array(right))
    end
  end
 
  def walk_nodes(order, &block)
    order.each do |node|
      case node
      when :left  then left && left.walk_nodes(order, &block)
      when :self  then yield self
      when :right then right && right.walk_nodes(order, &block)
      end
    end
  end
 
  def each_preorder(&b)  walk_nodes([:self, :left, :right], &b) end
  def each_inorder(&b)   walk_nodes([:left, :self, :right], &b) end
  def each_postorder(&b) walk_nodes([:left, :right, :self], &b) end
 
  def each_levelorder
    queue = [self]
    until queue.empty?
      node = queue.shift
      yield node
      queue << node.left if node.left
      queue << node.right if node.right
    end
  end
end

root = BinaryTreeNode.from_array [1, [2, [4, 7], [5]], [3, [6, [8], [9]]]] 

BinaryTreeNode.instance_methods.select{|m| m=~/.+order/}.each do |mthd|
  printf "%-11s ", mthd[5..-1] + ':'
  root.send(mthd) {|node| print "#{node.value} "}
  puts
end


  

You may also check:How to resolve the algorithm Averages/Mode step by step in the BQN programming language
You may also check:How to resolve the algorithm Variable size/Set step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Primality by trial division step by step in the C programming language
You may also check:How to resolve the algorithm Narcissist step by step in the Ada programming language
You may also check:How to resolve the algorithm Input loop step by step in the Sidef programming language