fork(1) download
  1. #!ruby
  2.  
  3. def tree_sub( data, cd, nst )
  4. cds = []
  5. data.each_with_index{|n,idx|
  6. cds << idx if n == cd
  7. }
  8. cds.each_with_index{|x,idx|
  9. print "│ " * nst, (cds.size - 1 <= idx)? "└" : "├", "─ #{x}\n"
  10. tree_sub( data, x, nst + 1 )
  11. }
  12. end
  13.  
  14. def tree( data )
  15. cd = data.index( -1 )
  16. puts cd
  17. tree_sub( data, cd, 0 )
  18. end
  19.  
  20.  
  21. tree( [1, 2, -1, 0, 0, 1, 1, 2] )
  22.  
Success #stdin #stdout 0.01s 5736KB
stdin
Standard input is empty
stdout
2
├─ 1
│ ├─ 0
│ │ ├─ 3
│ │ └─ 4
│ ├─ 5
│ └─ 6
└─ 7