Sha256: 052a9ebd32ba6223715261986968681d63e148d10b18188fe07dc773d870c352

Contents?: true

Size: 837 Bytes

Versions: 1

Compression:

Stored size: 837 Bytes

Contents

# -*- coding: utf-8 -*-
module TreeVisitor
  #
  # Clone a tree_node, nodes are duplicated.
  # Node content are not duplicated!
  #
  class CloneTreeNodeVisitor < BasicTreeNodeVisitor

    #
    # Contains the cloned tree node after the visit
    #
    attr_reader :cloned_root

    def initialize
      super
      @cloned_root = nil
      @stack = []
    end

    def enter_tree_node( tree_node )
      if @stack.empty?
        cloned_tree_node = TreeNode.new( tree_node.content )
        @cloned_root = cloned_tree_node
      else
        cloned_tree_node = TreeNode.new( tree_node.content, @stack.last )
      end
      @stack.push( cloned_tree_node )
    end

    def exit_tree_node( tree_node )
      @stack.pop
    end

    def visit_leaf_node( leaf_node )
      LeafNode.new( leaf_node.content, @stack.last )
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
treevisitor-0.1.6 lib/treevisitor/visitors/clone_tree_node_visitor.rb