@@ -108,11 +108,11 @@ class TreeNode
108108 # +content+ attribute for any non-unique node requirements.
109109 #
110110 # If you want to change the name, you probably want to call +rename+
111- # instead.
111+ # instead. Note that +name=+ is a protected method.
112112 #
113113 # @see content
114114 # @see rename
115- attr_reader :name
115+ attr_accessor :name
116116
117117 # @!attribute [rw] content
118118 # Content of this node. Can be +nil+. Note that there is no
@@ -217,7 +217,8 @@ def has_children?
217217 def initialize ( name , content = nil )
218218 raise ArgumentError , 'Node name HAS to be provided!' if name . nil?
219219
220- @name , @content = name , content
220+ @name = name
221+ @content = content
221222
222223 if name . is_a? ( Integer )
223224 warn StructuredWarnings ::StandardWarning ,
@@ -308,7 +309,7 @@ def marshal_load(dumped_tree_array)
308309 #
309310 # @return [String] A string representation of the node.
310311 def to_s
311- "Node Name: #{ @name } Content: #{ ( @content . to_s || '<Empty>' ) } Parent: #{ ( is_root? ? '<None>' : @parent . name . to_s ) } Children: #{ @children . length } Total Nodes: #{ size } "
312+ "Node Name: #{ @name } Content: #{ @content . to_s || '<Empty>' } Parent: #{ is_root? ? '<None>' : @parent . name . to_s } Children: #{ @children . length } Total Nodes: #{ size } "
312313 end
313314
314315 # @!group Structure Modification
@@ -445,14 +446,6 @@ def rename_child(old_name, new_name)
445446 @children_hash [ new_name ] . name = new_name
446447 end
447448
448- # Protected method to set the name of this node.
449- # This method should *NOT* be invoked by client code.
450- #
451- # @param [Object] new_name The node Name to set.
452- #
453- # @return [Object] The new name.
454- attr_writer :name
455-
456449 # Replaces the specified child node with another child node on this node.
457450 #
458451 # @param [Tree::TreeNode] old_child The child node to be replaced.
@@ -625,18 +618,18 @@ def [](name_or_index, num_as_name = false)
625618 # @return [Tree::TreeNode] this node, if a block if given
626619 # @return [Enumerator] an enumerator on this tree, if a block is *not* given
627620 # noinspection RubyUnusedLocalVariable
628- def each ( & block ) # :yields: node
621+ def each # :yields: node
629622 return to_enum unless block_given?
630623
631624 node_stack = [ self ] # Start with this node
632625
633626 until node_stack . empty?
634- current = node_stack . shift # Pop the top-most node
635- if current # Might be 'nil' (esp. for binary trees)
636- yield current # and process it
637- # Stack children of the current node at top of the stack
638- node_stack = current . children . concat ( node_stack )
639- end
627+ current = node_stack . shift # Pop the top-most node
628+ next unless current # Might be 'nil' (esp. for binary trees)
629+
630+ yield current # and process it
631+ # Stack children of the current node at top of the stack
632+ node_stack = current . children . concat ( node_stack )
640633 end
641634
642635 self if block_given?
@@ -666,7 +659,7 @@ def preordered_each(&block) # :yields: node
666659 # @return [Tree::TreeNode] this node, if a block if given
667660 # @return [Enumerator] an enumerator on this tree, if a block is *not* given
668661 # noinspection RubyUnusedLocalVariable
669- def postordered_each ( & block )
662+ def postordered_each
670663 return to_enum ( :postordered_each ) unless block_given?
671664
672665 # Using a marked node in order to skip adding the children of nodes that
@@ -705,7 +698,7 @@ def postordered_each(&block)
705698 # @return [Tree::TreeNode] this node, if a block if given
706699 # @return [Enumerator] an enumerator on this tree, if a block is *not* given
707700 # noinspection RubyUnusedLocalVariable
708- def breadth_each ( & block )
701+ def breadth_each
709702 return to_enum ( :breadth_each ) unless block_given?
710703
711704 node_queue = [ self ] # Create a queue with self as the initial entry
@@ -733,9 +726,9 @@ def breadth_each(&block)
733726 #
734727 # @return [Array<Tree::TreeNode>] An array of the child nodes, if no block
735728 # is given.
736- def children
729+ def children ( & block )
737730 if block_given?
738- @children . each { | child | yield child }
731+ @children . each ( & block )
739732 self
740733 else
741734 @children . clone
@@ -757,7 +750,7 @@ def children
757750 # @return [Tree::TreeNode] this node, if a block if given
758751 # @return [Array<Tree::TreeNode>] An array of the leaf nodes
759752 # noinspection RubyUnusedLocalVariable
760- def each_leaf ( & block )
753+ def each_leaf
761754 if block_given?
762755 each { |node | yield ( node ) if node . is_leaf? }
763756 self
@@ -775,7 +768,7 @@ def each_leaf(&block)
775768 #
776769 # @return [Tree::TreeNode] this node, if a block if given
777770 # @return [Enumerator] an enumerator on this tree, if a block is *not* given
778- def each_level & block
771+ def each_level
779772 if block_given?
780773 level = [ self ]
781774 until level . empty?
@@ -884,9 +877,9 @@ def siblings
884877 return [ ] if is_root?
885878
886879 siblings = [ ]
887- parent . children { |my_sibling |
880+ parent . children do |my_sibling |
888881 siblings << my_sibling if my_sibling != self
889- }
882+ end
890883 siblings
891884 end
892885 end
@@ -980,10 +973,11 @@ def print_tree(level = node_depth, max_depth = nil,
980973 # Exit if the max level is defined, and reached.
981974 return unless max_depth . nil? || level < max_depth
982975
983- children { |child |
976+ # Child might be 'nil'
977+ children do |child |
984978 child &.print_tree ( level + 1 ,
985979 max_depth , block )
986- } # Child might be 'nil'
980+ end
987981 end
988982 end
989983end
0 commit comments