Ruby iterative depth

Hello,

Short question:

How to make an iterative method of stack?

Long explanation:
I’m trying to create an iterative method for following structure:

class Obj
attr_accessor: id,: parent_id

def initialize (id, parent_id)
parent_id = @ parent_id
@ id = id
end

def to_s
"id: # {@ id}, parent_id: @ # {parent_id} ’
end
end

left = [Obj.new (1.0), Obj.new (2.1), Obj.new (3.2), Obj.new (4.0),
Obj.new (5.1), Obj new (6.1), Obj.new (7.3)]

My challenge is to make an iterative method, see how far I got:

def menu_iterative (menu)
res = []

el1 = menu.shift
res << el1
while not menu.empty?
res_aux = []
for i in menu
if el1.id == i.parent_id
print el1, “==” i "* ", res_aux.index (el1), “\ n”
res_aux << i
el1 = menu.delete (i)
retry
else
print el1, “! =” i, “\ n”
end
end
res << res_aux unless res_aux.empty?
puts
el1 = menu.shift
res << el1
end
return res
end

puts “result:”
pp menu_iterative (left_it)

I wish the outcome was:

[#<Obj:0x7f35b48cab40 @id=1, @parent_id=0>,
[#<Obj:0x7f35b48cab18 @id=2, @parent_id=1>,
[#<Obj:0x7f35b48caaf0 @id=3, @parent_id=2>,
[#<Obj:0x7f35b48caa50 @id=7, @parent_id=3>]],
[#<Obj:0x7f35b48caaf0 @id=3, @parent_id=2>]],
[#<Obj:0x7f35b48cab18 @id=2, @parent_id=1>,
#<Obj:0x7f35b48caaa0 @id=5, @parent_id=1>,
#<Obj:0x7f35b48caa78 @id=6, @parent_id=1>]]

It seems that the problem is attribution without stack, correct?
I’m attaching all I did.

Thanks for any help.

Hi Marcello,

For helping me understand your desired outcome I manually turned it
into something more readable (just to read the nesting [] ).

[“1=>0”,[“2=>1”,[“3=>2”,[“7=>3”]],[“3=>2”]],[“2=>1”,“5=>1”,“6=>1”]]

arr[0] = “1=>0”
arr[1] = [“2=>1”, [“3=>2”, [“7=>3”]], [“3=>2”]]
arr[2] = [“2=>1”, “5=>1”, “6=>1”]

Is this what you want?

Hi Abinoam,

Thanks for reply, actually the first number is ‘id’ and second
‘parent_id’, so correct is:
[“1=>0”,
[“2=>1”,
[“3=>2”,
[“7=>3”]
],“5=>1”,“6=>1”
],“4=>0”
]

From source flat:
[ “1=>0”,“2=>1”, “3=>2”,“4=>0”,“5=>1”,“6=>1”,“7=>3”]

The purpose is to use such as menu like this, based in example above:

  • "1=>0"
    • "2=>1"
      • "3=>2"
        • "7=>3"
  • "5=>1"
  • "6=>1"
  • "4=>0"
  • Thanks in advance!

    Hi Marcello,

    Sorry, but I was not able to figure out exactly how this piece of code
    is trying to generate your desired outcome. (my fault!)
    If you’re just willing to represent the data in a kind o hierarchical
    tree, there’s a lot of “Ruby way” to do that.
    It seams (in my humble opinion) that you’re trying to do it in “C”
    way, or something like that.
    But, I can give you some tips. See comments in the code.

    You shouldn’t need to define methods outside the object scope (like

    the one bellow).

    Mtodos busca em profundidade iterativo

    def menu_iterative(menu)
    res = []

    When shifting the first element you’re assuming the Array is in a

    kind of order,

    and the first element is the “root”. Is this always true?

    el1 = menu.shift
    res << el1
    while not menu.empty?
    res_aux = []
    for i in menu
    if el1.id == i.parent_id
    print el1," == “,i,”* “,res_aux.index(el1),”\n"
    res_aux << i

    # The same shift/delete problem here
    # This elemente will never be reevaluated after deleted. And...
    # Using delete here you're assuming that the elements are unique.
    # Is this always true. If there's 2 "i" elements in the Array, with
    # this line you'll be deleting the two of them.
    # You can delete by the index instead.
        el1 = menu.delete(i)
    
    # This use of retry should be considered deprecated.
    # It seems removed from Ruby 1.9.
        #       retry
    else
        print el1," != ",i," \n"
      end
    end
    res << res_aux unless res_aux.empty?
    el1 = menu.shift
    res << el1
    

    end

    You don’t need return here, the “return value” is always the last

    evaluated expression
    return res
    end

    Good luck!

    Hi Abinoam,

    You are true, I’m trying doing like C and hierarchical tree, what
    better way to do this in ruby? It may be that my data structure is
    bad, do you have some suggestion?

    The ideia is create a menu hierarchical, where there self nestedly by
    parent_id.
    Thank you for help.