Help for beginner

My program tree sort
i want display tree
(@data (@left, @right))
(@data (@left, @right))
(@data (@left, @right))

My code:
class Tree
attr_accessor :left
attr_accessor :right
attr_accessor :data

def initialize(x=nil)
@left = nil
@right = nil
@data = x
end
def insert(x)
if @data == nil
@data = x
elsif x <= @data
if @left == nil
@left = Tree.new x
else
@left.insert x
end
else
if @right == nil
@right = Tree.new x
else
@right.insert x
end
end
end

def inorder()
@left.inorder {|y| yield y} if @left != nil
yield @data
@right.inorder {|y| yield y} if @right != nil
end

def traverse()
list = []
yield @data
list << @left if @left != nil
list << @right if @right != nil
loop do
break if list.empty?
node = list.shift
yield node.data
list << node.left if node.left != nil
list << node.right if node.right != nil
end
end
end
items = [50, 20, 80, 10, 30, 70, 90, 5, 14,
28, 41, 66, 75, 88, 96]

tree = Tree.new

items.each {|x| tree.insert(x)}

tree.inorder {|x| print x, " "}
print “\n”

Jesus Castello wrote in post #1172357:

What seems to be the problem? Do you get any errors? In the mean time
you may want to check out my implementation here:

https://github.com/matugm/ruby-algorithms/blob/master/binary_search_tree_spec.rb

https://github.com/matugm/ruby-algorithms/blob/master/binary_search_tree.rb

How I do print ?
(50(20(10(5 nil nil)(14 nil nil))(30(28 nil nil)(41 nil nil)))(80(70(66
nil nil)(75 nil nil))(90(88 nil nil)(96 nil nil))))

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs