Hi, guys.

I’m writing a Node class for a Binary Tree.

Coding comes below.

There’s a method remove_leaf.

Now I want to do it this way:

=begin

[@left_node, @right_node].each do |node|

if node.is_leaf?

node = nil

end

end

=end

But it doesn’t work.

Is there a way to do it ?

You can ignore class Tree. I just post it here to make a little sense.

---------- Forwarded message ----------

From: salamond [email protected]

Date: Wed, Oct 13, 2010 at 9:12 AM

Subject: ruby-talk-ask

To: [email protected]

class Node

attr_accessor :value, :left_node, :right_node

def initialize(value=nil, left_node=nil, right_node=nil)

@value = value

@left_node = left_node

@right_node = right_node

end

def to_s

@value.to_s

end

def is_leaf?

return @left_node==nil && @right_node==nil

end

def remove_leaf

if is_leaf?

return

else

=begin

[@left_node, @right_node].each do |node|

if node.is_leaf?

node = nil

end

end

=end

if @left_node.is_leaf?

@left_node = nil

end

if @right_node.is_leaf?

@right_node = nil

end

end

end

end

require ‘test/unit’

require ‘node’

class NodeTest < Test::Unit::TestCase

def setup

@empty = Node.new()

@single = Node.new(“haha”)

@full = Node.new(1, Node.new(2), Node.new(4))

end

def test_default_init()

assert(@empty.value==nil)

assert(@empty.left_node==nil)

assert(@empty.right_node==nil)

end

def test_init_with_value()

assert(@full.left_node.value==2)

assert(@full.right_node.value==4)

assert(@full.value==1)

end

def test_to_s()

assert(@full.to_s == “1”)

assert(@single.to_s == “haha”)

end

def test_is_leaf()

assert(@empty.is_leaf? == true)

assert(@single.is_leaf? == true)

assert(@full.is_leaf? == false)

end

def test_remove_leaf()

assert(@full.is_leaf? == false)

@full.remove_leaf()

assert(@full.is_leaf? == true)

end

end

require “node”

class Tree

attr_accessor :root, :nodes, :sorted_nodes

def initialize( root=nil )

@root = root

@nodes = Array.new()

if @root!= nil

@nodes.push( @root )

end

@sorted_nodes = Array.new()

@targets = Array.new()

end

def insert( node )

@nodes.push( node )

@root = insert_at( @root, node )

end

# TODO: to prevent multiple travel in the same way, add state check

def travel(order = :inorder)

@sorted_nodes = Array.new()

travel_at( @root, order )

return @sorted_nodes

end

def to_s

@data.to_s

end

def cut_all_leaves()

@nodes.each do |node|

node.cut_leaf

end

end

def n_step_from_leaf( n )

(1…n).each do |times|

cut_all_leaves

end

nodes = Array.new()

travel().each do |node|

if node.is_leaf?

nodes.push(node)

end

end

return nodes

end

private

def insert_at(subroot, node)

if subroot == nil

subroot = node

else

if subroot.value >= node.value

subroot.left_node = insert_at(subroot.left_node, node)

else

subroot.right_node = insert_at(subroot.right_node, node)

end

end

return subroot

end

def travel_at(subroot, order)

if subroot == nil

return

end

case order

when :preorder

@sorted_nodes.push( subroot )

travel_at( subroot.left_node, order )

travel_at( subroot.right_node, order )

when :inorder

travel_at( subroot.left_node, order)

@sorted_nodes.push( subroot )

travel_at( subroot.right_node, order )

when :postorder

travel_at( subroot.left_node, order )

travel_at( subroot.right_node, order)

@sorted_nodes.push( subroot )

end

end

end