4747# This module also acts as the namespace for all classes in the *RubyTree*
4848# package.
4949module Tree
50-
5150 # == TreeNode Class Description
5251 #
5352 # This class models the nodes for an *N-ary* tree data structure. The
@@ -267,10 +266,9 @@ def marshal_dump
267266 # Creates a dump representation of this node and returns the same as
268267 # a hash.
269268 def create_dump_rep # :nodoc:
270- { name : @name ,
271- parent : ( is_root? ? nil : @parent . name ) ,
272- content : Marshal . dump ( @content )
273- }
269+ { name : @name ,
270+ parent : ( is_root? ? nil : @parent . name ) ,
271+ content : Marshal . dump ( @content ) }
274272 end
275273
276274 protected :create_dump_rep
@@ -285,7 +283,7 @@ def create_dump_rep # :nodoc:
285283 # self and makes itself the root.
286284 #
287285 def marshal_load ( dumped_tree_array )
288- nodes = { }
286+ nodes = { }
289287 dumped_tree_array . each do |node_hash |
290288 name = node_hash [ :name ]
291289 parent_name = node_hash [ :parent ]
@@ -381,7 +379,7 @@ def add(child, at_index = -1)
381379 raise ArgumentError , 'Attempting to add a nil node' unless child
382380
383381 raise ArgumentError , 'Attempting add node to itself' if equal? ( child )
384-
382+
385383 raise ArgumentError , 'Attempting add root as a child' if child . equal? ( root )
386384
387385 # Lazy man's unique test, won't test if children of child are unique in
@@ -408,7 +406,7 @@ def add(child, at_index = -1)
408406 # Return a range of valid insertion positions. Used in the #add method.
409407 def insertion_range
410408 max = @children . size
411- min = -( max + 1 )
409+ min = -( max + 1 )
412410 min ..max
413411 end
414412
@@ -444,7 +442,7 @@ def rename_child(old_name, new_name)
444442 unless @children_hash . key? ( old_name )
445443
446444 @children_hash [ new_name ] = @children_hash . delete ( old_name )
447- @children_hash [ new_name ] . name = new_name
445+ @children_hash [ new_name ] . name = new_name
448446 end
449447
450448 # Protected method to set the name of this node.
@@ -558,7 +556,7 @@ def set_as_root! # :nodoc:
558556 # The nodes become immutable after this operation. In effect, the entire tree's
559557 # structure and contents become _read-only_ and cannot be changed.
560558 def freeze_tree!
561- each { |node | node . freeze }
559+ each { |node | node . freeze }
562560 end
563561
564562 # @!endgroup
@@ -598,7 +596,7 @@ def freeze_tree!
598596 #
599597 # @see #add
600598 # @see #initialize
601- def []( name_or_index , num_as_name = false )
599+ def []( name_or_index , num_as_name = false )
602600 raise ArgumentError , 'Name_or_index needs to be provided!' if name_or_index . nil?
603601
604602 if name_or_index . is_a? ( Integer ) && !num_as_name
@@ -628,21 +626,20 @@ def [](name_or_index, num_as_name=false)
628626 # @return [Enumerator] an enumerator on this tree, if a block is *not* given
629627 # noinspection RubyUnusedLocalVariable
630628 def each ( &block ) # :yields: node
629+ return to_enum unless block_given?
631630
632- return to_enum unless block_given?
631+ node_stack = [ self ] # Start with this node
633632
634- node_stack = [ self ] # Start with this node
635-
636- until node_stack . empty?
637- current = node_stack . shift # Pop the top-most node
638- if current # Might be 'nil' (esp. for binary trees)
639- yield current # and process it
640- # Stack children of the current node at top of the stack
641- node_stack = current . children . concat ( node_stack )
642- end
643- end
633+ 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
640+ end
644641
645- self if block_given?
642+ self if block_given?
646643 end
647644
648645 # Traverses the (sub)tree rooted at this node in pre-ordered sequence.
@@ -684,7 +681,7 @@ def postordered_each(&block)
684681 peek_node . visited = true
685682 # Add the children to the stack. Use the marking structure.
686683 marked_children =
687- peek_node . node . children . map { |node | marked_node . new ( node , false ) }
684+ peek_node . node . children . map { |node | marked_node . new ( node , false ) }
688685 node_stack = marked_children . concat ( node_stack )
689686 next
690687 else
@@ -738,7 +735,7 @@ def breadth_each(&block)
738735 # is given.
739736 def children
740737 if block_given?
741- @children . each { |child | yield child }
738+ @children . each { |child | yield child }
742739 self
743740 else
744741 @children . clone
@@ -887,8 +884,9 @@ def siblings
887884 return [ ] if is_root?
888885
889886 siblings = [ ]
890- parent . children { |my_sibling |
891- siblings << my_sibling if my_sibling != self }
887+ parent . children { |my_sibling |
888+ siblings << my_sibling if my_sibling != self
889+ }
892890 siblings
893891 end
894892 end
@@ -963,7 +961,8 @@ def <=>(other)
963961 # @param [Proc] block optional block to use for rendering
964962 def print_tree ( level = node_depth , max_depth = nil ,
965963 block = lambda { |node , prefix |
966- puts "#{ prefix } #{ node . name } " } )
964+ puts "#{ prefix } #{ node . name } "
965+ } )
967966 prefix = ''
968967
969968 if is_root?
@@ -983,8 +982,8 @@ def print_tree(level = node_depth, max_depth = nil,
983982
984983 children { |child |
985984 child &.print_tree ( level + 1 ,
986- max_depth , block ) } # Child might be 'nil'
985+ max_depth , block )
986+ } # Child might be 'nil'
987987 end
988-
989988 end
990989end
0 commit comments