rubytree 0.4.0 → 0.4.1
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.
- data/ChangeLog +14 -2
- data/README +22 -22
- data/Rakefile +22 -8
- data/lib/tree.rb +20 -13
- data/lib/tree/binarytree.rb +124 -110
- data/test/{testbinarytree.rb → test_binarytree.rb} +50 -3
- data/test/{testtree.rb → test_tree.rb} +5 -2
- metadata +9 -8
- data/TAGS +0 -123
data/ChangeLog
CHANGED
@@ -1,5 +1,17 @@
|
|
1
|
+
2007-08-30 Anupam Sengupta <anupamsg@gmail.com>
|
2
|
+
|
3
|
+
* test/test_binarytree.rb (TC_BinaryTreeTest::test_swap_children):
|
4
|
+
Added a test case for the children swap function.
|
5
|
+
|
6
|
+
* lib/tree/binarytree.rb (Tree::BinaryTreeNode::swap_children):
|
7
|
+
Added new function to swap the children. Other minor changes in
|
8
|
+
comments and code.
|
9
|
+
|
1
10
|
2007-07-18 Anupam Sengupta <anupamsg@gmail.com>
|
2
11
|
|
12
|
+
* lib/tree/binarytree.rb (Tree::BinaryTreeNode::leftChild /
|
13
|
+
rightChild): Minor cosmetic change on the parameter name.
|
14
|
+
|
3
15
|
* test/testbinarytree.rb (TC_BinaryTreeTest::test_isLeftChild):
|
4
16
|
Minor syntax correction.
|
5
17
|
|
@@ -21,7 +33,7 @@
|
|
21
33
|
|
22
34
|
* lib/tree/binarytree.rb: Added the binary tree implementation.
|
23
35
|
|
24
|
-
|
36
|
+
2007-07-17 Anupam Sengupta <anupamsg@gmail.com>
|
25
37
|
|
26
38
|
* lib/tree.rb (Tree::TreeNode::parentage): Renamed 'ancestors'
|
27
39
|
method to 'parentage' to avoid clobbering Module.ancestors
|
@@ -42,7 +54,7 @@
|
|
42
54
|
2007-06-24 Anupam Sengupta <anupamsg@gmail.com>
|
43
55
|
|
44
56
|
* test/testtree.rb, lib/tree.rb: Added the each_leaf traversal method.
|
45
|
-
|
57
|
+
|
46
58
|
* README: Replaced all occurrances of LICENSE with COPYING
|
47
59
|
and lowercased all instances of the word 'RubyTree'.
|
48
60
|
|
data/README
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
= Rubytree 0.4.
|
1
|
+
= Rubytree 0.4.1
|
2
2
|
|
3
|
-
|
3
|
+
(c) 2006, 2007 Anupam Sengupta
|
4
4
|
http://rubytree.rubyforge.org
|
5
5
|
|
6
|
-
Document Revision: $Revision: 1.
|
6
|
+
Document Revision: $Revision: 1.10 $ by $Author: anupamsg $
|
7
7
|
|
8
8
|
== License
|
9
9
|
|
@@ -21,9 +21,9 @@ primary objects and drive the structure.
|
|
21
21
|
Rubytree is an open source project and is hosted at
|
22
22
|
http://rubyforge.org/projects/rubytree
|
23
23
|
|
24
|
-
The project home page is
|
24
|
+
The project home page is http://rubytree.rubyforge.org
|
25
25
|
|
26
|
-
Rubytree can be downloaded as a
|
26
|
+
Rubytree can be downloaded as a Rubygem or as a tar/zip file from:
|
27
27
|
|
28
28
|
http://rubyforge.org/frs/?group_id=1215&release_id=8817
|
29
29
|
|
@@ -66,25 +66,25 @@ the text mode ri documentation:
|
|
66
66
|
|
67
67
|
Documentation on the web is available at:
|
68
68
|
|
69
|
-
http://rubytree.rubyforge.org/rdoc
|
69
|
+
http://rubytree.rubyforge.org/rdoc
|
70
70
|
|
71
71
|
== Example
|
72
72
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
73
|
+
The following code-snippet implements this tree structure:
|
74
|
+
|
75
|
+
+------------+
|
76
|
+
| ROOT |
|
77
|
+
+-----+------+
|
78
|
+
+-------------+------------+
|
79
|
+
| |
|
80
|
+
+-------+-------+ +-------+-------+
|
81
|
+
| CHILD 1 | | CHILD 2 |
|
82
|
+
+-------+-------+ +---------------+
|
83
|
+
|
|
84
|
+
|
|
85
|
+
+-------+-------+
|
86
|
+
| GRANDCHILD 1 |
|
87
|
+
+---------------+
|
88
88
|
|
89
89
|
require 'tree'
|
90
90
|
|
@@ -132,7 +132,7 @@ are permitted provided that the following conditions are met:
|
|
132
132
|
be used to endorse or promote products derived from this software without
|
133
133
|
specific prior written permission.
|
134
134
|
|
135
|
-
|
135
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
136
136
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
137
137
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
138
138
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
data/Rakefile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Rakefile
|
2
2
|
#
|
3
|
-
# $Revision: 1.
|
3
|
+
# $Revision: 1.21 $ by $Author: anupamsg $
|
4
4
|
# $Name: $
|
5
5
|
#
|
6
6
|
# Copyright (c) 2006, 2007 Anupam Sengupta
|
@@ -39,10 +39,13 @@ require 'rake/gempackagetask'
|
|
39
39
|
require 'rake/testtask'
|
40
40
|
require 'rake/rdoctask'
|
41
41
|
|
42
|
+
require './lib/tree.rb' # To read the version information.
|
43
|
+
|
42
44
|
desc "Default Task"
|
43
45
|
task :default => :gem
|
44
46
|
|
45
|
-
PKG_VERSION =
|
47
|
+
PKG_VERSION = Tree::VERSION
|
48
|
+
|
46
49
|
PKG_FILES = FileList[
|
47
50
|
'[A-Z]*',
|
48
51
|
'lib/**/*.rb',
|
@@ -85,15 +88,16 @@ Rake::TestTask.new do |t|
|
|
85
88
|
t.verbose = true
|
86
89
|
end
|
87
90
|
|
88
|
-
Rake::RDocTask.new do |rd|
|
91
|
+
Rake::RDocTask.new(:rdoc) do |rd|
|
89
92
|
rd.rdoc_files.include("README", "COPYING", "ChangeLog", "lib/**/*.rb")
|
90
93
|
rd.title = "Rubytree Documentation"
|
91
|
-
end
|
92
94
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
rd.template =
|
95
|
+
# Use the template only if it is present, otherwise, the standard template is
|
96
|
+
# used.
|
97
|
+
template = "../allison/allison.rb"
|
98
|
+
rd.template = template if File.file?(template)
|
99
|
+
|
100
|
+
rd.options << '--line-numbers' << '--inline-source'
|
97
101
|
end
|
98
102
|
|
99
103
|
# Optional TAGS Task.
|
@@ -107,6 +111,16 @@ begin
|
|
107
111
|
end
|
108
112
|
|
109
113
|
# $Log: Rakefile,v $
|
114
|
+
# Revision 1.21 2007/07/21 05:14:43 anupamsg
|
115
|
+
# Added a VERSION constant to the Tree module,
|
116
|
+
# and using the same in the Rakefile.
|
117
|
+
#
|
118
|
+
# Revision 1.20 2007/07/21 03:24:25 anupamsg
|
119
|
+
# Minor edits to parameter names. User visible functionality does not change.
|
120
|
+
#
|
121
|
+
# Revision 1.19 2007/07/19 02:16:01 anupamsg
|
122
|
+
# Release 0.4.0 (and minor fix in Rakefile).
|
123
|
+
#
|
110
124
|
# Revision 1.18 2007/07/18 20:15:06 anupamsg
|
111
125
|
# Added two predicate methods in BinaryTreeNode to determine whether a node
|
112
126
|
# is a left or a right node.
|
data/lib/tree.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# tree.rb
|
2
2
|
#
|
3
|
-
# $Revision: 1.
|
3
|
+
# $Revision: 1.18 $ by $Author: anupamsg $
|
4
4
|
# $Name: $
|
5
5
|
#
|
6
6
|
# = tree.rb - Generic Multi-way Tree implementation
|
@@ -47,6 +47,8 @@
|
|
47
47
|
# This module mixes in the Enumerable module.
|
48
48
|
module Tree
|
49
49
|
|
50
|
+
VERSION = '0.4.1'
|
51
|
+
|
50
52
|
# == TreeNode Class Description
|
51
53
|
#
|
52
54
|
# The node class for the tree representation. the nodes are named and have a
|
@@ -160,7 +162,7 @@ module Tree
|
|
160
162
|
@parent = parent
|
161
163
|
end
|
162
164
|
|
163
|
-
# Convenience synonym for
|
165
|
+
# Convenience synonym for TreeNode#add method. This method allows a convenient
|
164
166
|
# method to add children hierarchies in the tree.
|
165
167
|
#
|
166
168
|
# E.g. root << child << grand_child
|
@@ -273,16 +275,16 @@ module Tree
|
|
273
275
|
|
274
276
|
# Returns the requested node from the set of immediate children.
|
275
277
|
#
|
276
|
-
# If the
|
277
|
-
# accessed (see Tree#children). If the
|
278
|
-
# assumed to be the *name* of the child node to be returned.
|
279
|
-
def [](
|
280
|
-
raise "
|
281
|
-
|
282
|
-
if
|
283
|
-
@children[
|
278
|
+
# If the parameter is _numeric_, then the in-sequence array of children is
|
279
|
+
# accessed (see Tree#children). If the parameter is not _numeric_, then it
|
280
|
+
# is assumed to be the *name* of the child node to be returned.
|
281
|
+
def [](name_or_index)
|
282
|
+
raise "Name_or_index needs to be provided" if name_or_index == nil
|
283
|
+
|
284
|
+
if name_or_index.kind_of?(Integer)
|
285
|
+
@children[name_or_index]
|
284
286
|
else
|
285
|
-
@childrenHash[
|
287
|
+
@childrenHash[name_or_index]
|
286
288
|
end
|
287
289
|
end
|
288
290
|
|
@@ -308,8 +310,6 @@ module Tree
|
|
308
310
|
print(isLastSibling? ? "+" : "|")
|
309
311
|
print "---"
|
310
312
|
print(hasChildren? ? "+" : ">")
|
311
|
-
|
312
|
-
# print((' ' * level) + "|--" + (hasChildren? ? "+" : ">"))
|
313
313
|
end
|
314
314
|
|
315
315
|
puts " #{name}"
|
@@ -461,6 +461,13 @@ module Tree
|
|
461
461
|
end
|
462
462
|
|
463
463
|
# $Log: tree.rb,v $
|
464
|
+
# Revision 1.18 2007/07/21 05:14:44 anupamsg
|
465
|
+
# Added a VERSION constant to the Tree module,
|
466
|
+
# and using the same in the Rakefile.
|
467
|
+
#
|
468
|
+
# Revision 1.17 2007/07/21 03:24:25 anupamsg
|
469
|
+
# Minor edits to parameter names. User visible functionality does not change.
|
470
|
+
#
|
464
471
|
# Revision 1.16 2007/07/18 23:38:55 anupamsg
|
465
472
|
# Minor updates to tree.rb
|
466
473
|
#
|
data/lib/tree/binarytree.rb
CHANGED
@@ -1,114 +1,128 @@
|
|
1
|
-
# binarytree.rb
|
2
|
-
#
|
3
|
-
# $Revision: 1.
|
4
|
-
# $Name: $
|
5
|
-
#
|
6
|
-
# = binarytree.rb - Binary Tree implementation
|
7
|
-
#
|
8
|
-
# Provides a generic tree data structure with ability to
|
9
|
-
# store keyed node elements in the tree. The implementation
|
10
|
-
# mixes in the Enumerable module.
|
11
|
-
#
|
12
|
-
# Author:: Anupam Sengupta (anupamsg@gmail.com)
|
13
|
-
#
|
14
|
-
|
15
|
-
# Copyright (c) 2007 Anupam Sengupta
|
16
|
-
#
|
17
|
-
# All rights reserved.
|
18
|
-
#
|
19
|
-
# Redistribution and use in source and binary forms, with or without modification,
|
20
|
-
# are permitted provided that the following conditions are met:
|
21
|
-
#
|
22
|
-
# - Redistributions of source code must retain the above copyright notice, this
|
23
|
-
# list of conditions and the following disclaimer.
|
24
|
-
#
|
25
|
-
# - Redistributions in binary form must reproduce the above copyright notice, this
|
26
|
-
# list of conditions and the following disclaimer in the documentation and/or
|
27
|
-
# other materials provided with the distribution.
|
28
|
-
#
|
29
|
-
# - Neither the name of the organization nor the names of its contributors may
|
30
|
-
# be used to endorse or promote products derived from this software without
|
31
|
-
# specific prior written permission.
|
32
|
-
#
|
33
|
-
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
34
|
-
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
35
|
-
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
36
|
-
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
37
|
-
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
38
|
-
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
39
|
-
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
40
|
-
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
41
|
-
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
42
|
-
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
43
|
-
#
|
44
|
-
|
45
|
-
require 'tree'
|
46
|
-
|
47
|
-
module Tree
|
48
|
-
|
49
|
-
# Provides a Binary tree implementation. This tree node allows only two child
|
50
|
-
# nodes (left and right childs). It also provides direct access to the left
|
51
|
-
# and right children, including assignment to the same.
|
52
|
-
class BinaryTreeNode < TreeNode
|
53
|
-
|
54
|
-
# Adds the specified child node to the receiver node. The child node's
|
55
|
-
# parent is set to be the receiver. The child nodes are added in the order
|
56
|
-
# of addition, i.e., the first child added becomes the left node, and the
|
57
|
-
# second child will be the second node.
|
58
|
-
# If only one child is present, then this will be the left child.
|
59
|
-
def add(child)
|
60
|
-
raise "Already has two child nodes" if @children.size == 2
|
61
|
-
|
62
|
-
super(child)
|
63
|
-
end
|
64
|
-
|
65
|
-
# Returns the left child node. Note that
|
66
|
-
# left Child == first Child
|
67
|
-
def leftChild
|
68
|
-
children.first
|
69
|
-
end
|
70
|
-
|
71
|
-
# Returns the right child node. Note that
|
72
|
-
# right child == last child unless there is only one child.
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
@
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
@
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
# if this is the
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
# if this is the
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
1
|
+
# binarytree.rb
|
2
|
+
#
|
3
|
+
# $Revision: 1.4 $ by $Author: anupamsg $
|
4
|
+
# $Name: $
|
5
|
+
#
|
6
|
+
# = binarytree.rb - Binary Tree implementation
|
7
|
+
#
|
8
|
+
# Provides a generic tree data structure with ability to
|
9
|
+
# store keyed node elements in the tree. The implementation
|
10
|
+
# mixes in the Enumerable module.
|
11
|
+
#
|
12
|
+
# Author:: Anupam Sengupta (anupamsg@gmail.com)
|
13
|
+
#
|
14
|
+
|
15
|
+
# Copyright (c) 2007 Anupam Sengupta
|
16
|
+
#
|
17
|
+
# All rights reserved.
|
18
|
+
#
|
19
|
+
# Redistribution and use in source and binary forms, with or without modification,
|
20
|
+
# are permitted provided that the following conditions are met:
|
21
|
+
#
|
22
|
+
# - Redistributions of source code must retain the above copyright notice, this
|
23
|
+
# list of conditions and the following disclaimer.
|
24
|
+
#
|
25
|
+
# - Redistributions in binary form must reproduce the above copyright notice, this
|
26
|
+
# list of conditions and the following disclaimer in the documentation and/or
|
27
|
+
# other materials provided with the distribution.
|
28
|
+
#
|
29
|
+
# - Neither the name of the organization nor the names of its contributors may
|
30
|
+
# be used to endorse or promote products derived from this software without
|
31
|
+
# specific prior written permission.
|
32
|
+
#
|
33
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
34
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
35
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
36
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
37
|
+
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
38
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
39
|
+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
40
|
+
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
41
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
42
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
43
|
+
#
|
44
|
+
|
45
|
+
require 'tree'
|
46
|
+
|
47
|
+
module Tree
|
48
|
+
|
49
|
+
# Provides a Binary tree implementation. This tree node allows only two child
|
50
|
+
# nodes (left and right childs). It also provides direct access to the left
|
51
|
+
# and right children, including assignment to the same.
|
52
|
+
class BinaryTreeNode < TreeNode
|
53
|
+
|
54
|
+
# Adds the specified child node to the receiver node. The child node's
|
55
|
+
# parent is set to be the receiver. The child nodes are added in the order
|
56
|
+
# of addition, i.e., the first child added becomes the left node, and the
|
57
|
+
# second child will be the second node.
|
58
|
+
# If only one child is present, then this will be the left child.
|
59
|
+
def add(child)
|
60
|
+
raise "Already has two child nodes" if @children.size == 2
|
61
|
+
|
62
|
+
super(child)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Returns the left child node. Note that
|
66
|
+
# left Child == first Child
|
67
|
+
def leftChild
|
68
|
+
children.first
|
69
|
+
end
|
70
|
+
|
71
|
+
# Returns the right child node. Note that
|
72
|
+
# right child == last child unless there is only one child.
|
73
|
+
# Returns nil if the right child does not exist.
|
74
|
+
def rightChild
|
75
|
+
children[1]
|
76
|
+
end
|
77
|
+
|
78
|
+
# Sets the left child. If a previous child existed, it is replaced.
|
79
|
+
def leftChild=(child)
|
80
|
+
@children[0] = child
|
81
|
+
@childrenHash[child.name] = child if child # Assign the name mapping
|
82
|
+
end
|
83
|
+
|
84
|
+
# Sets the right child. If a previous child existed, it is replaced.
|
85
|
+
def rightChild=(child)
|
86
|
+
@children[1] = child
|
87
|
+
@childrenHash[child.name] = child if child # Assign the name mapping
|
88
|
+
end
|
89
|
+
|
90
|
+
# Returns true if this is the left child of its parent. Always returns false
|
91
|
+
# if this is the root node.
|
92
|
+
def isLeftChild?
|
93
|
+
return nil if isRoot?
|
94
|
+
self == parent.leftChild
|
95
|
+
end
|
96
|
+
|
97
|
+
# Returns true if this is the right child of its parent. Always returns false
|
98
|
+
# if this is the root node.
|
99
|
+
def isRightChild?
|
100
|
+
return nil if isRoot?
|
101
|
+
self == parent.rightChild
|
102
|
+
end
|
103
|
+
|
104
|
+
# Swaps the left and right childs
|
105
|
+
def swap_children
|
106
|
+
tempChild = leftChild
|
107
|
+
self.leftChild= rightChild
|
108
|
+
self.rightChild= tempChild
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
107
114
|
# $Log: binarytree.rb,v $
|
115
|
+
# Revision 1.4 2007/08/30 22:08:58 anupamsg
|
116
|
+
# Added a new swap_children method for Binary Tree. Also added minor
|
117
|
+
# documentation and test updates.
|
118
|
+
#
|
119
|
+
# Revision 1.3 2007/07/21 03:24:25 anupamsg
|
120
|
+
# Minor edits to parameter names. User visible functionality does not change.
|
121
|
+
#
|
108
122
|
# Revision 1.2 2007/07/18 20:15:06 anupamsg
|
109
123
|
# Added two predicate methods in BinaryTreeNode to determine whether a node
|
110
124
|
# is a left or a right node.
|
111
|
-
#
|
112
|
-
# Revision 1.1 2007/07/18 19:33:27 anupamsg
|
113
|
-
# Added a new binary tree implementation.
|
114
|
-
#
|
125
|
+
#
|
126
|
+
# Revision 1.1 2007/07/18 19:33:27 anupamsg
|
127
|
+
# Added a new binary tree implementation.
|
128
|
+
#
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# testtree.rb
|
4
4
|
#
|
5
|
-
# $Revision: 1.
|
5
|
+
# $Revision: 1.2 $ by $Author: anupamsg $
|
6
6
|
# $Name: $
|
7
7
|
#
|
8
8
|
# Copyright (c) 2006, 2007 Anupam Sengupta
|
@@ -46,7 +46,7 @@ class TC_BinaryTreeTest < Test::Unit::TestCase
|
|
46
46
|
@root = Tree::BinaryTreeNode.new("ROOT", "Root Node")
|
47
47
|
|
48
48
|
@left_child1 = Tree::BinaryTreeNode.new("A Child at Left", "Child Node @ left")
|
49
|
-
@right_child1 = Tree::BinaryTreeNode.new("B Child at
|
49
|
+
@right_child1 = Tree::BinaryTreeNode.new("B Child at Right", "Child Node @ right")
|
50
50
|
|
51
51
|
end
|
52
52
|
|
@@ -99,6 +99,13 @@ class TC_BinaryTreeTest < Test::Unit::TestCase
|
|
99
99
|
|
100
100
|
@root.leftChild = Tree::BinaryTreeNode.new("New Left Child")
|
101
101
|
assert_equal("New Left Child", @root.leftChild.name, "The left child should now be the new child")
|
102
|
+
assert_equal("B Child at Right", @root.lastChild.name, "The last child should now be the right child")
|
103
|
+
|
104
|
+
# Now set the left child as nil, and retest
|
105
|
+
@root.leftChild = nil
|
106
|
+
assert_nil(@root.leftChild, "The left child should now be nil")
|
107
|
+
assert_nil(@root.firstChild, "The first child is now nil")
|
108
|
+
assert_equal("B Child at Right", @root.lastChild.name, "The last child should now be the right child")
|
102
109
|
end
|
103
110
|
|
104
111
|
def test_right_assignment
|
@@ -108,6 +115,14 @@ class TC_BinaryTreeTest < Test::Unit::TestCase
|
|
108
115
|
|
109
116
|
@root.rightChild = Tree::BinaryTreeNode.new("New Right Child")
|
110
117
|
assert_equal("New Right Child", @root.rightChild.name, "The right child should now be the new child")
|
118
|
+
assert_equal("A Child at Left", @root.firstChild.name, "The first child should now be the left child")
|
119
|
+
assert_equal("New Right Child", @root.lastChild.name, "The last child should now be the right child")
|
120
|
+
|
121
|
+
# Now set the right child as nil, and retest
|
122
|
+
@root.rightChild = nil
|
123
|
+
assert_nil(@root.rightChild, "The right child should now be nil")
|
124
|
+
assert_equal("A Child at Left", @root.firstChild.name, "The first child should now be the left child")
|
125
|
+
assert_nil(@root.lastChild, "The first child is now nil")
|
111
126
|
end
|
112
127
|
|
113
128
|
def test_isLeftChild?
|
@@ -117,6 +132,10 @@ class TC_BinaryTreeTest < Test::Unit::TestCase
|
|
117
132
|
assert(@left_child1.isLeftChild?, "left_child1 should be the left child")
|
118
133
|
assert(!@right_child1.isLeftChild?, "left_child1 should be the left child")
|
119
134
|
|
135
|
+
# Now set the right child as nil, and retest
|
136
|
+
@root.rightChild = nil
|
137
|
+
assert(@left_child1.isLeftChild?, "left_child1 should be the left child")
|
138
|
+
|
120
139
|
assert(!@root.isLeftChild?, "Root is neither left child nor right")
|
121
140
|
end
|
122
141
|
|
@@ -126,11 +145,39 @@ class TC_BinaryTreeTest < Test::Unit::TestCase
|
|
126
145
|
|
127
146
|
assert(@right_child1.isRightChild?, "right_child1 should be the right child")
|
128
147
|
assert(!@left_child1.isRightChild?, "right_child1 should be the right child")
|
148
|
+
|
149
|
+
# Now set the left child as nil, and retest
|
150
|
+
@root.leftChild = nil
|
151
|
+
assert(@right_child1.isRightChild?, "right_child1 should be the right child")
|
129
152
|
assert(!@root.isRightChild?, "Root is neither left child nor right")
|
130
153
|
end
|
154
|
+
|
155
|
+
def test_swap_children
|
156
|
+
@root << @left_child1
|
157
|
+
@root << @right_child1
|
158
|
+
|
159
|
+
assert(@right_child1.isRightChild?, "right_child1 should be the right child")
|
160
|
+
assert(!@left_child1.isRightChild?, "right_child1 should be the right child")
|
161
|
+
|
162
|
+
@root.swap_children
|
163
|
+
assert(@right_child1.isLeftChild?, "right_child1 should now be the left child")
|
164
|
+
assert(@left_child1.isRightChild?, "left_child1 should now be the right child")
|
165
|
+
assert_equal(@right_child1, @root.firstChild, "right_child1 should now be the first child")
|
166
|
+
assert_equal(@left_child1, @root.lastChild, "left_child1 should now be the last child")
|
167
|
+
assert_equal(@right_child1, @root[0], "right_child1 should now be the first child")
|
168
|
+
assert_equal(@left_child1, @root[1], "left_child1 should now be the last child")
|
169
|
+
|
170
|
+
end
|
131
171
|
end
|
132
172
|
|
133
|
-
# $Log:
|
173
|
+
# $Log: test_binarytree.rb,v $
|
174
|
+
# Revision 1.2 2007/08/30 22:06:13 anupamsg
|
175
|
+
# Added a new swap_children method for the Binary Tree class.
|
176
|
+
# Also made minor documentation updates and test additions.
|
177
|
+
#
|
178
|
+
# Revision 1.1 2007/07/21 04:52:37 anupamsg
|
179
|
+
# Renamed the test files.
|
180
|
+
#
|
134
181
|
# Revision 1.4 2007/07/19 02:03:57 anupamsg
|
135
182
|
# Minor syntax correction.
|
136
183
|
#
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# testtree.rb
|
4
4
|
#
|
5
|
-
# $Revision: 1.
|
5
|
+
# $Revision: 1.1 $ by $Author: anupamsg $
|
6
6
|
# $Name: $
|
7
7
|
#
|
8
8
|
# Copyright (c) 2006, 2007 Anupam Sengupta
|
@@ -459,7 +459,10 @@ end
|
|
459
459
|
|
460
460
|
__END__
|
461
461
|
|
462
|
-
# $Log:
|
462
|
+
# $Log: test_tree.rb,v $
|
463
|
+
# Revision 1.1 2007/07/21 04:52:38 anupamsg
|
464
|
+
# Renamed the test files.
|
465
|
+
#
|
463
466
|
# Revision 1.13 2007/07/18 22:11:50 anupamsg
|
464
467
|
# Added depth and breadth methods for the TreeNode.
|
465
468
|
#
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.
|
2
|
+
rubygems_version: 0.9.2
|
3
3
|
specification_version: 1
|
4
4
|
name: rubytree
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.4.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.4.1
|
7
|
+
date: 2007-08-30 00:00:00 -07:00
|
8
8
|
summary: Ruby implementation of the Tree data structure.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -31,17 +31,18 @@ authors:
|
|
31
31
|
files:
|
32
32
|
- ChangeLog
|
33
33
|
- COPYING
|
34
|
+
- lib
|
34
35
|
- Rakefile
|
35
36
|
- README
|
36
|
-
-
|
37
|
+
- test
|
37
38
|
- lib/tree/binarytree.rb
|
38
39
|
- lib/tree.rb
|
39
40
|
- test/person.rb
|
40
|
-
- test/
|
41
|
-
- test/
|
41
|
+
- test/test_binarytree.rb
|
42
|
+
- test/test_tree.rb
|
42
43
|
test_files:
|
43
|
-
- test/
|
44
|
-
- test/
|
44
|
+
- test/test_binarytree.rb
|
45
|
+
- test/test_tree.rb
|
45
46
|
rdoc_options: []
|
46
47
|
|
47
48
|
extra_rdoc_files:
|
data/TAGS
DELETED
@@ -1,123 +0,0 @@
|
|
1
|
-
|
2
|
-
lib/tree.rb,2410
|
3
|
-
module Tree::Tree48,2020
|
4
|
-
class TreeNode::Tree::TreeNode103,3802
|
5
|
-
attr_reader :content, :name, :parent::Tree::TreeNode#content106,3845
|
6
|
-
attr_reader :content, :name, :parent::Tree::TreeNode#name106,3845
|
7
|
-
attr_reader :content, :name, :parent::Tree::TreeNode#parent106,3845
|
8
|
-
attr_writer :content::Tree::TreeNode#content107,3886
|
9
|
-
def initialize::Tree::TreeNode#Tree::TreeNode.new118,4158
|
10
|
-
def to_s::Tree::TreeNode#to_s132,4447
|
11
|
-
def parentage::Tree::TreeNode#parentage144,4836
|
12
|
-
def parent=::Tree::TreeNode#parent=159,5172
|
13
|
-
def <<::Tree::TreeNode#<<167,5408
|
14
|
-
def add::Tree::TreeNode#add174,5667
|
15
|
-
def remove!::Tree::TreeNode#remove!189,6080
|
16
|
-
def removeFromParent!::Tree::TreeNode#removeFromParent!198,6356
|
17
|
-
def removeAll!::Tree::TreeNode#removeAll!203,6478
|
18
|
-
def hasContent?::Tree::TreeNode#hasContent?213,6692
|
19
|
-
def setAsRoot!::Tree::TreeNode#setAsRoot!218,6800
|
20
|
-
def isRoot?::Tree::TreeNode#isRoot?224,6966
|
21
|
-
def hasChildren?::Tree::TreeNode#hasChildren?229,7082
|
22
|
-
def isLeaf?::Tree::TreeNode#isLeaf?235,7220
|
23
|
-
def children::Tree::TreeNode#children241,7385
|
24
|
-
def firstChild::Tree::TreeNode#firstChild251,7613
|
25
|
-
def lastChild::Tree::TreeNode#lastChild257,7754
|
26
|
-
def each::Tree::TreeNode#each263,7948
|
27
|
-
def each_leaf::Tree::TreeNode#each_leaf270,8204
|
28
|
-
def []::Tree::TreeNode#[]279,8570
|
29
|
-
def size::Tree::TreeNode#size291,8848
|
30
|
-
def length::Tree::TreeNode#length296,8968
|
31
|
-
def printTree::Tree::TreeNode#printTree301,9057
|
32
|
-
def root::Tree::TreeNode#root321,9553
|
33
|
-
def firstSibling::Tree::TreeNode#firstSibling329,9752
|
34
|
-
def isFirstSibling?::Tree::TreeNode#isFirstSibling?338,9920
|
35
|
-
def lastSibling::Tree::TreeNode#lastSibling344,10071
|
36
|
-
def isLastSibling?::Tree::TreeNode#isLastSibling?353,10234
|
37
|
-
def siblings::Tree::TreeNode#siblings360,10453
|
38
|
-
def isOnlyChild?::Tree::TreeNode#isOnlyChild?374,10826
|
39
|
-
def nextSibling::Tree::TreeNode#nextSibling380,10989
|
40
|
-
def previousSibling::Tree::TreeNode#previousSibling388,11221
|
41
|
-
def <=>::Tree::TreeNode#<=>397,11486
|
42
|
-
def freezeTree!::Tree::TreeNode#freezeTree!403,11628
|
43
|
-
def createDumpRep::Tree::TreeNode#createDumpRep408,11760
|
44
|
-
def _dump::Tree::TreeNode#_dump414,11940
|
45
|
-
def TreeNode.loadDumpRep::Tree::TreeNode.loadDumpRep421,12130
|
46
|
-
def TreeNode._load::Tree::TreeNode._load439,12707
|
47
|
-
|
48
|
-
test/person.rb,322
|
49
|
-
class Person::Person37,1683
|
50
|
-
attr_reader :first, :last::Person#first38,1700
|
51
|
-
attr_reader :first, :last::Person#last38,1700
|
52
|
-
attr_writer :first, :last::Person#first39,1730
|
53
|
-
attr_writer :first, :last::Person#last39,1730
|
54
|
-
def initialize::Person#Person.new40,1760
|
55
|
-
def to_s::Person#to_s45,1853
|
56
|
-
|
57
|
-
test/testtree.rb,1847
|
58
|
-
class TC_TreeTest::TC_TreeTest43,1728
|
59
|
-
def setup::TC_TreeTest#setup45,1781
|
60
|
-
def loadChildren::TC_TreeTest#loadChildren73,2687
|
61
|
-
def teardown::TC_TreeTest#teardown79,2783
|
62
|
-
def test_root_setup::TC_TreeTest#test_root_setup83,2828
|
63
|
-
def test_root::TC_TreeTest#test_root97,3464
|
64
|
-
def test_firstSibling::TC_TreeTest#test_firstSibling105,3691
|
65
|
-
def test_isFirstSibling::TC_TreeTest#test_isFirstSibling115,4169
|
66
|
-
def test_isLastSibling::TC_TreeTest#test_isLastSibling125,4577
|
67
|
-
def test_lastSibling::TC_TreeTest#test_lastSibling135,4973
|
68
|
-
def test_siblings::TC_TreeTest#test_siblings145,5442
|
69
|
-
def test_isOnlyChild?::TC_TreeTest#test_isOnlyChild?164,6029
|
70
|
-
def test_nextSibling::TC_TreeTest#test_nextSibling173,6341
|
71
|
-
def test_previousSibling::TC_TreeTest#test_previousSibling182,6707
|
72
|
-
def test_add::TC_TreeTest#test_add191,7091
|
73
|
-
def test_remove::TC_TreeTest#test_remove210,7573
|
74
|
-
def test_removeAll::TC_TreeTest#test_removeAll237,8283
|
75
|
-
def test_removeFromParent::TC_TreeTest#test_removeFromParent246,8527
|
76
|
-
def test_children::TC_TreeTest#test_children263,9157
|
77
|
-
def test_firstChild::TC_TreeTest#test_firstChild289,10018
|
78
|
-
def test_lastChild::TC_TreeTest#test_lastChild298,10293
|
79
|
-
def test_find::TC_TreeTest#test_find307,10558
|
80
|
-
def test_parentage::TC_TreeTest#test_parentage321,11093
|
81
|
-
def test_each::TC_TreeTest#test_each329,11377
|
82
|
-
def test_each_leaf::TC_TreeTest#test_each_leaf346,11999
|
83
|
-
def test_parent::TC_TreeTest#test_parent360,12469
|
84
|
-
def test_indexed_access::TC_TreeTest#test_indexed_access369,12850
|
85
|
-
def test_printTree::TC_TreeTest#test_printTree377,13120
|
86
|
-
def test_dump::TC_TreeTest#test_dump383,13191
|
87
|
-
def test_collect::TC_TreeTest#test_collect399,13728
|
88
|
-
def test_freezeTree::TC_TreeTest#test_freezeTree408,13947
|
89
|
-
def test_content::TC_TreeTest#test_content417,14211
|
90
|
-
|
91
|
-
test/testtree.rb.~1.11.~,1847
|
92
|
-
class TC_TreeTest::TC_TreeTest43,1728
|
93
|
-
def setup::TC_TreeTest#setup45,1781
|
94
|
-
def loadChildren::TC_TreeTest#loadChildren73,2687
|
95
|
-
def teardown::TC_TreeTest#teardown79,2783
|
96
|
-
def test_root_setup::TC_TreeTest#test_root_setup83,2828
|
97
|
-
def test_root::TC_TreeTest#test_root97,3464
|
98
|
-
def test_firstSibling::TC_TreeTest#test_firstSibling105,3691
|
99
|
-
def test_isFirstSibling::TC_TreeTest#test_isFirstSibling115,4169
|
100
|
-
def test_isLastSibling::TC_TreeTest#test_isLastSibling125,4577
|
101
|
-
def test_lastSibling::TC_TreeTest#test_lastSibling135,4973
|
102
|
-
def test_siblings::TC_TreeTest#test_siblings145,5442
|
103
|
-
def test_isOnlyChild?::TC_TreeTest#test_isOnlyChild?164,6029
|
104
|
-
def test_nextSibling::TC_TreeTest#test_nextSibling173,6341
|
105
|
-
def test_previousSibling::TC_TreeTest#test_previousSibling182,6707
|
106
|
-
def test_add::TC_TreeTest#test_add191,7091
|
107
|
-
def test_remove::TC_TreeTest#test_remove210,7573
|
108
|
-
def test_removeAll::TC_TreeTest#test_removeAll237,8283
|
109
|
-
def test_removeFromParent::TC_TreeTest#test_removeFromParent246,8527
|
110
|
-
def test_children::TC_TreeTest#test_children263,9157
|
111
|
-
def test_firstChild::TC_TreeTest#test_firstChild289,10018
|
112
|
-
def test_lastChild::TC_TreeTest#test_lastChild298,10293
|
113
|
-
def test_find::TC_TreeTest#test_find307,10558
|
114
|
-
def test_ancestors::TC_TreeTest#test_ancestors321,11093
|
115
|
-
def test_each::TC_TreeTest#test_each329,11377
|
116
|
-
def test_each_leaf::TC_TreeTest#test_each_leaf346,11999
|
117
|
-
def test_parent::TC_TreeTest#test_parent360,12469
|
118
|
-
def test_indexed_access::TC_TreeTest#test_indexed_access369,12850
|
119
|
-
def test_printTree::TC_TreeTest#test_printTree377,13120
|
120
|
-
def test_dump::TC_TreeTest#test_dump383,13191
|
121
|
-
def test_collect::TC_TreeTest#test_collect399,13728
|
122
|
-
def test_freezeTree::TC_TreeTest#test_freezeTree408,13947
|
123
|
-
def test_content::TC_TreeTest#test_content417,14211
|