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

0
└─ 1
  └─ 2
    ├─ 3
    │ └─ 4
    └─ 5