rubytree 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +24 -1
- data/README +2 -2
- data/Rakefile +10 -2
- data/TAGS +35 -1
- data/lib/tree.rb +29 -16
- data/lib/tree/binarytree.rb +114 -0
- data/test/testbinarytree.rb +143 -0
- data/test/testtree.rb +44 -1
- metadata +5 -2
data/ChangeLog
CHANGED
@@ -1,4 +1,27 @@
|
|
1
|
-
2007-07-
|
1
|
+
2007-07-18 Anupam Sengupta <anupamsg@gmail.com>
|
2
|
+
|
3
|
+
* test/testbinarytree.rb (TC_BinaryTreeTest::test_isLeftChild):
|
4
|
+
Minor syntax correction.
|
5
|
+
|
6
|
+
* lib/tree.rb (Tree::TreeNode::depth): Added a tree depth
|
7
|
+
computation method.
|
8
|
+
(Tree::TreeNode::breadth): Added a tree breadth method.
|
9
|
+
|
10
|
+
* test/testtree.rb (TC_TreeTest::test_depth/test_breadth): Added a
|
11
|
+
test for the depth and breadth method.
|
12
|
+
|
13
|
+
* lib/tree/binarytree.rb (Tree::BinaryTreeNode::is*Child):
|
14
|
+
Added tests for determining whether a node is a left or right
|
15
|
+
child.
|
16
|
+
|
17
|
+
* test/testbinarytree.rb: Added the test cases for the binary tree
|
18
|
+
implementation.
|
19
|
+
(TC_BinaryTreeTest::test_is*Child): Added tests for right or left
|
20
|
+
childs.
|
21
|
+
|
22
|
+
* lib/tree/binarytree.rb: Added the binary tree implementation.
|
23
|
+
|
24
|
+
2007-07-17 Anupam Sengupta <anupamsg@gmail.com>
|
2
25
|
|
3
26
|
* lib/tree.rb (Tree::TreeNode::parentage): Renamed 'ancestors'
|
4
27
|
method to 'parentage' to avoid clobbering Module.ancestors
|
data/README
CHANGED
data/Rakefile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Rakefile
|
2
2
|
#
|
3
|
-
# $Revision: 1.
|
3
|
+
# $Revision: 1.18 $ by $Author: anupamsg $
|
4
4
|
# $Name: $
|
5
5
|
#
|
6
6
|
# Copyright (c) 2006, 2007 Anupam Sengupta
|
@@ -42,7 +42,7 @@ require 'rake/rdoctask'
|
|
42
42
|
desc "Default Task"
|
43
43
|
task :default => :gem
|
44
44
|
|
45
|
-
PKG_VERSION = '0.
|
45
|
+
PKG_VERSION = '0.4.0'
|
46
46
|
PKG_FILES = FileList[
|
47
47
|
'[A-Z]*',
|
48
48
|
'lib/**/*.rb',
|
@@ -107,6 +107,14 @@ begin
|
|
107
107
|
end
|
108
108
|
|
109
109
|
# $Log: Rakefile,v $
|
110
|
+
# Revision 1.18 2007/07/18 20:15:06 anupamsg
|
111
|
+
# Added two predicate methods in BinaryTreeNode to determine whether a node
|
112
|
+
# is a left or a right node.
|
113
|
+
#
|
114
|
+
# Revision 1.17 2007/07/18 07:17:34 anupamsg
|
115
|
+
# Fixed a issue where TreeNode.ancestors was shadowing Module.ancestors. This method
|
116
|
+
# has been renamed to TreeNode.parentage.
|
117
|
+
#
|
110
118
|
# Revision 1.16 2007/07/17 05:34:03 anupamsg
|
111
119
|
# Added an optional tags Rake-task for generating the TAGS file for Emacs.
|
112
120
|
#
|
data/TAGS
CHANGED
@@ -8,7 +8,7 @@ module Tree::Tree48,2020
|
|
8
8
|
attr_writer :content::Tree::TreeNode#content107,3886
|
9
9
|
def initialize::Tree::TreeNode#Tree::TreeNode.new118,4158
|
10
10
|
def to_s::Tree::TreeNode#to_s132,4447
|
11
|
-
def
|
11
|
+
def parentage::Tree::TreeNode#parentage144,4836
|
12
12
|
def parent=::Tree::TreeNode#parent=159,5172
|
13
13
|
def <<::Tree::TreeNode#<<167,5408
|
14
14
|
def add::Tree::TreeNode#add174,5667
|
@@ -55,6 +55,40 @@ class Person::Person37,1683
|
|
55
55
|
def to_s::Person#to_s45,1853
|
56
56
|
|
57
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
|
58
92
|
class TC_TreeTest::TC_TreeTest43,1728
|
59
93
|
def setup::TC_TreeTest#setup45,1781
|
60
94
|
def loadChildren::TC_TreeTest#loadChildren73,2687
|
data/lib/tree.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# tree.rb
|
2
2
|
#
|
3
|
-
# $Revision: 1.
|
3
|
+
# $Revision: 1.16 $ by $Author: anupamsg $
|
4
4
|
# $Name: $
|
5
5
|
#
|
6
|
-
# = tree.rb - Generic Tree implementation
|
6
|
+
# = tree.rb - Generic Multi-way Tree implementation
|
7
7
|
#
|
8
8
|
# Provides a generic tree data structure with ability to
|
9
9
|
# store keyed node elements in the tree. The implementation
|
@@ -440,27 +440,40 @@ module Tree
|
|
440
440
|
loadDumpRep(str)
|
441
441
|
end
|
442
442
|
|
443
|
+
# Returns depth of the tree from this node. A single leaf node has a
|
444
|
+
# depth of 1.
|
445
|
+
def depth
|
446
|
+
return 1 if isLeaf?
|
447
|
+
1 + @children.collect { |child| child.depth }.max
|
448
|
+
end
|
449
|
+
|
450
|
+
# Returns breadth of the tree at this node level. A single node has a
|
451
|
+
# breadth of 1.
|
452
|
+
def breadth
|
453
|
+
return 1 if isRoot?
|
454
|
+
parent.children.size
|
455
|
+
end
|
456
|
+
|
443
457
|
protected :parent=, :setAsRoot!
|
444
458
|
private_class_method :loadDumpRep
|
445
459
|
|
446
460
|
end
|
447
461
|
end
|
448
462
|
|
449
|
-
if __FILE__ == $0
|
450
|
-
root = Tree::TreeNode.new("ROOT")
|
451
|
-
child1 = Tree::TreeNode.new("CHILD1")
|
452
|
-
child2 = Tree::TreeNode.new("CHILD2")
|
453
|
-
child3 = Tree::TreeNode.new("CHILD3")
|
454
|
-
grandchild1 = Tree::TreeNode.new("GRANDCHILD1")
|
455
|
-
root << child1 << grandchild1
|
456
|
-
root << child2
|
457
|
-
root << child3
|
458
|
-
|
459
|
-
puts child1.nextSibling
|
460
|
-
root.printTree
|
461
|
-
end
|
462
|
-
|
463
463
|
# $Log: tree.rb,v $
|
464
|
+
# Revision 1.16 2007/07/18 23:38:55 anupamsg
|
465
|
+
# Minor updates to tree.rb
|
466
|
+
#
|
467
|
+
# Revision 1.15 2007/07/18 22:11:50 anupamsg
|
468
|
+
# Added depth and breadth methods for the TreeNode.
|
469
|
+
#
|
470
|
+
# Revision 1.14 2007/07/18 19:33:27 anupamsg
|
471
|
+
# Added a new binary tree implementation.
|
472
|
+
#
|
473
|
+
# Revision 1.13 2007/07/18 07:17:34 anupamsg
|
474
|
+
# Fixed a issue where TreeNode.ancestors was shadowing Module.ancestors. This method
|
475
|
+
# has been renamed to TreeNode.parentage.
|
476
|
+
#
|
464
477
|
# Revision 1.12 2007/07/17 03:39:28 anupamsg
|
465
478
|
# Moved the CVS Log keyword to end of the files.
|
466
479
|
#
|
@@ -0,0 +1,114 @@
|
|
1
|
+
# binarytree.rb
|
2
|
+
#
|
3
|
+
# $Revision: 1.2 $ 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
|
+
def rightChild
|
74
|
+
children.last
|
75
|
+
end
|
76
|
+
|
77
|
+
# Sets the left child. If a previous child existed, it is replaced.
|
78
|
+
def leftChild=(leftChild)
|
79
|
+
@children[0] = leftChild
|
80
|
+
@childrenHash[leftChild.name] = leftChild
|
81
|
+
end
|
82
|
+
|
83
|
+
# Sets the right child. If a previous child existed, it is replaced.
|
84
|
+
def rightChild=(rightChild)
|
85
|
+
@children[1] = rightChild
|
86
|
+
@childrenHash[rightChild.name] = rightChild
|
87
|
+
end
|
88
|
+
|
89
|
+
# Returns true if this is the left child of its parent. Always returns false
|
90
|
+
# if this is the root node.
|
91
|
+
def isLeftChild?
|
92
|
+
return nil if isRoot?
|
93
|
+
self == parent.leftChild
|
94
|
+
end
|
95
|
+
|
96
|
+
# Returns true if this is the right child of its parent. Always returns false
|
97
|
+
# if this is the root node.
|
98
|
+
def isRightChild?
|
99
|
+
return nil if isRoot?
|
100
|
+
self == parent.rightChild
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
# $Log: binarytree.rb,v $
|
108
|
+
# Revision 1.2 2007/07/18 20:15:06 anupamsg
|
109
|
+
# Added two predicate methods in BinaryTreeNode to determine whether a node
|
110
|
+
# 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
|
+
#
|
@@ -0,0 +1,143 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# testtree.rb
|
4
|
+
#
|
5
|
+
# $Revision: 1.4 $ by $Author: anupamsg $
|
6
|
+
# $Name: $
|
7
|
+
#
|
8
|
+
# Copyright (c) 2006, 2007 Anupam Sengupta
|
9
|
+
#
|
10
|
+
# All rights reserved.
|
11
|
+
#
|
12
|
+
# Redistribution and use in source and binary forms, with or without modification,
|
13
|
+
# are permitted provided that the following conditions are met:
|
14
|
+
#
|
15
|
+
# - Redistributions of source code must retain the above copyright notice, this
|
16
|
+
# list of conditions and the following disclaimer.
|
17
|
+
#
|
18
|
+
# - Redistributions in binary form must reproduce the above copyright notice, this
|
19
|
+
# list of conditions and the following disclaimer in the documentation and/or
|
20
|
+
# other materials provided with the distribution.
|
21
|
+
#
|
22
|
+
# - Neither the name of the organization nor the names of its contributors may
|
23
|
+
# be used to endorse or promote products derived from this software without
|
24
|
+
# specific prior written permission.
|
25
|
+
#
|
26
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
27
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
28
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
29
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
30
|
+
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
31
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
32
|
+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
33
|
+
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
34
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
35
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
36
|
+
#
|
37
|
+
|
38
|
+
require 'test/unit'
|
39
|
+
require 'tree/binarytree'
|
40
|
+
require 'person'
|
41
|
+
|
42
|
+
# Test class for the Tree node.
|
43
|
+
class TC_BinaryTreeTest < Test::Unit::TestCase
|
44
|
+
|
45
|
+
def setup
|
46
|
+
@root = Tree::BinaryTreeNode.new("ROOT", "Root Node")
|
47
|
+
|
48
|
+
@left_child1 = Tree::BinaryTreeNode.new("A Child at Left", "Child Node @ left")
|
49
|
+
@right_child1 = Tree::BinaryTreeNode.new("B Child at right", "Child Node @ right")
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
def teardown
|
54
|
+
@root.remove!(@left_child1)
|
55
|
+
@root.remove!(@right_child1)
|
56
|
+
@root = nil
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_initialize
|
60
|
+
assert_not_nil(@root, "Binary tree's Root should have been created")
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_add
|
64
|
+
@root.add @left_child1
|
65
|
+
assert_same(@left_child1, @root.leftChild, "The left node should be left_child1")
|
66
|
+
assert_same(@left_child1, @root.firstChild, "The first node should be left_child1")
|
67
|
+
|
68
|
+
@root.add @right_child1
|
69
|
+
assert_same(@right_child1, @root.rightChild, "The right node should be right_child1")
|
70
|
+
assert_same(@right_child1, @root.lastChild, "The first node should be right_child1")
|
71
|
+
|
72
|
+
assert_raise RuntimeError do
|
73
|
+
@root.add Tree::BinaryTreeNode.new("The third child!")
|
74
|
+
end
|
75
|
+
|
76
|
+
assert_raise RuntimeError do
|
77
|
+
@root << Tree::BinaryTreeNode.new("The third child!")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_left
|
82
|
+
@root << @left_child1
|
83
|
+
@root << @right_child1
|
84
|
+
assert_same(@left_child1, @root.leftChild, "The left child should be 'left_child1")
|
85
|
+
assert_not_same(@right_child1, @root.leftChild, "The right_child1 is not the left child")
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_right
|
89
|
+
@root << @left_child1
|
90
|
+
@root << @right_child1
|
91
|
+
assert_same(@right_child1, @root.rightChild, "The right child should be 'right_child1")
|
92
|
+
assert_not_same(@left_child1, @root.rightChild, "The left_child1 is not the left child")
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_left_assignment
|
96
|
+
@root << @left_child1
|
97
|
+
@root << @right_child1
|
98
|
+
assert_same(@left_child1, @root.leftChild, "The left child should be 'left_child1")
|
99
|
+
|
100
|
+
@root.leftChild = Tree::BinaryTreeNode.new("New Left Child")
|
101
|
+
assert_equal("New Left Child", @root.leftChild.name, "The left child should now be the new child")
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_right_assignment
|
105
|
+
@root << @left_child1
|
106
|
+
@root << @right_child1
|
107
|
+
assert_same(@right_child1, @root.rightChild, "The right child should be 'right_child1")
|
108
|
+
|
109
|
+
@root.rightChild = Tree::BinaryTreeNode.new("New Right Child")
|
110
|
+
assert_equal("New Right Child", @root.rightChild.name, "The right child should now be the new child")
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_isLeftChild?
|
114
|
+
@root << @left_child1
|
115
|
+
@root << @right_child1
|
116
|
+
|
117
|
+
assert(@left_child1.isLeftChild?, "left_child1 should be the left child")
|
118
|
+
assert(!@right_child1.isLeftChild?, "left_child1 should be the left child")
|
119
|
+
|
120
|
+
assert(!@root.isLeftChild?, "Root is neither left child nor right")
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_isRightChild?
|
124
|
+
@root << @left_child1
|
125
|
+
@root << @right_child1
|
126
|
+
|
127
|
+
assert(@right_child1.isRightChild?, "right_child1 should be the right child")
|
128
|
+
assert(!@left_child1.isRightChild?, "right_child1 should be the right child")
|
129
|
+
assert(!@root.isRightChild?, "Root is neither left child nor right")
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
# $Log: testbinarytree.rb,v $
|
134
|
+
# Revision 1.4 2007/07/19 02:03:57 anupamsg
|
135
|
+
# Minor syntax correction.
|
136
|
+
#
|
137
|
+
# Revision 1.3 2007/07/19 02:02:12 anupamsg
|
138
|
+
# Removed useless files (including rdoc, which should be generated for each release.
|
139
|
+
#
|
140
|
+
# Revision 1.2 2007/07/18 20:15:06 anupamsg
|
141
|
+
# Added two predicate methods in BinaryTreeNode to determine whether a node
|
142
|
+
# is a left or a right node.
|
143
|
+
#
|
data/test/testtree.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# testtree.rb
|
4
4
|
#
|
5
|
-
# $Revision: 1.
|
5
|
+
# $Revision: 1.13 $ by $Author: anupamsg $
|
6
6
|
# $Name: $
|
7
7
|
#
|
8
8
|
# Copyright (c) 2006, 2007 Anupam Sengupta
|
@@ -419,11 +419,54 @@ class TC_TreeTest < Test::Unit::TestCase
|
|
419
419
|
@root.content = pers
|
420
420
|
assert_same(pers, @root.content, "Content should be the same")
|
421
421
|
end
|
422
|
+
|
423
|
+
def test_depth
|
424
|
+
assert_equal(1, @root.depth, "A single node's depth is 1")
|
425
|
+
|
426
|
+
@root << @child1
|
427
|
+
assert_equal(2, @root.depth, "This should be of depth 2")
|
428
|
+
|
429
|
+
@root << @child2
|
430
|
+
assert_equal(2, @root.depth, "This should be of depth 2")
|
431
|
+
|
432
|
+
@child2 << @child3
|
433
|
+
assert_equal(3, @root.depth, "This should be of depth 3")
|
434
|
+
assert_equal(2, @child2.depth, "This should be of depth 2")
|
435
|
+
|
436
|
+
@child3 << @child4
|
437
|
+
assert_equal(4, @root.depth, "This should be of depth 4")
|
438
|
+
|
439
|
+
end
|
440
|
+
|
441
|
+
def test_breadth
|
442
|
+
assert_equal(1, @root.breadth, "A single node's breadth is 1")
|
443
|
+
|
444
|
+
@root << @child1
|
445
|
+
assert_equal(1, @root.breadth, "This should be of breadth 1")
|
446
|
+
|
447
|
+
@root << @child2
|
448
|
+
assert_equal(2, @child1.breadth, "This should be of breadth 2")
|
449
|
+
assert_equal(2, @child2.breadth, "This should be of breadth 2")
|
450
|
+
|
451
|
+
@root << @child3
|
452
|
+
assert_equal(3, @child1.breadth, "This should be of breadth 3")
|
453
|
+
assert_equal(3, @child2.breadth, "This should be of breadth 3")
|
454
|
+
|
455
|
+
@child3 << @child4
|
456
|
+
assert_equal(1, @child4.breadth, "This should be of breadth 1")
|
457
|
+
end
|
422
458
|
end
|
423
459
|
|
424
460
|
__END__
|
425
461
|
|
426
462
|
# $Log: testtree.rb,v $
|
463
|
+
# Revision 1.13 2007/07/18 22:11:50 anupamsg
|
464
|
+
# Added depth and breadth methods for the TreeNode.
|
465
|
+
#
|
466
|
+
# Revision 1.12 2007/07/18 07:17:34 anupamsg
|
467
|
+
# Fixed a issue where TreeNode.ancestors was shadowing Module.ancestors. This method
|
468
|
+
# has been renamed to TreeNode.parentage.
|
469
|
+
#
|
427
470
|
# Revision 1.11 2007/07/17 03:39:29 anupamsg
|
428
471
|
# Moved the CVS Log keyword to end of the files.
|
429
472
|
#
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: rubytree
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2007-07-
|
6
|
+
version: 0.4.0
|
7
|
+
date: 2007-07-18 00:00:00 -07:00
|
8
8
|
summary: Ruby implementation of the Tree data structure.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -34,10 +34,13 @@ files:
|
|
34
34
|
- Rakefile
|
35
35
|
- README
|
36
36
|
- TAGS
|
37
|
+
- lib/tree/binarytree.rb
|
37
38
|
- lib/tree.rb
|
38
39
|
- test/person.rb
|
40
|
+
- test/testbinarytree.rb
|
39
41
|
- test/testtree.rb
|
40
42
|
test_files:
|
43
|
+
- test/testbinarytree.rb
|
41
44
|
- test/testtree.rb
|
42
45
|
rdoc_options: []
|
43
46
|
|