rubytree 0.9.5 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Gemfile +0 -6
- data/Gemfile.lock +75 -44
- data/History.rdoc +18 -2
- data/LICENSE.md +1 -2
- data/README.md +10 -8
- data/Rakefile +43 -35
- data/examples/example_basic.rb +11 -8
- data/lib/rubytree.rb +1 -1
- data/lib/tree/binarytree.rb +17 -18
- data/lib/tree/tree_deps.rb +8 -8
- data/lib/tree/utils/camel_case_method_handler.rb +12 -14
- data/lib/tree/utils/hash_converter.rb +62 -64
- data/lib/tree/utils/json_converter.rb +16 -17
- data/lib/tree/utils/metrics_methods.rb +12 -9
- data/lib/tree/utils/path_methods.rb +8 -10
- data/lib/tree/utils/tree_merge_handler.rb +9 -10
- data/lib/tree/utils/utils.rb +3 -2
- data/lib/tree/version.rb +2 -3
- data/lib/tree.rb +143 -123
- data/rubytree.gemspec +27 -25
- data/setup.rb +239 -259
- data/spec/spec_helper.rb +10 -0
- data/spec/tree_spec.rb +72 -0
- data/test/run_test.rb +6 -6
- data/test/test_binarytree.rb +93 -92
- data/test/test_rubytree_require.rb +2 -4
- data/test/test_subclassed_node.rb +13 -16
- data/test/test_thread_and_fiber.rb +10 -13
- data/test/test_tree.rb +565 -572
- metadata +98 -29
- data/TAGS +0 -248
- data/gem_graph.png +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e9977428c2a28cf7d3a31ccc91047f646ec88c4f0da90663e9596dfb006ce0c6
|
4
|
+
data.tar.gz: 0e3a42cf9214d95e5f9deed0511c6728c5204a03a319b30a80d96daf3f20f505
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f311eec1cf9a9710cb5905e5c7c691344bd60beb57b472462d40a81e12af475bb6bf328a6502aa8bbb9af17d5d350b163b5d8d36888835866c473b9ea6e13953
|
7
|
+
data.tar.gz: fdc9890aaf6e298d4400ee6f84398ced73dfdaf93c6fe87a4981dd35c5f17934e2a01e803d44351724c615f11e075fbf3181403cbb4343005452ee47fdfbe25e
|
data/Gemfile
CHANGED
@@ -3,12 +3,6 @@ source 'https://rubygems.org'
|
|
3
3
|
# Specify your gem's dependencies in rubytree.gemspec
|
4
4
|
gemspec
|
5
5
|
|
6
|
-
group :development, :test do
|
7
|
-
gem "rake", "~> 10.4"
|
8
|
-
gem "test-unit", "~> 3.0"
|
9
|
-
gem "coveralls", ">= 0.7", :require => false, :platforms => :mri_21
|
10
|
-
end
|
11
|
-
|
12
6
|
# Local Variables:
|
13
7
|
# mode: ruby
|
14
8
|
# End:
|
data/Gemfile.lock
CHANGED
@@ -1,66 +1,97 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rubytree (0.
|
5
|
-
json (~> 1
|
6
|
-
structured_warnings (~> 0.
|
4
|
+
rubytree (1.0.2)
|
5
|
+
json (~> 2.6.1)
|
6
|
+
structured_warnings (~> 0.4.0)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
simplecov (~> 0.
|
11
|
+
ast (2.4.2)
|
12
|
+
coveralls (0.8.23)
|
13
|
+
json (>= 1.8, < 3)
|
14
|
+
simplecov (~> 0.16.1)
|
15
15
|
term-ansicolor (~> 1.3)
|
16
|
-
thor (
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
16
|
+
thor (>= 0.19.4, < 2.0)
|
17
|
+
tins (~> 1.6)
|
18
|
+
diff-lcs (1.5.0)
|
19
|
+
docile (1.4.0)
|
20
|
+
json (2.6.1)
|
21
|
+
parallel (1.21.0)
|
22
|
+
parser (3.0.3.2)
|
23
|
+
ast (~> 2.4.1)
|
24
|
+
power_assert (2.0.1)
|
25
|
+
psych (4.0.3)
|
26
|
+
stringio
|
27
|
+
rainbow (3.0.0)
|
28
|
+
rake (13.0.6)
|
29
|
+
rdoc (6.4.0)
|
30
|
+
psych (>= 4.0.0)
|
31
|
+
regexp_parser (2.2.0)
|
32
|
+
rexml (3.2.5)
|
33
|
+
rspec (3.10.0)
|
34
|
+
rspec-core (~> 3.10.0)
|
35
|
+
rspec-expectations (~> 3.10.0)
|
36
|
+
rspec-mocks (~> 3.10.0)
|
37
|
+
rspec-core (3.10.1)
|
38
|
+
rspec-support (~> 3.10.0)
|
39
|
+
rspec-expectations (3.10.1)
|
40
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
41
|
+
rspec-support (~> 3.10.0)
|
42
|
+
rspec-mocks (3.10.2)
|
43
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
44
|
+
rspec-support (~> 3.10.0)
|
45
|
+
rspec-support (3.10.3)
|
32
46
|
rtags (0.97)
|
33
47
|
rtagstask (0.0.4)
|
34
48
|
rtags (> 0.0.0)
|
35
|
-
|
36
|
-
|
37
|
-
|
49
|
+
rubocop (1.24.0)
|
50
|
+
parallel (~> 1.10)
|
51
|
+
parser (>= 3.0.0.0)
|
52
|
+
rainbow (>= 2.2.2, < 4.0)
|
53
|
+
regexp_parser (>= 1.8, < 3.0)
|
54
|
+
rexml
|
55
|
+
rubocop-ast (>= 1.15.0, < 2.0)
|
56
|
+
ruby-progressbar (~> 1.7)
|
57
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
58
|
+
rubocop-ast (1.15.1)
|
59
|
+
parser (>= 3.0.1.1)
|
60
|
+
ruby-progressbar (1.11.0)
|
61
|
+
simplecov (0.16.1)
|
62
|
+
docile (~> 1.1)
|
63
|
+
json (>= 1.8, < 3)
|
38
64
|
simplecov-html (~> 0.10.0)
|
39
|
-
simplecov-html (0.10.
|
40
|
-
|
41
|
-
|
65
|
+
simplecov-html (0.10.2)
|
66
|
+
stringio (3.0.1)
|
67
|
+
structured_warnings (0.4.0)
|
68
|
+
sync (0.5.0)
|
69
|
+
term-ansicolor (1.7.1)
|
42
70
|
tins (~> 1.0)
|
43
|
-
test-unit (3.
|
71
|
+
test-unit (3.5.3)
|
44
72
|
power_assert
|
45
|
-
thor (
|
46
|
-
tins (1.
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
yard (0.
|
73
|
+
thor (1.1.0)
|
74
|
+
tins (1.30.0)
|
75
|
+
sync
|
76
|
+
unicode-display_width (2.1.0)
|
77
|
+
webrick (1.7.0)
|
78
|
+
yard (0.9.27)
|
79
|
+
webrick (~> 1.7.0)
|
51
80
|
|
52
81
|
PLATFORMS
|
53
82
|
ruby
|
54
83
|
|
55
84
|
DEPENDENCIES
|
56
|
-
bundler (~>
|
57
|
-
coveralls (>= 0.
|
58
|
-
rake (
|
59
|
-
rdoc (
|
60
|
-
|
85
|
+
bundler (~> 2.3.4)
|
86
|
+
coveralls (>= 0.8.23)
|
87
|
+
rake (>= 13.0.6)
|
88
|
+
rdoc (>= 6.4.0)
|
89
|
+
rspec (~> 3.10.0)
|
90
|
+
rtagstask (~> 0.0.4)
|
91
|
+
rubocop (>= 1.24.0)
|
61
92
|
rubytree!
|
62
|
-
test-unit (
|
63
|
-
yard (~> 0.
|
93
|
+
test-unit (>= 3.5.3)
|
94
|
+
yard (~> 0.9.27)
|
64
95
|
|
65
96
|
BUNDLED WITH
|
66
|
-
|
97
|
+
2.3.4
|
data/History.rdoc
CHANGED
@@ -1,7 +1,23 @@
|
|
1
|
-
# @markup rdoc
|
2
|
-
|
3
1
|
= History of Changes
|
4
2
|
|
3
|
+
=== 0.9.7 / 2015-12-31
|
4
|
+
|
5
|
+
* Released 0.9.6. This is a minor bug-fix release.
|
6
|
+
|
7
|
+
* This release allows the {Tree::TreeNode#print_tree} method to be used on
|
8
|
+
non-root nodes. Thanks to {https://github.com/ojab Ojab}.
|
9
|
+
|
10
|
+
* The spaceship operator ('<=>') now returns 'nil' if the object being compared
|
11
|
+
to is itself 'nil' or not another {Tree::TreeNode}.
|
12
|
+
|
13
|
+
=== 0.9.6 / 2015-05-30
|
14
|
+
|
15
|
+
* Released 0.9.6, which is same as 0.9.5, except for an update to the Gem release date.
|
16
|
+
|
17
|
+
=== 0.9.5 / 2015-05-30
|
18
|
+
|
19
|
+
* Released 0.9.5.
|
20
|
+
|
5
21
|
=== 0.9.5pre7 / 2015-05-30
|
6
22
|
|
7
23
|
* Added new methods for getting the path of a node as a string. These have been
|
data/LICENSE.md
CHANGED
data/README.md
CHANGED
@@ -1,17 +1,15 @@
|
|
1
1
|
<!--
|
2
2
|
README.md
|
3
3
|
|
4
|
-
Copyright (C) 2006-
|
4
|
+
Copyright (C) 2006-2017, 2020 Anupam Sengupta (anupamsg@gmail.com)
|
5
5
|
|
6
6
|
-->
|
7
7
|
# **RubyTree** #
|
8
8
|
|
9
9
|
[![Gem Version](https://badge.fury.io/rb/rubytree.png)](http://badge.fury.io/rb/rubytree)
|
10
10
|
[![Travis Build Status](https://secure.travis-ci.org/evolve75/RubyTree.png)](http://travis-ci.org/evolve75/rubytree)
|
11
|
-
[![Dependency Status](https://gemnasium.com/evolve75/RubyTree.png)](https://gemnasium.com/evolve75/RubyTree)
|
12
11
|
[![Code Climate](https://codeclimate.com/github/evolve75/RubyTree.png)](https://codeclimate.com/github/evolve75/RubyTree)
|
13
12
|
[![Coverage Status](https://coveralls.io/repos/evolve75/RubyTree/badge.png)](https://coveralls.io/r/evolve75/RubyTree)
|
14
|
-
[![githalytics.com alpha](https://cruel-carlota.pagodabox.com/473b881f1075bf9f255f0181dde6068f)](http://githalytics.com/evolve75/RubyTree)
|
15
13
|
|
16
14
|
## DESCRIPTION: ##
|
17
15
|
|
@@ -104,6 +102,9 @@ siblings_of_child1 = child1.siblings
|
|
104
102
|
# ..... Retrieve immediate children of the root node as an array.
|
105
103
|
children_of_root = root_node.children
|
106
104
|
|
105
|
+
# ..... Retrieve the parent of a node.
|
106
|
+
parent = child1.parent
|
107
|
+
|
107
108
|
# ..... This is a depth-first and L-to-R pre-ordered traversal.
|
108
109
|
root_node.each { |node| node.content.reverse }
|
109
110
|
|
@@ -118,7 +119,7 @@ This example can also be found at
|
|
118
119
|
|
119
120
|
## REQUIREMENTS: ##
|
120
121
|
|
121
|
-
* [Ruby][]
|
122
|
+
* [Ruby][] 2.2.x, 2.3.x or 2.4.x
|
122
123
|
|
123
124
|
|
124
125
|
* Run-time Dependencies:
|
@@ -130,6 +131,7 @@ This example can also be found at
|
|
130
131
|
* [Bundler][] for creating the stable build environment
|
131
132
|
* [Rake][] for building the package
|
132
133
|
* [Yard][] for the documentation
|
134
|
+
* [RSpec][] for additional Ruby Spec test files
|
133
135
|
|
134
136
|
## INSTALL: ##
|
135
137
|
|
@@ -241,8 +243,8 @@ A big thanks to the following contributors for helping improve **RubyTree**:
|
|
241
243
|
[BSD]: http://opensource.org/licenses/bsd-license.php "BSD License"
|
242
244
|
[Binary tree]: http://en.wikipedia.org/wiki/Binary_tree "Binary Tree Data Structure"
|
243
245
|
[Bundler]: http://bundler.io "Bundler"
|
244
|
-
[Comparable]: http://ruby-doc.org/core-
|
245
|
-
[Enumerable]: http://ruby-doc.org/core-
|
246
|
+
[Comparable]: http://ruby-doc.org/core-2.4.2/Comparable.html "Comparable mix-in"
|
247
|
+
[Enumerable]: http://ruby-doc.org/core-2.4.2/Enumerable.html "Enumerable mix-in"
|
246
248
|
[JSON]: http://flori.github.com/json "JSON"
|
247
249
|
[Rake]: https://rubygems.org/gems/rake "Rake"
|
248
250
|
[Ruby]: http://www.ruby-lang.org "Ruby Programming Language"
|
@@ -251,7 +253,7 @@ A big thanks to the following contributors for helping improve **RubyTree**:
|
|
251
253
|
[breadth-first]: http://en.wikipedia.org/wiki/Breadth-first_search "Breadth-first (level-first) Traversal"
|
252
254
|
[git]: http://git-scm.com "Git SCM"
|
253
255
|
[in-order]: http://en.wikipedia.org/wiki/Tree_traversal#In-order "In-order (symmetric) Traversal"
|
254
|
-
[marshaling]: http://ruby-doc.org/core-
|
256
|
+
[marshaling]: http://ruby-doc.org/core-2.4.2/Marshal.html "Marshaling in Ruby"
|
255
257
|
[post-order]: http://en.wikipedia.org/wiki/Tree_traversal#Post-order "Post-ordered Traversal"
|
256
258
|
[pre-order]: http://en.wikipedia.org/wiki/Tree_traversal#Pre-order "Pre-ordered Traversal"
|
257
259
|
[rt@github]: http://github.com/evolve75/RubyTree "RubyTree Project Page on Github"
|
@@ -261,6 +263,6 @@ A big thanks to the following contributors for helping improve **RubyTree**:
|
|
261
263
|
[rt_site]: http://rubytree.anupamsg.me "RubyTree Site"
|
262
264
|
[structured_warnings]: http://github.com/schmidt/structured_warnings "structured_warnings"
|
263
265
|
[tree_data_structure]: http://en.wikipedia.org/wiki/Tree_data_structure "Tree Data Structure"
|
264
|
-
|
266
|
+
[RSpec]: https://relishapp.com/rspec/
|
265
267
|
|
266
268
|
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/evolve75/rubytree/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
|
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
#
|
3
3
|
# Rakefile - This file is part of the RubyTree package.
|
4
4
|
#
|
5
|
-
# Copyright (c) 2006-
|
5
|
+
# Copyright (c) 2006-2021 Anupam Sengupta
|
6
6
|
#
|
7
7
|
# All rights reserved.
|
8
8
|
#
|
@@ -33,46 +33,47 @@
|
|
33
33
|
#
|
34
34
|
|
35
35
|
require 'rubygems'
|
36
|
-
GEM_SPEC = eval(File.read(
|
36
|
+
GEM_SPEC = eval(File.read('./rubytree.gemspec')) # Load the gemspec.
|
37
37
|
|
38
38
|
PKG_NAME = GEM_SPEC.name
|
39
39
|
PKG_VER = GEM_SPEC.version
|
40
40
|
GEM_NAME = "#{PKG_NAME}-#{PKG_VER}.gem"
|
41
41
|
|
42
|
-
desc
|
42
|
+
desc 'Default Task (Run the tests)'
|
43
43
|
task :default do
|
44
|
-
if ENV[
|
45
|
-
Rake::Task[
|
44
|
+
if ENV['COVERAGE']
|
45
|
+
Rake::Task['test:coverage'].invoke
|
46
46
|
else
|
47
|
-
Rake::Task[
|
47
|
+
Rake::Task['test:unit'].invoke
|
48
|
+
Rake::Task['spec'].invoke
|
48
49
|
end
|
49
50
|
end
|
50
51
|
|
51
|
-
desc
|
52
|
+
desc 'Display the current gem version'
|
52
53
|
task :version do
|
53
54
|
puts "Current Version: #{GEM_NAME}"
|
54
55
|
end
|
55
56
|
|
56
57
|
require 'rake/clean'
|
57
|
-
task :
|
58
|
+
task clean: 'gem:clobber_package'
|
58
59
|
CLEAN.include('coverage')
|
59
|
-
task :
|
60
|
+
task clobber: [:clean, 'doc:clobber_rdoc', 'doc:clobber_yard']
|
60
61
|
|
61
|
-
desc
|
62
|
+
desc 'Open an irb session preloaded with this library'
|
62
63
|
task :console do
|
63
|
-
sh
|
64
|
+
sh 'irb -rubygems -r ./lib/tree.rb'
|
64
65
|
end
|
65
66
|
|
66
|
-
namespace :doc do
|
67
|
+
namespace :doc do # ................................ Documentation
|
67
68
|
begin
|
68
|
-
gem 'rdoc',
|
69
|
+
gem 'rdoc', '>= 6.4.0' # To get around a stupid bug in Ruby 1.9.2 Rake.
|
69
70
|
require 'rdoc/task'
|
70
71
|
Rake::RDocTask.new do |rdoc|
|
71
72
|
rdoc.rdoc_dir = 'rdoc'
|
72
73
|
rdoc.title = "#{PKG_NAME}-#{PKG_VER}"
|
73
74
|
rdoc.main = 'README.rdoc'
|
74
75
|
rdoc.rdoc_files.include(GEM_SPEC.extra_rdoc_files)
|
75
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
76
|
+
rdoc.rdoc_files.include('./lib/**/*.rb')
|
76
77
|
end
|
77
78
|
rescue LoadError
|
78
79
|
# Oh well.
|
@@ -82,23 +83,22 @@ namespace :doc do # ................................ Documentation
|
|
82
83
|
require 'yard'
|
83
84
|
YARD::Rake::YardocTask.new do |t|
|
84
85
|
t.files = ['lib/**/*.rb', '-', GEM_SPEC.extra_rdoc_files]
|
85
|
-
t.options = [
|
86
|
+
t.options = %w[--no-private --embed-mixins]
|
86
87
|
end
|
87
88
|
rescue LoadError
|
88
89
|
# Oh well.
|
89
90
|
end
|
90
91
|
|
91
|
-
desc
|
92
|
+
desc 'Remove YARD Documentation'
|
92
93
|
task :clobber_yard do
|
93
94
|
rm_rf 'doc'
|
94
95
|
end
|
95
96
|
end
|
96
97
|
|
97
|
-
desc
|
98
|
-
task :
|
99
|
-
|
100
|
-
namespace :test do # ................................ Test related
|
98
|
+
desc 'Run the test cases'
|
99
|
+
task test: 'test:unit'
|
101
100
|
|
101
|
+
namespace :test do # ................................ Test related
|
102
102
|
require 'rake/testtask'
|
103
103
|
Rake::TestTask.new(:unit) do |test|
|
104
104
|
test.libs << 'lib' << 'test'
|
@@ -106,7 +106,7 @@ namespace :test do # ................................ Test related
|
|
106
106
|
test.verbose = false
|
107
107
|
end
|
108
108
|
|
109
|
-
desc
|
109
|
+
desc 'Run the examples'
|
110
110
|
Rake::TestTask.new(:examples) do |example|
|
111
111
|
example.libs << 'lib' << 'examples'
|
112
112
|
example.pattern = 'examples/**/example_*.rb'
|
@@ -114,7 +114,7 @@ namespace :test do # ................................ Test related
|
|
114
114
|
example.warning = false
|
115
115
|
end
|
116
116
|
|
117
|
-
desc
|
117
|
+
desc 'Run the code coverage'
|
118
118
|
task :coverage do
|
119
119
|
ruby 'test/run_test.rb'
|
120
120
|
end
|
@@ -122,7 +122,7 @@ namespace :test do # ................................ Test related
|
|
122
122
|
begin
|
123
123
|
require 'rcov/rcovtask'
|
124
124
|
Rcov::RcovTask.new(:rcov) do |t|
|
125
|
-
t.libs <<
|
125
|
+
t.libs << 'test'
|
126
126
|
t.test_files = FileList['test/**/test_*.rb']
|
127
127
|
t.verbose = true
|
128
128
|
t.rcov_opts << '--exclude /gems/,/Library/,/usr/,spec,lib/tasks'
|
@@ -130,20 +130,28 @@ namespace :test do # ................................ Test related
|
|
130
130
|
rescue LoadError
|
131
131
|
# Oh well. Can't have everything.
|
132
132
|
end
|
133
|
+
end
|
134
|
+
|
135
|
+
begin # ................................ rspec tests
|
136
|
+
require 'rspec/core/rake_task'
|
133
137
|
|
138
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
139
|
+
t.fail_on_error = false
|
140
|
+
t.rspec_opts = ['--color', '--format doc']
|
141
|
+
end
|
142
|
+
rescue LoadError
|
143
|
+
# Cannot load rspec.
|
134
144
|
end
|
135
145
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
CLEAN.include('TAGS')
|
142
|
-
end
|
143
|
-
rescue LoadError
|
144
|
-
# Oh well. Can't have everything.
|
146
|
+
namespace :tag do # ................................ Emacs Tags
|
147
|
+
require 'rtagstask'
|
148
|
+
RTagsTask.new(:tags) do |rd|
|
149
|
+
rd.vi = false
|
150
|
+
CLEAN.include('TAGS')
|
145
151
|
end
|
146
|
-
|
152
|
+
rescue LoadError
|
153
|
+
# Oh well. Can't have everything.
|
154
|
+
end
|
147
155
|
|
148
156
|
namespace :gem do # ................................ Gem related
|
149
157
|
require 'rubygems/package_task'
|
@@ -152,8 +160,8 @@ namespace :gem do # ................................ Gem related
|
|
152
160
|
pkg.need_tar = true
|
153
161
|
end
|
154
162
|
|
155
|
-
desc
|
156
|
-
task :
|
163
|
+
desc 'Push the gem into the Rubygems repository'
|
164
|
+
task push: :gem do
|
157
165
|
sh "gem push pkg/#{GEM_NAME}"
|
158
166
|
end
|
159
167
|
end
|
data/examples/example_basic.rb
CHANGED
@@ -3,8 +3,8 @@
|
|
3
3
|
# example_basic.rb:: Basic usage of the tree library.
|
4
4
|
#
|
5
5
|
# Author: Anupam Sengupta
|
6
|
-
# Time-stamp: <
|
7
|
-
# Copyright (C) 2013 Anupam Sengupta <anupamsg@gmail.com>
|
6
|
+
# Time-stamp: <2015-12-31 22:17:30 anupam>
|
7
|
+
# Copyright (C) 2013, 2015 Anupam Sengupta <anupamsg@gmail.com>
|
8
8
|
#
|
9
9
|
# The following example implements this tree structure:
|
10
10
|
#
|
@@ -23,22 +23,22 @@
|
|
23
23
|
# +---------------+
|
24
24
|
|
25
25
|
# ..... Example starts.
|
26
|
-
require 'tree'
|
26
|
+
require 'tree' # Load the library
|
27
27
|
|
28
28
|
# ..... Create the root node first. Note that every node has a name and an optional content payload.
|
29
|
-
root_node = Tree::TreeNode.new(
|
29
|
+
root_node = Tree::TreeNode.new('ROOT', 'Root Content')
|
30
30
|
root_node.print_tree
|
31
31
|
|
32
32
|
# ..... Now insert the child nodes. Note that you can "chain" the child insertions for a given path to any depth.
|
33
|
-
root_node << Tree::TreeNode.new(
|
34
|
-
root_node << Tree::TreeNode.new(
|
33
|
+
root_node << Tree::TreeNode.new('CHILD1', 'Child1 Content') << Tree::TreeNode.new('GRANDCHILD1', 'GrandChild1 Content')
|
34
|
+
root_node << Tree::TreeNode.new('CHILD2', 'Child2 Content')
|
35
35
|
|
36
36
|
# ..... Lets print the representation to stdout. This is primarily used for debugging purposes.
|
37
37
|
root_node.print_tree
|
38
38
|
|
39
39
|
# ..... Lets directly access children and grandchildren of the root. The can be "chained" for a given path to any depth.
|
40
|
-
child1 = root_node[
|
41
|
-
grand_child1 = root_node[
|
40
|
+
child1 = root_node['CHILD1']
|
41
|
+
grand_child1 = root_node['CHILD1']['GRANDCHILD1']
|
42
42
|
|
43
43
|
# ..... Now lets retrieve siblings of the current node as an array.
|
44
44
|
siblings_of_child1 = child1.siblings
|
@@ -46,6 +46,9 @@ siblings_of_child1 = child1.siblings
|
|
46
46
|
# ..... Lets retrieve immediate children of the root node as an array.
|
47
47
|
children_of_root = root_node.children
|
48
48
|
|
49
|
+
# ..... Retrieve the parent of a node.
|
50
|
+
parent = child1.parent
|
51
|
+
|
49
52
|
# ..... This is a depth-first and L-to-R pre-ordered traversal.
|
50
53
|
root_node.each { |node| node.content.reverse }
|
51
54
|
|
data/lib/rubytree.rb
CHANGED
data/lib/tree/binarytree.rb
CHANGED
@@ -38,10 +38,9 @@
|
|
38
38
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
39
39
|
#
|
40
40
|
|
41
|
-
|
41
|
+
require_relative '../tree'
|
42
42
|
|
43
43
|
module Tree
|
44
|
-
|
45
44
|
# Provides a Binary tree implementation. This node allows only two child nodes
|
46
45
|
# (left and right child). It also provides direct access to the left or right
|
47
46
|
# child, including assignment to the same.
|
@@ -51,7 +50,6 @@ module Tree
|
|
51
50
|
# @author Anupam Sengupta
|
52
51
|
#
|
53
52
|
class BinaryTreeNode < TreeNode
|
54
|
-
|
55
53
|
# @!group Core Attributes
|
56
54
|
|
57
55
|
# @!attribute [rw] left_child
|
@@ -85,6 +83,7 @@ module Tree
|
|
85
83
|
# @return [Boolean] +true+ if this is the left child of its parent.
|
86
84
|
def is_left_child?
|
87
85
|
return false if is_root?
|
86
|
+
|
88
87
|
self == parent.left_child
|
89
88
|
end
|
90
89
|
|
@@ -95,6 +94,7 @@ module Tree
|
|
95
94
|
# @return [Boolean] +true+ if this is the right child of its parent.
|
96
95
|
def is_right_child?
|
97
96
|
return false if is_root?
|
97
|
+
|
98
98
|
self == parent.right_child
|
99
99
|
end
|
100
100
|
|
@@ -114,12 +114,11 @@ module Tree
|
|
114
114
|
# @raise [ArgumentError] This exception is raised if two children are
|
115
115
|
# already present.
|
116
116
|
def add(child)
|
117
|
-
raise ArgumentError,
|
117
|
+
raise ArgumentError, 'Already has two child nodes' if @children.size == 2
|
118
118
|
|
119
119
|
super(child)
|
120
120
|
end
|
121
121
|
|
122
|
-
|
123
122
|
# Instantiate and insert child nodes from data in a Ruby +Hash+
|
124
123
|
#
|
125
124
|
# This method is used in conjunction with {Tree::TreeNode.from_hash} to
|
@@ -150,12 +149,13 @@ module Tree
|
|
150
149
|
# @raise [ArgumentError] This exception is raised if a non-hash is passed.
|
151
150
|
# @return [Array] Array of child nodes added
|
152
151
|
def add_from_hash(hashed_subtree)
|
153
|
-
raise ArgumentError,
|
152
|
+
raise ArgumentError, 'Too many children'\
|
154
153
|
if hashed_subtree.size + @children.size > 2
|
154
|
+
|
155
155
|
super(hashed_subtree)
|
156
156
|
end
|
157
157
|
|
158
|
-
# Performs
|
158
|
+
# Performs in-order traversal (including this node).
|
159
159
|
#
|
160
160
|
# @yieldparam node [Tree::BinaryTreeNode] Each node (in-order).
|
161
161
|
#
|
@@ -164,29 +164,30 @@ module Tree
|
|
164
164
|
#
|
165
165
|
# @since 0.9.0
|
166
166
|
#
|
167
|
+
# @param [Object] block
|
168
|
+
#
|
167
169
|
# @see #each
|
168
170
|
# @see #preordered_each
|
169
171
|
# @see #postordered_each
|
170
|
-
|
171
|
-
|
172
|
-
return
|
172
|
+
# noinspection RubyUnusedLocalVariable
|
173
|
+
def inordered_each
|
174
|
+
return to_enum unless block_given?
|
173
175
|
|
174
176
|
node_stack = []
|
175
177
|
current_node = self
|
176
178
|
|
177
|
-
until node_stack.empty? and current_node
|
179
|
+
until node_stack.empty? and current_node.nil?
|
178
180
|
if current_node
|
179
181
|
node_stack.push(current_node)
|
180
182
|
current_node = current_node.left_child
|
181
183
|
else
|
182
|
-
current_node = node_stack.pop
|
184
|
+
current_node = node_stack.pop
|
183
185
|
yield current_node
|
184
186
|
current_node = current_node.right_child
|
185
187
|
end
|
186
188
|
end
|
187
189
|
|
188
|
-
|
189
|
-
|
190
|
+
self if block_given?
|
190
191
|
end
|
191
192
|
|
192
193
|
# A protected method to allow insertion of child nodes at the specified
|
@@ -201,7 +202,7 @@ module Tree
|
|
201
202
|
#
|
202
203
|
# @raise [ArgumentError] If the index is out of limits.
|
203
204
|
def set_child_at(child, at_index)
|
204
|
-
raise ArgumentError
|
205
|
+
raise ArgumentError 'A binary tree cannot have more than two children.'\
|
205
206
|
unless (0..1).include? at_index
|
206
207
|
|
207
208
|
@children[at_index] = child
|
@@ -242,9 +243,7 @@ module Tree
|
|
242
243
|
|
243
244
|
# Swaps the left and right child nodes of the receiver node with each other.
|
244
245
|
def swap_children
|
245
|
-
self.left_child, self.right_child =
|
246
|
+
self.left_child, self.right_child = right_child, left_child
|
246
247
|
end
|
247
|
-
|
248
248
|
end
|
249
|
-
|
250
249
|
end
|
data/lib/tree/tree_deps.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
#
|
3
3
|
# = rubytree_deps.rb - Dependencies for RubyTree
|
4
4
|
#
|
5
|
-
# Centralizes and lists the dependencies
|
5
|
+
# Centralizes and lists the dependencies for the RubyTree gem.
|
6
6
|
#
|
7
7
|
# Author:: Anupam Sengupta (anupamsg@gmail.com)
|
8
8
|
#
|
@@ -40,10 +40,10 @@
|
|
40
40
|
require 'structured_warnings'
|
41
41
|
require 'json'
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
43
|
+
require_relative '../tree/version'
|
44
|
+
require_relative '../tree/utils/metrics_methods'
|
45
|
+
require_relative '../tree/utils/path_methods'
|
46
|
+
require_relative '../tree/utils/camel_case_method_handler'
|
47
|
+
require_relative '../tree/utils/json_converter'
|
48
|
+
require_relative '../tree/utils/tree_merge_handler'
|
49
|
+
require_relative '../tree/utils/hash_converter'
|