rubytree 0.5.3 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +3 -1
- data/ChangeLog +13 -0
- data/History.txt +10 -0
- data/README +4 -2
- data/Rakefile +7 -32
- data/TODO +11 -1
- data/lib/tree.rb +65 -101
- data/lib/tree/binarytree.rb +14 -39
- data/test/test_binarytree.rb +2 -35
- data/test/test_tree.rb +62 -49
- metadata +13 -2
data/COPYING
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
http://rubytree.rubyforge.org
|
4
4
|
|
5
|
-
Copyright (c) 2006
|
5
|
+
Copyright (c) 2006-2010 Anupam Sengupta
|
6
6
|
|
7
7
|
All rights reserved.
|
8
8
|
|
@@ -25,3 +25,5 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PRO
|
|
25
25
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
26
26
|
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
27
27
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28
|
+
|
29
|
+
$Id: COPYING 143 2010-01-02 09:10:16Z anupamsg $
|
data/ChangeLog
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
2010-01-03 Anupam Sengupta <anupamsg@gmail.com>
|
2
|
+
|
3
|
+
* lib/tree.rb (Tree::TreeNode#nodeDepth): Added the nodeDepth and
|
4
|
+
nodeHeight methods. Deprecated the depth method. This is the
|
5
|
+
bug-fix for bug # 22535.
|
6
|
+
|
7
|
+
* Rakefile: Added run-time dependency on the 'structured_warnings' gem.
|
8
|
+
|
9
|
+
* test/test_tree.rb: Added new test cases for the new nodeDepth
|
10
|
+
and nodeHeight methods.
|
11
|
+
|
1
12
|
2010-01-01 Anupam Sengupta <anupamsg@gmail.com>
|
2
13
|
|
3
14
|
* Rakefile: Have migrated 100% to usage of Hoe for the rake
|
@@ -179,3 +190,5 @@
|
|
179
190
|
* LICENSE: Added the BSD LICENSE file.
|
180
191
|
|
181
192
|
* Changelog: Added the Changelog file.
|
193
|
+
|
194
|
+
$Id: ChangeLog 154 2010-01-03 18:01:28Z anupamsg $
|
data/History.txt
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
=== 0.6.0 / 2010-01-03
|
2
|
+
|
3
|
+
* Fixed the bug#22535[http://rubyforge.org/tracker/index.php?func=detail&aid=22535&group_id=1215&atid=4793] where the
|
4
|
+
depth method was actually returning height+1 (not the depth).
|
5
|
+
|
6
|
+
* Marked the Tree::TreeNode#depth method as *deprecated* (and introduced the run-time dependency on
|
7
|
+
structured-warnings[http://github.com/schmidt/structured_warnings] gem).
|
8
|
+
|
1
9
|
=== 0.5.3 / 2009-12-31
|
2
10
|
|
3
11
|
* Cleanup of the build system to exclusively use Hoe.
|
@@ -24,3 +32,5 @@
|
|
24
32
|
=== 0.4.2 / 2007-10-01
|
25
33
|
|
26
34
|
* Minor code refactoring. Changes in the Rakefile.
|
35
|
+
|
36
|
+
$Id: History.txt 156 2010-01-03 18:06:22Z anupamsg $
|
data/README
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
\/ \_/\__,_|_.__/ \__, |\__|_| \___|\___|
|
7
7
|
|___/
|
8
8
|
|
9
|
-
(c) 2006, 2007, 2008, 2009 Anupam Sengupta
|
9
|
+
(c) 2006, 2007, 2008, 2009, 2010 Anupam Sengupta
|
10
10
|
http://rubytree.rubyforge.org
|
11
11
|
|
12
12
|
== DESCRIPTION:
|
@@ -57,6 +57,8 @@ As an example, the following code-snippet implements this tree structure:
|
|
57
57
|
== REQUIREMENTS:
|
58
58
|
|
59
59
|
* Ruby 1.8+ (http://www.ruby-lang.org)
|
60
|
+
* Hoe (http://seattlerb.rubyforge.org/hoe/Hoe.html) Rubygem
|
61
|
+
* structured_warnings (http://github.com/schmidt/structured_warnings) Rubygem
|
60
62
|
|
61
63
|
== INSTALL:
|
62
64
|
|
@@ -147,4 +149,4 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
|
|
147
149
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
148
150
|
|
149
151
|
|
150
|
-
(Document Revision: $Revision:
|
152
|
+
(Document Revision: $Revision: 158 $ by $Author: anupamsg $)
|
data/Rakefile
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
+
# -*- mode: ruby; -*-
|
2
|
+
#
|
1
3
|
# Rakefile
|
2
4
|
#
|
3
|
-
# $Revision:
|
4
|
-
# $Name: $
|
5
|
+
# $Revision: 154 $ by $Author: anupamsg $ on $Date: 2010-01-03 23:31:28 +0530 (Sun, 03 Jan 2010) $
|
5
6
|
#
|
6
7
|
# Copyright (c) 2006, 2007 Anupam Sengupta
|
7
8
|
#
|
@@ -49,7 +50,10 @@ begin
|
|
49
50
|
self.extra_rdoc_files = ['README', 'COPYING', 'ChangeLog']
|
50
51
|
self.url = "http://rubytree.rubyforge.org"
|
51
52
|
self.readme_file = 'README'
|
52
|
-
self.
|
53
|
+
self.extra_deps << ['structured_warnings', '>= 0.1.3']
|
54
|
+
# Set the RDoc Options.
|
55
|
+
self.spec_extras[:rdoc_options] = ['--main', 'README', '--line-numbers']
|
56
|
+
self.spec_extras[:has_rdoc] = true
|
53
57
|
|
54
58
|
# Now the publishing settings
|
55
59
|
self.remote_rdoc_dir = 'rdoc'
|
@@ -99,32 +103,3 @@ rescue LoadError
|
|
99
103
|
|
100
104
|
END
|
101
105
|
end
|
102
|
-
|
103
|
-
#Rakefile,v $
|
104
|
-
# Revision 1.21 2007/07/21 05:14:43 anupamsg
|
105
|
-
# Added a VERSION constant to the Tree module,
|
106
|
-
# and using the same in the Rakefile.
|
107
|
-
#
|
108
|
-
# Revision 1.20 2007/07/21 03:24:25 anupamsg
|
109
|
-
# Minor edits to parameter names. User visible functionality does not change.
|
110
|
-
#
|
111
|
-
# Revision 1.19 2007/07/19 02:16:01 anupamsg
|
112
|
-
# Release 0.4.0 (and minor fix in Rakefile).
|
113
|
-
#
|
114
|
-
# Revision 1.18 2007/07/18 20:15:06 anupamsg
|
115
|
-
# Added two predicate methods in BinaryTreeNode to determine whether a node
|
116
|
-
# is a left or a right node.
|
117
|
-
#
|
118
|
-
# Revision 1.17 2007/07/18 07:17:34 anupamsg
|
119
|
-
# Fixed a issue where TreeNode.ancestors was shadowing Module.ancestors. This method
|
120
|
-
# has been renamed to TreeNode.parentage.
|
121
|
-
#
|
122
|
-
# Revision 1.16 2007/07/17 05:34:03 anupamsg
|
123
|
-
# Added an optional tags Rake-task for generating the TAGS file for Emacs.
|
124
|
-
#
|
125
|
-
# Revision 1.15 2007/07/17 04:42:45 anupamsg
|
126
|
-
# Minor fixes to the Rakefile.
|
127
|
-
#
|
128
|
-
# Revision 1.14 2007/07/17 03:39:28 anupamsg
|
129
|
-
# Moved the CVS Log keyword to end of the files.
|
130
|
-
#
|
data/TODO
CHANGED
@@ -1,9 +1,19 @@
|
|
1
1
|
# -*- mode: org; coding: utf-8-unix; -*-
|
2
2
|
|
3
|
-
* TODO
|
3
|
+
* TODO Create the basic UML diagrams and upload to the Site
|
4
|
+
DEADLINE: <2010-01-04 Mon>
|
5
|
+
|
6
|
+
* DONE Fix bug # [[http://rubyforge.org/tracker/index.php%3Ffunc%3Ddetail&aid%3D22535&group_id%3D1215&atid%3D4793][22535]]: The method Tree::TreeNode#depth is a misnomer. The current definition actually provides the height function.
|
7
|
+
DEADLINE: <2010-01-09 Sat> CLOSED: [2010-01-03 Sun 22:15]
|
8
|
+
|
4
9
|
* TODO Add a YAML export method to the TreeNode class.
|
5
10
|
|
11
|
+
* DONE Get the version control moved from CVS to Subversion (request submitted to RubyForge)
|
12
|
+
CLOSED: [2010-01-02 Sat 17:58]
|
13
|
+
|
6
14
|
* DONE Add logic in Rakefile to read the file list from Manifest.txt file.
|
7
15
|
CLOSED: [2009-12-31 Thu 23:37]
|
8
16
|
|
9
17
|
|
18
|
+
|
19
|
+
$Id: TODO 154 2010-01-03 18:01:28Z anupamsg $
|
data/lib/tree.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
# tree.rb
|
2
2
|
#
|
3
|
-
# $Revision:
|
4
|
-
# $Name: $
|
3
|
+
# $Revision: 154 $ by $Author: anupamsg $ on $Date: 2010-01-03 23:31:28 +0530 (Sun, 03 Jan 2010) $
|
5
4
|
#
|
6
5
|
# = tree.rb - Generic Multi-way Tree implementation
|
7
6
|
#
|
@@ -12,7 +11,7 @@
|
|
12
11
|
# Author:: Anupam Sengupta (anupamsg@gmail.com)
|
13
12
|
#
|
14
13
|
|
15
|
-
# Copyright (c) 2006, 2007 Anupam Sengupta
|
14
|
+
# Copyright (c) 2006, 2007, 2008, 2009, 2010 Anupam Sengupta
|
16
15
|
#
|
17
16
|
# All rights reserved.
|
18
17
|
#
|
@@ -42,22 +41,24 @@
|
|
42
41
|
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
43
42
|
#
|
44
43
|
|
44
|
+
require 'structured_warnings' # To enable the deprecation of the depth method.
|
45
|
+
|
45
46
|
# This module provides a TreeNode class which is the primary class for all
|
46
47
|
# nodes represented in the Tree.
|
47
48
|
# This module mixes in the Enumerable module.
|
48
49
|
module Tree
|
49
50
|
|
50
51
|
# Rubytree Package Version
|
51
|
-
VERSION = '0.
|
52
|
+
VERSION = '0.6.0'
|
52
53
|
|
53
54
|
# == TreeNode Class Description
|
54
55
|
#
|
55
56
|
# The node class for the tree representation. the nodes are +named+ and have a
|
56
57
|
# place-holder for the node data (i.e., the `content' of the node). The node
|
57
|
-
# names are
|
58
|
+
# names are required to be unique. In addition, the node provides navigation
|
58
59
|
# methods to traverse the tree.
|
59
60
|
#
|
60
|
-
# A node can have any number of child nodes attached to it.
|
61
|
+
# A node can have any number of child nodes attached to it. Note that while
|
61
62
|
# this implementation does not support directed graphs, the class itself makes
|
62
63
|
# no restrictions on associating a node's CONTENT with multiple parent nodes.
|
63
64
|
#
|
@@ -103,18 +104,22 @@ module Tree
|
|
103
104
|
# myTreeRoot.each { |node| node.content.reverse }
|
104
105
|
#
|
105
106
|
# myTreeRoot.remove!(child1) # Remove the child
|
107
|
+
#
|
106
108
|
class TreeNode
|
107
109
|
include Enumerable
|
108
110
|
|
109
|
-
|
110
|
-
|
111
|
+
# Name of this Node
|
112
|
+
attr_reader :name
|
113
|
+
# Content of this Node
|
114
|
+
attr_accessor :content
|
115
|
+
# Parent of this Node
|
116
|
+
attr_reader :parent
|
111
117
|
|
112
|
-
|
113
|
-
#
|
114
|
-
# Name of the node is expected to be unique across the
|
115
|
-
# tree.
|
118
|
+
|
119
|
+
# Constructor which expects the name of the node.
|
120
|
+
# Name of the node is expected to be unique across the tree.
|
116
121
|
#
|
117
|
-
# The content can be of any type,
|
122
|
+
# The content can be of any type, defaults to +nil+.
|
118
123
|
def initialize(name, content = nil)
|
119
124
|
raise "Node name HAS to be provided" if name == nil
|
120
125
|
@name = name
|
@@ -125,12 +130,13 @@ module Tree
|
|
125
130
|
@children = []
|
126
131
|
end
|
127
132
|
|
128
|
-
# Returns a copy of this node, with the parent and children links removed.
|
133
|
+
# Returns a copy of this node, with the parent and children links removed. The original node remains attached to
|
134
|
+
# its tree.
|
129
135
|
def detached_copy
|
130
136
|
Tree::TreeNode.new(@name, @content ? @content.clone : nil)
|
131
137
|
end
|
132
138
|
|
133
|
-
# Print the string representation of this node.
|
139
|
+
# Print the string representation of this node. This is primary for debugging purposes.
|
134
140
|
def to_s
|
135
141
|
"Node Name: #{@name}" +
|
136
142
|
" Content: " + (@content || "<Empty>") +
|
@@ -140,7 +146,7 @@ module Tree
|
|
140
146
|
end
|
141
147
|
|
142
148
|
# Returns an array of ancestors in reversed order (the first element is the
|
143
|
-
# immediate parent). Returns nil if this is
|
149
|
+
# immediate parent). Returns +nil+ if this is the root node.
|
144
150
|
def parentage
|
145
151
|
return nil if isRoot?
|
146
152
|
|
@@ -209,7 +215,7 @@ module Tree
|
|
209
215
|
self
|
210
216
|
end
|
211
217
|
|
212
|
-
#
|
218
|
+
# Returns +true+ if this node has any associated content.
|
213
219
|
def hasContent?
|
214
220
|
@content != nil
|
215
221
|
end
|
@@ -219,19 +225,19 @@ module Tree
|
|
219
225
|
@parent = nil
|
220
226
|
end
|
221
227
|
|
222
|
-
#
|
228
|
+
# Returns +true+ if this is a root node. Note that
|
223
229
|
# orphaned children will also be reported as root nodes.
|
224
230
|
def isRoot?
|
225
231
|
@parent == nil
|
226
232
|
end
|
227
233
|
|
228
|
-
#
|
234
|
+
# Returns +true+ if this node has any immediate child nodes.
|
229
235
|
def hasChildren?
|
230
236
|
@children.length != 0
|
231
237
|
end
|
232
238
|
|
233
|
-
#
|
234
|
-
# any children
|
239
|
+
# Returns +true+ if this node is a 'leaf' - i.e., one without
|
240
|
+
# any children.
|
235
241
|
def isLeaf?
|
236
242
|
!hasChildren?
|
237
243
|
end
|
@@ -272,7 +278,7 @@ module Tree
|
|
272
278
|
each(&block)
|
273
279
|
end
|
274
280
|
|
275
|
-
# Performs breadth first traversal of the tree
|
281
|
+
# Performs breadth first traversal of the tree starting at this node. The
|
276
282
|
# traversal in a given level is from left to right.
|
277
283
|
def breadth_each &block
|
278
284
|
node_queue = [self] # Create a queue with self as the initial entry
|
@@ -308,8 +314,11 @@ module Tree
|
|
308
314
|
end
|
309
315
|
end
|
310
316
|
|
311
|
-
# Returns the total number of nodes in this tree,
|
312
|
-
#
|
317
|
+
# Returns the total number of nodes in this tree, including this node.
|
318
|
+
#
|
319
|
+
# Size of the tree is defined as:
|
320
|
+
#
|
321
|
+
# Size:: The total number nodes in the subtree including this node.
|
313
322
|
def size
|
314
323
|
@children.inject(1) {|sum, node| sum + node.size}
|
315
324
|
end
|
@@ -337,7 +346,7 @@ module Tree
|
|
337
346
|
children { |child| child.printTree(level + 1)}
|
338
347
|
end
|
339
348
|
|
340
|
-
# Returns the root for this tree. Root's root is itself
|
349
|
+
# Returns the root for this tree. Root's root is itself (Hence beware of any loop that can become infinite!)
|
341
350
|
def root
|
342
351
|
root = self
|
343
352
|
root = root.parent while !root.isRoot?
|
@@ -455,15 +464,42 @@ module Tree
|
|
455
464
|
end
|
456
465
|
end
|
457
466
|
|
458
|
-
# Returns
|
459
|
-
#
|
467
|
+
# Returns height of the (sub)tree from this node. Height of a node is defined as:
|
468
|
+
#
|
469
|
+
# Height:: Length of the longest downward path to a leaf from the node. Height of from the root node is height of
|
470
|
+
# the tree. The height of a leaf node is zero.
|
471
|
+
def nodeHeight
|
472
|
+
return 0 if isLeaf?
|
473
|
+
1 + @children.collect { |child| child.nodeHeight }.max
|
474
|
+
end
|
475
|
+
|
476
|
+
# Returns depth this node in its (sub)tree. Depth of a node is defined as:
|
477
|
+
#
|
478
|
+
# Depth:: Length of the node's path to its root. Depth of a root node is zero.
|
479
|
+
#
|
480
|
+
# Note that the deprecated method Tree::TreeNode#depth was incorrectly computing this value. Please replace all
|
481
|
+
# calls to the old method by this one.
|
482
|
+
def nodeDepth
|
483
|
+
return 0 if isRoot?
|
484
|
+
1 + parent.nodeDepth
|
485
|
+
end
|
486
|
+
|
487
|
+
# Returns depth of the tree from this node. A single leaf node has a depth of 1.
|
488
|
+
#
|
489
|
+
# This method is *DEPRECATED* and may be removed in the subsequent releases. Note that per convention, the value
|
490
|
+
# returned by this method is actually the _height_ + 1 of the node, *not* the _depth_.
|
491
|
+
#
|
492
|
+
# For correct and conventional behavior, please use Tree::TreeNode#nodeDepth and Tree::TreeNode#nodeHeight methods
|
493
|
+
# instead.
|
460
494
|
def depth
|
495
|
+
warn DeprecatedMethodWarning, 'This method is deprecated. Please use nodeDepth or nodeHeight instead (bug # 22535)'
|
461
496
|
return 1 if isLeaf?
|
462
497
|
1 + @children.collect { |child| child.depth }.max
|
463
498
|
end
|
464
499
|
|
465
|
-
# Returns breadth of the tree at this node level. A single node has a
|
466
|
-
#
|
500
|
+
# Returns breadth of the tree at this node level. A single node has a breadth of 1. Breadth is defined to be the
|
501
|
+
# number of nodes present as siblings to this node + 1 (this node itself), i.e., the number of children the parent
|
502
|
+
# of this node has.
|
467
503
|
def breadth
|
468
504
|
return 1 if isRoot?
|
469
505
|
parent.children.size
|
@@ -473,75 +509,3 @@ module Tree
|
|
473
509
|
|
474
510
|
end
|
475
511
|
end
|
476
|
-
|
477
|
-
# $Log: tree.rb,v $
|
478
|
-
# Revision 1.31 2009/12/31 19:28:50 anupamsg
|
479
|
-
# Migrated the rake build and install mechanism to use Hoe (http://seattlerb.rubyforge.org/hoe/Hoe.html) exclusively.
|
480
|
-
# Have removed all legacy custom tasks (including the old GemSpec). Have also made some formatting modifications to the
|
481
|
-
# documentation. The release itself does not contain any user visible changes from a functionality point of view.
|
482
|
-
#
|
483
|
-
# Revision 1.30 2009/10/05 18:27:10 anupamsg
|
484
|
-
# Minor editing of the comment and an addition to the .cvsignore.
|
485
|
-
#
|
486
|
-
# Revision 1.29 2007/12/22 00:28:59 anupamsg
|
487
|
-
# Added more test cases, and enabled ZenTest compatibility.
|
488
|
-
#
|
489
|
-
# Revision 1.28 2007/12/20 03:19:33 anupamsg
|
490
|
-
# * README (Module): Modified the install instructions from source.
|
491
|
-
# (Module): Updated the minor version number.
|
492
|
-
#
|
493
|
-
# Revision 1.27 2007/12/20 03:00:03 anupamsg
|
494
|
-
# Minor code changes. Removed self_initialize from the protected methods' list.
|
495
|
-
#
|
496
|
-
# Revision 1.26 2007/12/20 02:50:04 anupamsg
|
497
|
-
# (Tree::TreeNode): Removed the spurious self_initialize from the protected list.
|
498
|
-
#
|
499
|
-
# Revision 1.25 2007/12/19 20:28:05 anupamsg
|
500
|
-
# Removed the unnecesary self_initialize method.
|
501
|
-
#
|
502
|
-
# Revision 1.24 2007/12/19 06:39:17 anupamsg
|
503
|
-
# Removed the unnecessary field and record separator constants. Also updated the
|
504
|
-
# history.txt file.
|
505
|
-
#
|
506
|
-
# Revision 1.23 2007/12/19 06:25:00 anupamsg
|
507
|
-
# (Tree::TreeNode): Minor fix to the comments. Also fixed the private/protected
|
508
|
-
# scope issue with the createDumpRep method.
|
509
|
-
#
|
510
|
-
# Revision 1.22 2007/12/19 06:22:03 anupamsg
|
511
|
-
# Updated the marshalling logic to correctly handle non-string content. This
|
512
|
-
# should fix the bug # 15614 ("When dumping with an Object as the content, you get
|
513
|
-
# a delimiter collision")
|
514
|
-
#
|
515
|
-
# Revision 1.21 2007/12/19 02:24:17 anupamsg
|
516
|
-
# Updated the marshalling logic to handle non-string contents on the nodes.
|
517
|
-
#
|
518
|
-
# Revision 1.20 2007/10/10 08:42:57 anupamsg
|
519
|
-
# Release 0.4.3
|
520
|
-
#
|
521
|
-
# Revision 1.19 2007/08/31 01:16:27 anupamsg
|
522
|
-
# Added breadth and pre-order traversals for the tree. Also added a method
|
523
|
-
# to return the detached copy of a node from the tree.
|
524
|
-
#
|
525
|
-
# Revision 1.18 2007/07/21 05:14:44 anupamsg
|
526
|
-
# Added a VERSION constant to the Tree module,
|
527
|
-
# and using the same in the Rakefile.
|
528
|
-
#
|
529
|
-
# Revision 1.17 2007/07/21 03:24:25 anupamsg
|
530
|
-
# Minor edits to parameter names. User visible functionality does not change.
|
531
|
-
#
|
532
|
-
# Revision 1.16 2007/07/18 23:38:55 anupamsg
|
533
|
-
# Minor updates to tree.rb
|
534
|
-
#
|
535
|
-
# Revision 1.15 2007/07/18 22:11:50 anupamsg
|
536
|
-
# Added depth and breadth methods for the TreeNode.
|
537
|
-
#
|
538
|
-
# Revision 1.14 2007/07/18 19:33:27 anupamsg
|
539
|
-
# Added a new binary tree implementation.
|
540
|
-
#
|
541
|
-
# Revision 1.13 2007/07/18 07:17:34 anupamsg
|
542
|
-
# Fixed a issue where TreeNode.ancestors was shadowing Module.ancestors. This method
|
543
|
-
# has been renamed to TreeNode.parentage.
|
544
|
-
#
|
545
|
-
# Revision 1.12 2007/07/17 03:39:28 anupamsg
|
546
|
-
# Moved the CVS Log keyword to end of the files.
|
547
|
-
#
|
data/lib/tree/binarytree.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
# binarytree.rb
|
2
2
|
#
|
3
|
-
# $Revision:
|
4
|
-
# $Name: $
|
3
|
+
# $Revision: 160 $ by $Author: anupamsg $ on $Date: 2010-01-03 23:58:07 +0530 (Sun, 03 Jan 2010) $
|
5
4
|
#
|
6
5
|
# = binarytree.rb - Binary Tree implementation
|
7
6
|
#
|
@@ -12,7 +11,7 @@
|
|
12
11
|
# Author:: Anupam Sengupta (anupamsg@gmail.com)
|
13
12
|
#
|
14
13
|
|
15
|
-
# Copyright (c) 2007 Anupam Sengupta
|
14
|
+
# Copyright (c) 2007, 2008, 2009, 2010 Anupam Sengupta
|
16
15
|
#
|
17
16
|
# All rights reserved.
|
18
17
|
#
|
@@ -46,15 +45,14 @@ require 'tree'
|
|
46
45
|
|
47
46
|
module Tree
|
48
47
|
|
49
|
-
# Provides a Binary tree implementation. This tree node allows only two child
|
50
|
-
#
|
51
|
-
# and right children, including assignment to the same.
|
48
|
+
# Provides a Binary tree implementation. This tree node allows only two child nodes (left and right childs). It also
|
49
|
+
# provides direct access to the left or right child, including assignment to the same.
|
52
50
|
class BinaryTreeNode < TreeNode
|
53
51
|
|
54
|
-
# Adds the specified child node to the receiver node. The child node's
|
55
|
-
#
|
56
|
-
#
|
57
|
-
#
|
52
|
+
# Adds the specified child node to the receiver node. The child node's parent is set to be the receiver. The child
|
53
|
+
# nodes are added in the order of addition, i.e., the first child added becomes the left node, and the second child
|
54
|
+
# will be the second node.
|
55
|
+
#
|
58
56
|
# If only one child is present, then this will be the left child.
|
59
57
|
def add(child)
|
60
58
|
raise "Already has two child nodes" if @children.size == 2
|
@@ -62,15 +60,13 @@ module Tree
|
|
62
60
|
super(child)
|
63
61
|
end
|
64
62
|
|
65
|
-
# Returns the left child node. Note that
|
66
|
-
# left Child == first Child
|
63
|
+
# Returns the left child node. Note that left Child == first Child.
|
67
64
|
def leftChild
|
68
65
|
children.first
|
69
66
|
end
|
70
67
|
|
71
|
-
# Returns the right child node. Note that
|
72
|
-
#
|
73
|
-
# Returns nil if the right child does not exist.
|
68
|
+
# Returns the right child node. Note that right child == last child unless there is only one child. Returns +nil+
|
69
|
+
# if the right child does not exist.
|
74
70
|
def rightChild
|
75
71
|
children[1]
|
76
72
|
end
|
@@ -87,21 +83,19 @@ module Tree
|
|
87
83
|
@childrenHash[child.name] = child if child # Assign the name mapping
|
88
84
|
end
|
89
85
|
|
90
|
-
# Returns true if this is the left child of its parent.
|
91
|
-
# if this is the root node.
|
86
|
+
# Returns +true+ if this is the left child of its parent. Always returns +false+ if this is the root node.
|
92
87
|
def isLeftChild?
|
93
88
|
return nil if isRoot?
|
94
89
|
self == parent.leftChild
|
95
90
|
end
|
96
91
|
|
97
|
-
# Returns true if this is the right child of its parent. Always returns false
|
98
|
-
# if this is the root node.
|
92
|
+
# Returns +true+ if this is the right child of its parent. Always returns +false+ if this is the root node.
|
99
93
|
def isRightChild?
|
100
94
|
return nil if isRoot?
|
101
95
|
self == parent.rightChild
|
102
96
|
end
|
103
97
|
|
104
|
-
# Swaps the left and right
|
98
|
+
# Swaps the left and right child nodes with each other.
|
105
99
|
def swap_children
|
106
100
|
tempChild = leftChild
|
107
101
|
self.leftChild= rightChild
|
@@ -110,22 +104,3 @@ module Tree
|
|
110
104
|
end
|
111
105
|
|
112
106
|
end
|
113
|
-
|
114
|
-
# $Log: binarytree.rb,v $
|
115
|
-
# Revision 1.5 2007/12/18 23:11:29 anupamsg
|
116
|
-
# Minor documentation changes in the binarytree class.
|
117
|
-
#
|
118
|
-
# Revision 1.4 2007/08/30 22:08:58 anupamsg
|
119
|
-
# Added a new swap_children method for Binary Tree. Also added minor
|
120
|
-
# documentation and test updates.
|
121
|
-
#
|
122
|
-
# Revision 1.3 2007/07/21 03:24:25 anupamsg
|
123
|
-
# Minor edits to parameter names. User visible functionality does not change.
|
124
|
-
#
|
125
|
-
# Revision 1.2 2007/07/18 20:15:06 anupamsg
|
126
|
-
# Added two predicate methods in BinaryTreeNode to determine whether a node
|
127
|
-
# is a left or a right node.
|
128
|
-
#
|
129
|
-
# Revision 1.1 2007/07/18 19:33:27 anupamsg
|
130
|
-
# Added a new binary tree implementation.
|
131
|
-
#
|
data/test/test_binarytree.rb
CHANGED
@@ -2,10 +2,9 @@
|
|
2
2
|
|
3
3
|
# test_binarytree.rb
|
4
4
|
#
|
5
|
-
# $Revision:
|
6
|
-
# $Name: $
|
5
|
+
# $Revision: 146 $ by $Author: anupamsg $ on $Date: 2010-01-02 14:53:30 +0530 (Sat, 02 Jan 2010) $
|
7
6
|
#
|
8
|
-
# Copyright (c) 2006, 2007 Anupam Sengupta
|
7
|
+
# Copyright (c) 2006, 2007, 2008, 2009, 2010 Anupam Sengupta
|
9
8
|
#
|
10
9
|
# All rights reserved.
|
11
10
|
#
|
@@ -170,35 +169,3 @@ module TestTree
|
|
170
169
|
end
|
171
170
|
end
|
172
171
|
end
|
173
|
-
|
174
|
-
# $Log: test_binarytree.rb,v $
|
175
|
-
# Revision 1.5 2007/12/22 00:28:59 anupamsg
|
176
|
-
# Added more test cases, and enabled ZenTest compatibility.
|
177
|
-
#
|
178
|
-
# Revision 1.4 2007/12/18 23:11:29 anupamsg
|
179
|
-
# Minor documentation changes in the binarytree class.
|
180
|
-
#
|
181
|
-
# Revision 1.3 2007/10/02 03:07:30 anupamsg
|
182
|
-
# * Rakefile: Added an optional task for rcov code coverage.
|
183
|
-
#
|
184
|
-
# * test/test_binarytree.rb: Removed the unnecessary dependency on "Person" class.
|
185
|
-
#
|
186
|
-
# * test/test_tree.rb: Removed dependency on the redundant "Person" class.
|
187
|
-
#
|
188
|
-
# Revision 1.2 2007/08/30 22:06:13 anupamsg
|
189
|
-
# Added a new swap_children method for the Binary Tree class.
|
190
|
-
# Also made minor documentation updates and test additions.
|
191
|
-
#
|
192
|
-
# Revision 1.1 2007/07/21 04:52:37 anupamsg
|
193
|
-
# Renamed the test files.
|
194
|
-
#
|
195
|
-
# Revision 1.4 2007/07/19 02:03:57 anupamsg
|
196
|
-
# Minor syntax correction.
|
197
|
-
#
|
198
|
-
# Revision 1.3 2007/07/19 02:02:12 anupamsg
|
199
|
-
# Removed useless files (including rdoc, which should be generated for each release.
|
200
|
-
#
|
201
|
-
# Revision 1.2 2007/07/18 20:15:06 anupamsg
|
202
|
-
# Added two predicate methods in BinaryTreeNode to determine whether a node
|
203
|
-
# is a left or a right node.
|
204
|
-
#
|
data/test/test_tree.rb
CHANGED
@@ -2,10 +2,9 @@
|
|
2
2
|
|
3
3
|
# testtree.rb
|
4
4
|
#
|
5
|
-
# $Revision:
|
6
|
-
# $Name: $
|
5
|
+
# $Revision: 154 $ by $Author: anupamsg $ on $Date: 2010-01-03 23:31:28 +0530 (Sun, 03 Jan 2010) $
|
7
6
|
#
|
8
|
-
# Copyright (c) 2006, 2007 Anupam Sengupta
|
7
|
+
# Copyright (c) 2006, 2007, 2008, 2009, 2010 Anupam Sengupta
|
9
8
|
#
|
10
9
|
# All rights reserved.
|
11
10
|
#
|
@@ -37,6 +36,7 @@
|
|
37
36
|
|
38
37
|
require 'test/unit'
|
39
38
|
require 'tree'
|
39
|
+
require 'structured_warnings'
|
40
40
|
|
41
41
|
module TestTree
|
42
42
|
# Test class for the Tree node.
|
@@ -82,6 +82,7 @@ module TestTree
|
|
82
82
|
@root = nil
|
83
83
|
end
|
84
84
|
|
85
|
+
# This test is for the root alone - without any children being linked
|
85
86
|
def test_root_setup
|
86
87
|
assert_not_nil(@root, "Root cannot be nil")
|
87
88
|
assert_nil(@root.parent, "Parent of root node should be nil")
|
@@ -92,17 +93,20 @@ module TestTree
|
|
92
93
|
assert(!@root.hasChildren?, "Cannot have any children")
|
93
94
|
assert(@root.hasContent?, "This root should have content")
|
94
95
|
assert_equal(1, @root.size, "Number of nodes should be one")
|
95
|
-
assert_nil(@root.siblings, "
|
96
|
+
assert_nil(@root.siblings, "This root does not have any children")
|
96
97
|
|
98
|
+
assert_equal(0, @root.nodeHeight, "Root's height before adding any children is 0")
|
97
99
|
assert_raise(RuntimeError) { Tree::TreeNode.new(nil) }
|
98
100
|
end
|
99
101
|
|
102
|
+
# This test is for the state after the children are linked to the root
|
100
103
|
def test_root
|
101
104
|
loadChildren
|
102
105
|
|
103
106
|
assert_same(@root, @root.root, "Root's root is self")
|
104
107
|
assert_same(@root, @child1.root, "Root should be ROOT")
|
105
108
|
assert_same(@root, @child4.root, "Root should be ROOT")
|
109
|
+
assert_equal(2, @root.nodeHeight, "Root's height after adding the children should be 2")
|
106
110
|
end
|
107
111
|
|
108
112
|
def test_hasContent_eh
|
@@ -237,6 +241,7 @@ module TestTree
|
|
237
241
|
def test_add
|
238
242
|
assert(!@root.hasChildren?, "Should not have any children")
|
239
243
|
|
244
|
+
assert_equal(1, @root.size, "Should have 1 node (the root)")
|
240
245
|
@root.add(@child1)
|
241
246
|
|
242
247
|
@root << @child2
|
@@ -310,10 +315,15 @@ module TestTree
|
|
310
315
|
loadChildren
|
311
316
|
|
312
317
|
assert(@root.hasChildren?, "Should have children")
|
313
|
-
assert_equal(5, @root.size, "Should have
|
318
|
+
assert_equal(5, @root.size, "Should have five nodes")
|
314
319
|
assert(@child3.hasChildren?, "Should have children")
|
315
320
|
assert(!@child3.isLeaf?, "Should not be a leaf")
|
316
321
|
|
322
|
+
assert_equal(1, @child3.nodeHeight, "The subtree at Child 3 should have a height of 1")
|
323
|
+
for child in [@child1, @child2, @child4]
|
324
|
+
assert_equal(0, child.nodeHeight, "The subtree at #{child.name} should have a height of 0")
|
325
|
+
end
|
326
|
+
|
317
327
|
children = []
|
318
328
|
for child in @root.children
|
319
329
|
children << child
|
@@ -498,22 +508,62 @@ module TestTree
|
|
498
508
|
assert_same(pers, @root.content, "Content should be the same")
|
499
509
|
end
|
500
510
|
|
501
|
-
# Test the depth computation algorithm
|
511
|
+
# Test the depth computation algorithm. Note that this is an incorrect computation and actually returns height+1
|
512
|
+
# instead of depth. This method has been deprecated in this release and may be removed in the future.
|
502
513
|
def test_depth
|
503
|
-
|
514
|
+
assert_warn(DeprecatedMethodWarning) do
|
515
|
+
assert_equal(1, @root.depth, "A single node's depth is 1")
|
516
|
+
|
517
|
+
@root << @child1
|
518
|
+
assert_equal(2, @root.depth, "This should be of depth 2")
|
519
|
+
|
520
|
+
@root << @child2
|
521
|
+
assert_equal(2, @root.depth, "This should be of depth 2")
|
522
|
+
|
523
|
+
@child2 << @child3
|
524
|
+
assert_equal(3, @root.depth, "This should be of depth 3")
|
525
|
+
assert_equal(2, @child2.depth, "This should be of depth 2")
|
526
|
+
|
527
|
+
@child3 << @child4
|
528
|
+
assert_equal(4, @root.depth, "This should be of depth 4")
|
529
|
+
end
|
530
|
+
end
|
531
|
+
|
532
|
+
# Test the height computation algorithm
|
533
|
+
def test_nodeHeight
|
534
|
+
assert_equal(0, @root.nodeHeight, "A single node's height is 0")
|
504
535
|
|
505
536
|
@root << @child1
|
506
|
-
assert_equal(
|
537
|
+
assert_equal(1, @root.nodeHeight, "This should be of height 1")
|
538
|
+
assert_equal(0, @child1.nodeHeight, "This should be of height 0")
|
507
539
|
|
508
540
|
@root << @child2
|
509
|
-
assert_equal(
|
541
|
+
assert_equal(1, @root.nodeHeight, "This should be of height 1")
|
542
|
+
assert_equal(0, @child2.nodeHeight, "This should be of height 0")
|
510
543
|
|
511
544
|
@child2 << @child3
|
512
|
-
assert_equal(
|
513
|
-
assert_equal(
|
545
|
+
assert_equal(2, @root.nodeHeight, "This should be of height 2")
|
546
|
+
assert_equal(1, @child2.nodeHeight, "This should be of height 1")
|
547
|
+
assert_equal(0, @child3.nodeHeight, "This should be of height 0")
|
514
548
|
|
515
549
|
@child3 << @child4
|
516
|
-
assert_equal(
|
550
|
+
assert_equal(3, @root.nodeHeight, "This should be of height 3")
|
551
|
+
assert_equal(2, @child2.nodeHeight, "This should be of height 2")
|
552
|
+
assert_equal(1, @child3.nodeHeight, "This should be of height 1")
|
553
|
+
assert_equal(0, @child4.nodeHeight, "This should be of height 0")
|
554
|
+
end
|
555
|
+
|
556
|
+
# Test the depth computation algorithm. Note that this is the correct depth computation. The original
|
557
|
+
# Tree::TreeNode#depth was incorrectly computing the height of the node - instead of its depth.
|
558
|
+
def test_nodeDepth
|
559
|
+
assert_equal(0, @root.nodeDepth, "A root node's depth is 0")
|
560
|
+
|
561
|
+
loadChildren
|
562
|
+
for child in [@child1, @child2, @child3]
|
563
|
+
assert_equal(1, child.nodeDepth, "Node #{child.name} should have depth 1")
|
564
|
+
end
|
565
|
+
|
566
|
+
assert_equal(2, @child4.nodeDepth, "Child 4 should have depth 2")
|
517
567
|
end
|
518
568
|
|
519
569
|
# Test the breadth computation algorithm
|
@@ -679,40 +729,3 @@ module TestTree
|
|
679
729
|
end
|
680
730
|
|
681
731
|
__END__
|
682
|
-
|
683
|
-
# $Log: test_tree.rb,v $
|
684
|
-
# Revision 1.6 2007/12/22 00:28:59 anupamsg
|
685
|
-
# Added more test cases, and enabled ZenTest compatibility.
|
686
|
-
#
|
687
|
-
# Revision 1.5 2007/12/19 02:24:18 anupamsg
|
688
|
-
# Updated the marshalling logic to handle non-string contents on the nodes.
|
689
|
-
#
|
690
|
-
# Revision 1.4 2007/10/02 03:38:11 anupamsg
|
691
|
-
# Removed dependency on the redundant "Person" class.
|
692
|
-
# (TC_TreeTest::test_comparator): Added a new test for the spaceship operator.
|
693
|
-
# (TC_TreeTest::test_hasContent): Added tests for hasContent? and length methods.
|
694
|
-
#
|
695
|
-
# Revision 1.3 2007/10/02 03:07:30 anupamsg
|
696
|
-
# * Rakefile: Added an optional task for rcov code coverage.
|
697
|
-
#
|
698
|
-
# * test/test_binarytree.rb: Removed the unnecessary dependency on "Person" class.
|
699
|
-
#
|
700
|
-
# * test/test_tree.rb: Removed dependency on the redundant "Person" class.
|
701
|
-
#
|
702
|
-
# Revision 1.2 2007/08/31 01:16:28 anupamsg
|
703
|
-
# Added breadth and pre-order traversals for the tree. Also added a method
|
704
|
-
# to return the detached copy of a node from the tree.
|
705
|
-
#
|
706
|
-
# Revision 1.1 2007/07/21 04:52:38 anupamsg
|
707
|
-
# Renamed the test files.
|
708
|
-
#
|
709
|
-
# Revision 1.13 2007/07/18 22:11:50 anupamsg
|
710
|
-
# Added depth and breadth methods for the TreeNode.
|
711
|
-
#
|
712
|
-
# Revision 1.12 2007/07/18 07:17:34 anupamsg
|
713
|
-
# Fixed a issue where TreeNode.ancestors was shadowing Module.ancestors. This method
|
714
|
-
# has been renamed to TreeNode.parentage.
|
715
|
-
#
|
716
|
-
# Revision 1.11 2007/07/17 03:39:29 anupamsg
|
717
|
-
# Moved the CVS Log keyword to end of the files.
|
718
|
-
#
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubytree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anupam Sengupta
|
@@ -9,9 +9,19 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
12
|
+
date: 2010-01-04 00:00:00 +05:30
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: structured_warnings
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.1.3
|
24
|
+
version:
|
15
25
|
- !ruby/object:Gem::Dependency
|
16
26
|
name: hoe
|
17
27
|
type: :development
|
@@ -60,6 +70,7 @@ post_install_message:
|
|
60
70
|
rdoc_options:
|
61
71
|
- --main
|
62
72
|
- README
|
73
|
+
- --line-numbers
|
63
74
|
require_paths:
|
64
75
|
- lib
|
65
76
|
required_ruby_version: !ruby/object:Gem::Requirement
|