tree_print 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/tree_print.rb +34 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 34db3754e710a0426fad9e1a2b5d47d7c801e496
|
4
|
+
data.tar.gz: 2615bb555858f784da0a9b503e4350006ab28c4c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 50535450e9d51eab8784600bd17aa9a424b3c1eb366ee70db41f0d2e13c3b65b326f5227cabfdf0da468b610f21083bd7d48f6394b701501d1ce87155b532694
|
7
|
+
data.tar.gz: adac1654b3a5119c979b3633bb1d190c6371ce60034b13fc73b768d6049b18e192009a7bb82110fba2e94596943ac1eac7cce2f5776dfde7b25667b0b244f784
|
data/lib/tree_print.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module TreePrint
|
2
|
+
def tree_to_s(*hosts)
|
3
|
+
return '' if !hosts.any?
|
4
|
+
height = hosts.compact.map(&:height).max
|
5
|
+
buffer = indent(height)
|
6
|
+
max_length = hosts.compact.map { |node| node.val.to_s.length }.max
|
7
|
+
max_length = [8, max_length].min
|
8
|
+
str = ""
|
9
|
+
row = ""
|
10
|
+
max_length.times do |i|
|
11
|
+
row = hosts.inject("") do |sum, node|
|
12
|
+
sum + buffer + node.val.to_s[i] + buffer + " " rescue sum + buffer + "." + buffer + " "
|
13
|
+
end + " \n"
|
14
|
+
str << row
|
15
|
+
end
|
16
|
+
if height > 1
|
17
|
+
(2**(height-2)).times do
|
18
|
+
row = row.gsub(/ \//, '/ ').gsub(/\\ /, " \\").gsub(/ [\w|.] /, '/ \\')
|
19
|
+
str << row
|
20
|
+
end
|
21
|
+
end
|
22
|
+
return str + tree_to_s(*hosts.flat_map{ |host|
|
23
|
+
if !host
|
24
|
+
[nil, nil]
|
25
|
+
else
|
26
|
+
[host.left, host.right]
|
27
|
+
end
|
28
|
+
})
|
29
|
+
end
|
30
|
+
|
31
|
+
def indent(height)
|
32
|
+
" " * (1...height).inject { |sum, n| sum + 2 ** (n-1) } rescue ""
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tree_print
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Carlson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-05 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Recursive method that converts the head node to a plain string.
|
14
|
+
email: acarl005@g.ucla.edu
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/tree_print.rb
|
20
|
+
homepage: http://rubygems.org/gems/tree_print
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.0.14
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Print your tree data structures as a nicely formatted string!
|
44
|
+
test_files: []
|