rubytree 0.4.1 → 0.4.2
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 +19 -0
- data/README +2 -2
- data/Rakefile +14 -2
- data/TAGS +120 -0
- data/lib/tree.rb +36 -5
- data/test/test_binarytree.rb +8 -2
- data/test/test_tree.rb +167 -18
- metadata +14 -8
- data/test/person.rb +0 -54
data/ChangeLog
CHANGED
@@ -1,5 +1,24 @@
|
|
1
|
+
2007-10-01 Anupam Sengupta <anupamsg@gmail.com>
|
2
|
+
|
3
|
+
* Rakefile: Added an optional task for rcov code coverage.
|
4
|
+
Added a dependency for rake in the Gem Specification.
|
5
|
+
|
6
|
+
* test/test_binarytree.rb: Removed the unnecessary dependency on "Person" class.
|
7
|
+
|
8
|
+
* test/test_tree.rb: Removed dependency on the redundant "Person" class.
|
9
|
+
(TC_TreeTest::test_comparator): Added a new test for the spaceship operator.
|
10
|
+
(TC_TreeTest::test_hasContent): Added tests for hasContent? and length methods.
|
11
|
+
|
1
12
|
2007-08-30 Anupam Sengupta <anupamsg@gmail.com>
|
2
13
|
|
14
|
+
* test/test_tree.rb (TC_TreeTest::test_preordered_each, TC_TreeTest::test_breadth_each, TC_TreeTest::test_detached_copy):
|
15
|
+
Added new tests for the new functions added to tree.rb.
|
16
|
+
|
17
|
+
* lib/tree.rb (Tree::TreeNode::detached_copy, Tree::TreeNode::preordered_each, Tree::TreeNode::breadth_each):
|
18
|
+
Added new functions for returning a detached copy of the node and
|
19
|
+
for performing breadth first traversal. Also added the pre-ordered
|
20
|
+
traversal function which is an alias of the existing 'each' method.
|
21
|
+
|
3
22
|
* test/test_binarytree.rb (TC_BinaryTreeTest::test_swap_children):
|
4
23
|
Added a test case for the children swap function.
|
5
24
|
|
data/README
CHANGED
data/Rakefile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Rakefile
|
2
2
|
#
|
3
|
-
# $Revision: 1.
|
3
|
+
# $Revision: 1.23 $ by $Author: anupamsg $
|
4
4
|
# $Name: $
|
5
5
|
#
|
6
6
|
# Copyright (c) 2006, 2007 Anupam Sengupta
|
@@ -61,6 +61,7 @@ spec = Gem::Specification.new do |s|
|
|
61
61
|
s.homepage = "http://rubytree.rubyforge.org/"
|
62
62
|
s.rubyforge_project = 'rubytree'
|
63
63
|
s.summary = "Ruby implementation of the Tree data structure."
|
64
|
+
s.add_dependency('rake', '>= 0.7.2')
|
64
65
|
|
65
66
|
s.description = <<-END
|
66
67
|
Provides a generic tree data-structure with ability to
|
@@ -110,7 +111,18 @@ begin
|
|
110
111
|
rescue LoadError
|
111
112
|
end
|
112
113
|
|
113
|
-
#
|
114
|
+
# Optional RCOV Task
|
115
|
+
# Needs http://eigenclass.org/hiki/rcov
|
116
|
+
begin
|
117
|
+
require 'rcov/rcovtask'
|
118
|
+
Rcov::RcovTask.new do |t|
|
119
|
+
t.test_files = FileList['test/test*.rb']
|
120
|
+
# t.verbose = true # uncomment to see the executed commands
|
121
|
+
end
|
122
|
+
rescue LoadError
|
123
|
+
end
|
124
|
+
|
125
|
+
#Rakefile,v $
|
114
126
|
# Revision 1.21 2007/07/21 05:14:43 anupamsg
|
115
127
|
# Added a VERSION constant to the Tree module,
|
116
128
|
# and using the same in the Rakefile.
|
data/TAGS
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
|
2
|
+
lib/tree/binarytree.rb,572
|
3
|
+
module Tree::Tree47,1886
|
4
|
+
class BinaryTreeNode::Tree::BinaryTreeNode52,2117
|
5
|
+
def add::Tree::BinaryTreeNode#add59,2502
|
6
|
+
def leftChild::Tree::BinaryTreeNode#leftChild67,2705
|
7
|
+
def rightChild::Tree::BinaryTreeNode#rightChild74,2917
|
8
|
+
def leftChild=::Tree::BinaryTreeNode#leftChild=79,3021
|
9
|
+
def rightChild=::Tree::BinaryTreeNode#rightChild=85,3231
|
10
|
+
def isLeftChild?::Tree::BinaryTreeNode#isLeftChild?92,3498
|
11
|
+
def isRightChild?::Tree::BinaryTreeNode#isRightChild?99,3702
|
12
|
+
def swap_children::Tree::BinaryTreeNode#swap_children105,3831
|
13
|
+
|
14
|
+
lib/tree.rb,2700
|
15
|
+
module Tree::Tree48,2030
|
16
|
+
class TreeNode::Tree::TreeNode105,3833
|
17
|
+
attr_reader :content, :name, :parent::Tree::TreeNode#content108,3876
|
18
|
+
attr_reader :content, :name, :parent::Tree::TreeNode#name108,3876
|
19
|
+
attr_reader :content, :name, :parent::Tree::TreeNode#parent108,3876
|
20
|
+
attr_writer :content::Tree::TreeNode#content109,3917
|
21
|
+
def initialize::Tree::TreeNode#Tree::TreeNode.new120,4189
|
22
|
+
def detached_copy::Tree::TreeNode#detached_copy134,4514
|
23
|
+
def to_s::Tree::TreeNode#to_s139,4653
|
24
|
+
def parentage::Tree::TreeNode#parentage151,5042
|
25
|
+
def parent=::Tree::TreeNode#parent=166,5378
|
26
|
+
def <<::Tree::TreeNode#<<174,5618
|
27
|
+
def add::Tree::TreeNode#add181,5877
|
28
|
+
def remove!::Tree::TreeNode#remove!196,6290
|
29
|
+
def removeFromParent!::Tree::TreeNode#removeFromParent!205,6566
|
30
|
+
def removeAll!::Tree::TreeNode#removeAll!210,6688
|
31
|
+
def hasContent?::Tree::TreeNode#hasContent?220,6902
|
32
|
+
def setAsRoot!::Tree::TreeNode#setAsRoot!225,7010
|
33
|
+
def isRoot?::Tree::TreeNode#isRoot?231,7176
|
34
|
+
def hasChildren?::Tree::TreeNode#hasChildren?236,7292
|
35
|
+
def isLeaf?::Tree::TreeNode#isLeaf?242,7430
|
36
|
+
def children::Tree::TreeNode#children248,7595
|
37
|
+
def firstChild::Tree::TreeNode#firstChild258,7823
|
38
|
+
def lastChild::Tree::TreeNode#lastChild264,7964
|
39
|
+
def each::Tree::TreeNode#each271,8188
|
40
|
+
def preordered_each::Tree::TreeNode#preordered_each278,8385
|
41
|
+
def breadth_each::Tree::TreeNode#breadth_each284,8572
|
42
|
+
def each_leaf::Tree::TreeNode#each_leaf299,9143
|
43
|
+
def []::Tree::TreeNode#[]308,9521
|
44
|
+
def size::Tree::TreeNode#size320,9859
|
45
|
+
def length::Tree::TreeNode#length325,9979
|
46
|
+
def printTree::Tree::TreeNode#printTree330,10068
|
47
|
+
def root::Tree::TreeNode#root348,10495
|
48
|
+
def firstSibling::Tree::TreeNode#firstSibling356,10694
|
49
|
+
def isFirstSibling?::Tree::TreeNode#isFirstSibling?365,10862
|
50
|
+
def lastSibling::Tree::TreeNode#lastSibling371,11013
|
51
|
+
def isLastSibling?::Tree::TreeNode#isLastSibling?380,11176
|
52
|
+
def siblings::Tree::TreeNode#siblings387,11395
|
53
|
+
def isOnlyChild?::Tree::TreeNode#isOnlyChild?401,11768
|
54
|
+
def nextSibling::Tree::TreeNode#nextSibling407,11931
|
55
|
+
def previousSibling::Tree::TreeNode#previousSibling415,12163
|
56
|
+
def <=>::Tree::TreeNode#<=>424,12428
|
57
|
+
def freezeTree!::Tree::TreeNode#freezeTree!430,12570
|
58
|
+
def createDumpRep::Tree::TreeNode#createDumpRep435,12702
|
59
|
+
def _dump::Tree::TreeNode#_dump441,12882
|
60
|
+
def TreeNode.loadDumpRep::Tree::TreeNode.loadDumpRep448,13072
|
61
|
+
def TreeNode._load::Tree::TreeNode._load466,13649
|
62
|
+
def depth::Tree::TreeNode#depth472,13809
|
63
|
+
def breadth::Tree::TreeNode#breadth479,14010
|
64
|
+
|
65
|
+
test/test_binarytree.rb,750
|
66
|
+
class TC_BinaryTreeTest::TC_BinaryTreeTest42,1721
|
67
|
+
def setup::TC_BinaryTreeTest#setup44,1780
|
68
|
+
def teardown::TC_BinaryTreeTest#teardown52,2034
|
69
|
+
def test_initialize::TC_BinaryTreeTest#test_initialize58,2144
|
70
|
+
def test_add::TC_BinaryTreeTest#test_add62,2239
|
71
|
+
def test_left::TC_BinaryTreeTest#test_left80,2877
|
72
|
+
def test_right::TC_BinaryTreeTest#test_right87,3136
|
73
|
+
def test_left_assignment::TC_BinaryTreeTest#test_left_assignment94,3408
|
74
|
+
def test_right_assignment::TC_BinaryTreeTest#test_right_assignment110,4173
|
75
|
+
def test_isLeftChild?::TC_BinaryTreeTest#test_isLeftChild?127,5052
|
76
|
+
def test_isRightChild?::TC_BinaryTreeTest#test_isRightChild?141,5524
|
77
|
+
def test_swap_children::TC_BinaryTreeTest#test_swap_children154,6004
|
78
|
+
|
79
|
+
test/test_tree.rb,2386
|
80
|
+
class TC_TreeTest::TC_TreeTest42,1710
|
81
|
+
def setup::TC_TreeTest#setup46,1802
|
82
|
+
def loadChildren::TC_TreeTest#loadChildren74,2708
|
83
|
+
def teardown::TC_TreeTest#teardown80,2804
|
84
|
+
def test_root_setup::TC_TreeTest#test_root_setup84,2849
|
85
|
+
def test_root::TC_TreeTest#test_root99,3548
|
86
|
+
def test_hasContent::TC_TreeTest#test_hasContent107,3773
|
87
|
+
def test_length::TC_TreeTest#test_length117,4141
|
88
|
+
def test_comparator::TC_TreeTest#test_comparator123,4288
|
89
|
+
def test_to_s::TC_TreeTest#test_to_s143,4828
|
90
|
+
def test_firstSibling::TC_TreeTest#test_firstSibling151,5112
|
91
|
+
def test_isFirstSibling::TC_TreeTest#test_isFirstSibling161,5590
|
92
|
+
def test_isLastSibling::TC_TreeTest#test_isLastSibling171,5998
|
93
|
+
def test_lastSibling::TC_TreeTest#test_lastSibling181,6394
|
94
|
+
def test_siblings::TC_TreeTest#test_siblings191,6863
|
95
|
+
def test_isOnlyChild?::TC_TreeTest#test_isOnlyChild?210,7450
|
96
|
+
def test_nextSibling::TC_TreeTest#test_nextSibling219,7762
|
97
|
+
def test_previousSibling::TC_TreeTest#test_previousSibling228,8128
|
98
|
+
def test_add::TC_TreeTest#test_add237,8512
|
99
|
+
def test_remove::TC_TreeTest#test_remove256,8994
|
100
|
+
def test_removeAll::TC_TreeTest#test_removeAll283,9704
|
101
|
+
def test_removeFromParent::TC_TreeTest#test_removeFromParent292,9948
|
102
|
+
def test_children::TC_TreeTest#test_children309,10578
|
103
|
+
def test_firstChild::TC_TreeTest#test_firstChild335,11439
|
104
|
+
def test_lastChild::TC_TreeTest#test_lastChild344,11714
|
105
|
+
def test_find::TC_TreeTest#test_find353,11979
|
106
|
+
def test_parentage::TC_TreeTest#test_parentage367,12514
|
107
|
+
def test_each::TC_TreeTest#test_each375,12798
|
108
|
+
def test_each_leaf::TC_TreeTest#test_each_leaf392,13420
|
109
|
+
def test_parent::TC_TreeTest#test_parent406,13890
|
110
|
+
def test_indexed_access::TC_TreeTest#test_indexed_access415,14271
|
111
|
+
def test_printTree::TC_TreeTest#test_printTree423,14541
|
112
|
+
def test_dump::TC_TreeTest#test_dump429,14612
|
113
|
+
def test_collect::TC_TreeTest#test_collect442,15085
|
114
|
+
def test_freezeTree::TC_TreeTest#test_freezeTree451,15304
|
115
|
+
def test_content::TC_TreeTest#test_content460,15568
|
116
|
+
def test_depth::TC_TreeTest#test_depth466,15722
|
117
|
+
def test_breadth::TC_TreeTest#test_breadth484,16216
|
118
|
+
def test_breadth_each::TC_TreeTest#test_breadth_each502,16810
|
119
|
+
def test_preordered_each::TC_TreeTest#test_preordered_each542,17749
|
120
|
+
def test_detached_copy::TC_TreeTest#test_detached_copy575,18593
|
data/lib/tree.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# tree.rb
|
2
2
|
#
|
3
|
-
# $Revision: 1.
|
3
|
+
# $Revision: 1.19 $ by $Author: anupamsg $
|
4
4
|
# $Name: $
|
5
5
|
#
|
6
6
|
# = tree.rb - Generic Multi-way Tree implementation
|
@@ -47,7 +47,7 @@
|
|
47
47
|
# This module mixes in the Enumerable module.
|
48
48
|
module Tree
|
49
49
|
|
50
|
-
VERSION = '0.4.
|
50
|
+
VERSION = '0.4.2'
|
51
51
|
|
52
52
|
# == TreeNode Class Description
|
53
53
|
#
|
@@ -130,6 +130,11 @@ module Tree
|
|
130
130
|
@children = []
|
131
131
|
end
|
132
132
|
|
133
|
+
# Returns a copy of this node, with the parent and children links removed.
|
134
|
+
def detached_copy
|
135
|
+
Tree::TreeNode.new(@name, @content ? @content.clone : nil)
|
136
|
+
end
|
137
|
+
|
133
138
|
# Print the string representation of this node.
|
134
139
|
def to_s
|
135
140
|
|
@@ -261,14 +266,36 @@ module Tree
|
|
261
266
|
end
|
262
267
|
|
263
268
|
# Returns every node (including the receiver node) from the tree to the
|
264
|
-
# specified block. The traversal is depth first and from left to right
|
269
|
+
# specified block. The traversal is depth first and from left to right in
|
270
|
+
# pre-ordered sequence.
|
265
271
|
def each &block
|
266
272
|
yield self
|
267
273
|
children { |child| child.each(&block) }
|
268
274
|
end
|
269
275
|
|
270
|
-
#
|
271
|
-
#
|
276
|
+
# Traverses the tree in a pre-ordered sequence. This is equivalent to
|
277
|
+
# TreeNode#each
|
278
|
+
def preordered_each &block
|
279
|
+
each &block
|
280
|
+
end
|
281
|
+
|
282
|
+
# Performs breadth first traversal of the tree rooted at this node. The
|
283
|
+
# traversal in a given level is from left to right.
|
284
|
+
def breadth_each &block
|
285
|
+
node_queue = [self] # Create a queue with self as the initial entry
|
286
|
+
|
287
|
+
# Use a queue to do breadth traversal
|
288
|
+
until node_queue.empty?
|
289
|
+
node_to_traverse = node_queue.shift
|
290
|
+
yield node_to_traverse
|
291
|
+
# Enqueue the children from left to right.
|
292
|
+
node_to_traverse.children { |child| node_queue.push child }
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
296
|
+
# Yields all leaf nodes from this node to the specified block. May yield
|
297
|
+
# this node as well if this is a leaf node. Leaf traversal depth first and
|
298
|
+
# left to right.
|
272
299
|
def each_leaf &block
|
273
300
|
self.each { |node| yield(node) if node.isLeaf? }
|
274
301
|
end
|
@@ -461,6 +488,10 @@ module Tree
|
|
461
488
|
end
|
462
489
|
|
463
490
|
# $Log: tree.rb,v $
|
491
|
+
# Revision 1.19 2007/08/31 01:16:27 anupamsg
|
492
|
+
# Added breadth and pre-order traversals for the tree. Also added a method
|
493
|
+
# to return the detached copy of a node from the tree.
|
494
|
+
#
|
464
495
|
# Revision 1.18 2007/07/21 05:14:44 anupamsg
|
465
496
|
# Added a VERSION constant to the Tree module,
|
466
497
|
# and using the same in the Rakefile.
|
data/test/test_binarytree.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# testtree.rb
|
4
4
|
#
|
5
|
-
# $Revision: 1.
|
5
|
+
# $Revision: 1.3 $ by $Author: anupamsg $
|
6
6
|
# $Name: $
|
7
7
|
#
|
8
8
|
# Copyright (c) 2006, 2007 Anupam Sengupta
|
@@ -37,7 +37,6 @@
|
|
37
37
|
|
38
38
|
require 'test/unit'
|
39
39
|
require 'tree/binarytree'
|
40
|
-
require 'person'
|
41
40
|
|
42
41
|
# Test class for the Tree node.
|
43
42
|
class TC_BinaryTreeTest < Test::Unit::TestCase
|
@@ -171,6 +170,13 @@ class TC_BinaryTreeTest < Test::Unit::TestCase
|
|
171
170
|
end
|
172
171
|
|
173
172
|
# $Log: test_binarytree.rb,v $
|
173
|
+
# Revision 1.3 2007/10/02 03:07:30 anupamsg
|
174
|
+
# * Rakefile: Added an optional task for rcov code coverage.
|
175
|
+
#
|
176
|
+
# * test/test_binarytree.rb: Removed the unnecessary dependency on "Person" class.
|
177
|
+
#
|
178
|
+
# * test/test_tree.rb: Removed dependency on the redundant "Person" class.
|
179
|
+
#
|
174
180
|
# Revision 1.2 2007/08/30 22:06:13 anupamsg
|
175
181
|
# Added a new swap_children method for the Binary Tree class.
|
176
182
|
# Also made minor documentation updates and test additions.
|
data/test/test_tree.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# testtree.rb
|
4
4
|
#
|
5
|
-
# $Revision: 1.
|
5
|
+
# $Revision: 1.4 $ by $Author: anupamsg $
|
6
6
|
# $Name: $
|
7
7
|
#
|
8
8
|
# Copyright (c) 2006, 2007 Anupam Sengupta
|
@@ -37,11 +37,12 @@
|
|
37
37
|
|
38
38
|
require 'test/unit'
|
39
39
|
require 'tree'
|
40
|
-
require 'person'
|
41
40
|
|
42
41
|
# Test class for the Tree node.
|
43
42
|
class TC_TreeTest < Test::Unit::TestCase
|
44
43
|
|
44
|
+
Person = Struct::new(:First, :last)
|
45
|
+
|
45
46
|
def setup
|
46
47
|
@root = Tree::TreeNode.new("ROOT", "Root Node")
|
47
48
|
|
@@ -88,6 +89,7 @@ class TC_TreeTest < Test::Unit::TestCase
|
|
88
89
|
assert_equal("Root Node", @root.content, "Content should be 'Root Node'")
|
89
90
|
assert(@root.isRoot?, "Should identify as root")
|
90
91
|
assert(!@root.hasChildren?, "Cannot have any children")
|
92
|
+
assert(@root.hasContent?, "This root should have content")
|
91
93
|
assert_equal(1, @root.size, "Number of nodes should be one")
|
92
94
|
assert_nil(@root.siblings, "Root cannot have any children")
|
93
95
|
|
@@ -102,6 +104,50 @@ class TC_TreeTest < Test::Unit::TestCase
|
|
102
104
|
assert_same(@root, @child4.root, "Root should be ROOT")
|
103
105
|
end
|
104
106
|
|
107
|
+
def test_hasContent
|
108
|
+
aNode = Tree::TreeNode.new("A Node")
|
109
|
+
assert_nil(aNode.content, "The node should not have content")
|
110
|
+
assert(!aNode.hasContent?, "The node should not have content")
|
111
|
+
|
112
|
+
aNode.content = "Something"
|
113
|
+
assert_not_nil(aNode.content, "The node should now have content")
|
114
|
+
assert(aNode.hasContent?, "The node should now have content")
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_length
|
118
|
+
loadChildren
|
119
|
+
|
120
|
+
assert_equal(@root.size, @root.length, "Length and size methods should return the same result")
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_comparator
|
124
|
+
firstNode = Tree::TreeNode.new(1)
|
125
|
+
secondNode = Tree::TreeNode.new(2)
|
126
|
+
|
127
|
+
assert_equal(firstNode <=> nil, +1)
|
128
|
+
assert_equal(firstNode <=> secondNode, -1)
|
129
|
+
|
130
|
+
secondNode = Tree::TreeNode.new(1)
|
131
|
+
assert_equal(firstNode <=> secondNode, 0)
|
132
|
+
|
133
|
+
firstNode = Tree::TreeNode.new("ABC")
|
134
|
+
secondNode = Tree::TreeNode.new("XYZ")
|
135
|
+
|
136
|
+
assert_equal(firstNode <=> nil, +1)
|
137
|
+
assert_equal(firstNode <=> secondNode, -1)
|
138
|
+
|
139
|
+
secondNode = Tree::TreeNode.new("ABC")
|
140
|
+
assert_equal(firstNode <=> secondNode, 0)
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_to_s
|
144
|
+
aNode = Tree::TreeNode.new("A Node", "Some Content")
|
145
|
+
|
146
|
+
expectedString = "Node Name: A Node Content: Some Content Parent: <None> Children: 0 Total Nodes: 1"
|
147
|
+
|
148
|
+
assert_equal(expectedString, aNode.to_s, "The string representation should be same")
|
149
|
+
end
|
150
|
+
|
105
151
|
def test_firstSibling
|
106
152
|
loadChildren
|
107
153
|
|
@@ -383,9 +429,6 @@ class TC_TreeTest < Test::Unit::TestCase
|
|
383
429
|
def test_dump
|
384
430
|
loadChildren
|
385
431
|
|
386
|
-
pers = Person.new("John", "Doe")
|
387
|
-
#@root.content = pers
|
388
|
-
|
389
432
|
data = Marshal.dump(@root)
|
390
433
|
|
391
434
|
newRoot = Marshal.load(data)
|
@@ -415,7 +458,7 @@ class TC_TreeTest < Test::Unit::TestCase
|
|
415
458
|
end
|
416
459
|
|
417
460
|
def test_content
|
418
|
-
pers = Person
|
461
|
+
pers = Person::new("John", "Doe")
|
419
462
|
@root.content = pers
|
420
463
|
assert_same(pers, @root.content, "Content should be the same")
|
421
464
|
end
|
@@ -439,27 +482,133 @@ class TC_TreeTest < Test::Unit::TestCase
|
|
439
482
|
end
|
440
483
|
|
441
484
|
def test_breadth
|
442
|
-
|
485
|
+
assert_equal(1, @root.breadth, "A single node's breadth is 1")
|
443
486
|
|
444
|
-
|
445
|
-
|
487
|
+
@root << @child1
|
488
|
+
assert_equal(1, @root.breadth, "This should be of breadth 1")
|
446
489
|
|
447
|
-
|
448
|
-
|
449
|
-
|
490
|
+
@root << @child2
|
491
|
+
assert_equal(2, @child1.breadth, "This should be of breadth 2")
|
492
|
+
assert_equal(2, @child2.breadth, "This should be of breadth 2")
|
450
493
|
|
451
|
-
|
452
|
-
|
453
|
-
|
494
|
+
@root << @child3
|
495
|
+
assert_equal(3, @child1.breadth, "This should be of breadth 3")
|
496
|
+
assert_equal(3, @child2.breadth, "This should be of breadth 3")
|
497
|
+
|
498
|
+
@child3 << @child4
|
499
|
+
assert_equal(1, @child4.breadth, "This should be of breadth 1")
|
500
|
+
end
|
501
|
+
|
502
|
+
def test_breadth_each
|
503
|
+
j = Tree::TreeNode.new("j")
|
504
|
+
f = Tree::TreeNode.new("f")
|
505
|
+
k = Tree::TreeNode.new("k")
|
506
|
+
a = Tree::TreeNode.new("a")
|
507
|
+
d = Tree::TreeNode.new("d")
|
508
|
+
h = Tree::TreeNode.new("h")
|
509
|
+
z = Tree::TreeNode.new("z")
|
510
|
+
|
511
|
+
# The expected order of response
|
512
|
+
expected_array = [j,
|
513
|
+
f, k,
|
514
|
+
a, h, z,
|
515
|
+
d]
|
516
|
+
|
517
|
+
# Create the following Tree
|
518
|
+
# j <-- level 0 (Root)
|
519
|
+
# / \
|
520
|
+
# f k <-- level 1
|
521
|
+
# / \ \
|
522
|
+
# a h z <-- level 2
|
523
|
+
# \
|
524
|
+
# d <-- level 3
|
525
|
+
j << f << a << d
|
526
|
+
f << h
|
527
|
+
j << k << z
|
528
|
+
|
529
|
+
result_array = []
|
530
|
+
|
531
|
+
# Create the response
|
532
|
+
j.breadth_each { |node| result_array << node.detached_copy }
|
533
|
+
|
534
|
+
expected_array.each_index do |i|
|
535
|
+
# Match only the names.
|
536
|
+
assert_equal(expected_array[i].name, result_array[i].name)
|
537
|
+
end
|
538
|
+
|
539
|
+
end
|
540
|
+
|
541
|
+
|
542
|
+
def test_preordered_each
|
543
|
+
j = Tree::TreeNode.new("j")
|
544
|
+
f = Tree::TreeNode.new("f")
|
545
|
+
k = Tree::TreeNode.new("k")
|
546
|
+
a = Tree::TreeNode.new("a")
|
547
|
+
d = Tree::TreeNode.new("d")
|
548
|
+
h = Tree::TreeNode.new("h")
|
549
|
+
z = Tree::TreeNode.new("z")
|
550
|
+
|
551
|
+
# The expected order of response
|
552
|
+
expected_array = [j, f, a, d, h, k, z]
|
553
|
+
|
554
|
+
# Create the following Tree
|
555
|
+
# j <-- level 0 (Root)
|
556
|
+
# / \
|
557
|
+
# f k <-- level 1
|
558
|
+
# / \ \
|
559
|
+
# a h z <-- level 2
|
560
|
+
# \
|
561
|
+
# d <-- level 3
|
562
|
+
j << f << a << d
|
563
|
+
f << h
|
564
|
+
j << k << z
|
565
|
+
|
566
|
+
result_array = []
|
567
|
+
j.preordered_each { |node| result_array << node.detached_copy}
|
568
|
+
|
569
|
+
expected_array.each_index do |i|
|
570
|
+
# Match only the names.
|
571
|
+
assert_equal(expected_array[i].name, result_array[i].name)
|
572
|
+
end
|
573
|
+
end
|
574
|
+
|
575
|
+
def test_detached_copy
|
576
|
+
loadChildren
|
577
|
+
|
578
|
+
assert(@root.hasChildren?, "The root should have children")
|
579
|
+
copy_of_root = @root.detached_copy
|
580
|
+
assert(!copy_of_root.hasChildren?, "The copy should not have children")
|
581
|
+
assert_equal(@root.name, copy_of_root.name, "The names should be equal")
|
582
|
+
|
583
|
+
# Try the same test with a child node
|
584
|
+
assert(!@child3.isRoot?, "Child 3 is not a root")
|
585
|
+
assert(@child3.hasChildren?, "Child 3 has children")
|
586
|
+
copy_of_child3 = @child3.detached_copy
|
587
|
+
assert(copy_of_child3.isRoot?, "Child 3's copy is a root")
|
588
|
+
assert(!copy_of_child3.hasChildren?, "Child 3's copy does not have children")
|
589
|
+
end
|
454
590
|
|
455
|
-
@child3 << @child4
|
456
|
-
assert_equal(1, @child4.breadth, "This should be of breadth 1")
|
457
|
-
end
|
458
591
|
end
|
459
592
|
|
460
593
|
__END__
|
461
594
|
|
462
595
|
# $Log: test_tree.rb,v $
|
596
|
+
# Revision 1.4 2007/10/02 03:38:11 anupamsg
|
597
|
+
# Removed dependency on the redundant "Person" class.
|
598
|
+
# (TC_TreeTest::test_comparator): Added a new test for the spaceship operator.
|
599
|
+
# (TC_TreeTest::test_hasContent): Added tests for hasContent? and length methods.
|
600
|
+
#
|
601
|
+
# Revision 1.3 2007/10/02 03:07:30 anupamsg
|
602
|
+
# * Rakefile: Added an optional task for rcov code coverage.
|
603
|
+
#
|
604
|
+
# * test/test_binarytree.rb: Removed the unnecessary dependency on "Person" class.
|
605
|
+
#
|
606
|
+
# * test/test_tree.rb: Removed dependency on the redundant "Person" class.
|
607
|
+
#
|
608
|
+
# Revision 1.2 2007/08/31 01:16:28 anupamsg
|
609
|
+
# Added breadth and pre-order traversals for the tree. Also added a method
|
610
|
+
# to return the detached copy of a node from the tree.
|
611
|
+
#
|
463
612
|
# Revision 1.1 2007/07/21 04:52:38 anupamsg
|
464
613
|
# Renamed the test files.
|
465
614
|
#
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.
|
2
|
+
rubygems_version: 0.9.4
|
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.2
|
7
|
+
date: 2007-10-01 00:00:00 -07:00
|
8
8
|
summary: Ruby implementation of the Tree data structure.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -31,13 +31,11 @@ authors:
|
|
31
31
|
files:
|
32
32
|
- ChangeLog
|
33
33
|
- COPYING
|
34
|
-
- lib
|
35
34
|
- Rakefile
|
36
35
|
- README
|
37
|
-
-
|
36
|
+
- TAGS
|
38
37
|
- lib/tree/binarytree.rb
|
39
38
|
- lib/tree.rb
|
40
|
-
- test/person.rb
|
41
39
|
- test/test_binarytree.rb
|
42
40
|
- test/test_tree.rb
|
43
41
|
test_files:
|
@@ -55,5 +53,13 @@ extensions: []
|
|
55
53
|
|
56
54
|
requirements: []
|
57
55
|
|
58
|
-
dependencies:
|
59
|
-
|
56
|
+
dependencies:
|
57
|
+
- !ruby/object:Gem::Dependency
|
58
|
+
name: rake
|
59
|
+
version_requirement:
|
60
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 0.7.2
|
65
|
+
version:
|
data/test/person.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
# person.rb
|
2
|
-
#
|
3
|
-
# $Revision: 1.8 $ by $Author: anupamsg $
|
4
|
-
# $Name: $
|
5
|
-
#
|
6
|
-
# Copyright (c) 2006, 2007 Anupam Sengupta
|
7
|
-
#
|
8
|
-
# All rights reserved.
|
9
|
-
#
|
10
|
-
# Redistribution and use in source and binary forms, with or without modification,
|
11
|
-
# are permitted provided that the following conditions are met:
|
12
|
-
#
|
13
|
-
# - Redistributions of source code must retain the above copyright notice, this
|
14
|
-
# list of conditions and the following disclaimer.
|
15
|
-
#
|
16
|
-
# - Redistributions in binary form must reproduce the above copyright notice, this
|
17
|
-
# list of conditions and the following disclaimer in the documentation and/or
|
18
|
-
# other materials provided with the distribution.
|
19
|
-
#
|
20
|
-
# - Neither the name of the organization nor the names of its contributors may
|
21
|
-
# be used to endorse or promote products derived from this software without
|
22
|
-
# specific prior written permission.
|
23
|
-
#
|
24
|
-
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
25
|
-
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
26
|
-
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
27
|
-
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
28
|
-
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
29
|
-
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
30
|
-
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
31
|
-
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
32
|
-
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
33
|
-
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
34
|
-
#
|
35
|
-
|
36
|
-
# Define a simple class for testing the RubyTree implementation
|
37
|
-
class Person
|
38
|
-
attr_reader :first, :last
|
39
|
-
attr_writer :first, :last
|
40
|
-
def initialize(first, last)
|
41
|
-
@first = first
|
42
|
-
@last = last
|
43
|
-
end
|
44
|
-
|
45
|
-
def to_s
|
46
|
-
"#@first, #@last"
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
# $Log: person.rb,v $
|
52
|
-
# Revision 1.8 2007/07/17 03:39:28 anupamsg
|
53
|
-
# Moved the CVS Log keyword to end of the files.
|
54
|
-
#
|