Sha256: 518830ea648a0abc8c5109df6456dd36e42e0310fd7d071d39d77efc2642837a

Contents?: true

Size: 694 Bytes

Versions: 5

Compression:

Stored size: 694 Bytes

Contents

# -*- coding: utf-8 -*-
module TreeVisitor
  #
  # Prints TreeNode names indenting according to depth
  #
  class PrintTreeNodeVisitor # < BasicTreeNodeVisitor

    def initialize
      @depth = 0
    end

    def enter_node( tree_node )
      str = ""
      (0...@depth).step {
        str << " |-"
      }

      if @depth == 0
        puts str + tree_node.name.to_s
      else
        puts str + tree_node.name.to_s
      end
      @depth += 1
    end

    def exit_node( tree_node )
      @depth -= 1
    end

    def visit_leaf( leaf_node )
      str = ""
      (0...@depth-1).step {
        str << " |-"
      }
      str << " |  "
      puts str + leaf_node.name.to_s
    end

  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
tree.rb-0.3.0 lib/treevisitor/visitors/print_tree_node_visitor.rb
treevisitor-0.2.3 lib/treevisitor/visitors/print_tree_node_visitor.rb
treevisitor-0.2.2 lib/treevisitor/visitors/print_tree_node_visitor.rb
treevisitor-0.2.1 lib/treevisitor/visitors/print_tree_node_visitor.rb
treevisitor-0.2.0 lib/treevisitor/visitors/print_tree_node_visitor.rb