Class: TreeRb::PrintTreeNodeVisitor

Inherits:
Object
  • Object
show all
Defined in:
lib/tree_rb/visitors/print_tree_node_visitor.rb

Overview

Prints TreeNode names indenting according to depth

Instance Method Summary collapse

Constructor Details

#initializePrintTreeNodeVisitor

< BasicTreeNodeVisitor



8
9
10
# File 'lib/tree_rb/visitors/print_tree_node_visitor.rb', line 8

def initialize
  @depth = 0
end

Instance Method Details

#cannot_enter_node(tree_node, error) ⇒ Object

called when the tree node is not accessible or an exception is raise when the node is accessed



33
34
# File 'lib/tree_rb/visitors/print_tree_node_visitor.rb', line 33

def cannot_enter_node( tree_node, error)
end

#enter_node(tree_node) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/tree_rb/visitors/print_tree_node_visitor.rb', line 12

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

#exit_node(tree_node) ⇒ Object



26
27
28
# File 'lib/tree_rb/visitors/print_tree_node_visitor.rb', line 26

def exit_node( tree_node )
  @depth -= 1
end

#visit_leaf(leaf_node) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/tree_rb/visitors/print_tree_node_visitor.rb', line 36

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