ealdent-simple-tree 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/LICENSE +10 -0
  2. data/lib/simple_tree.rb +161 -0
  3. metadata +54 -0
data/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ Copyright (c) 2008, Jason M. Adams
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+ * The name of Jason M. Adams may not be used to endorse or promote products derived from this software without specific prior written permission.
9
+
10
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,161 @@
1
+ #
2
+ # Ruby Tree Interface by Jason M. Adams. Distributed under the BSD license, please see LICENSE for more information.
3
+ #
4
+ # To use this interface, drop it in your current class with +include+.
5
+ # You must then simply implement the +parent+ and +children+ methods.
6
+ # * +parent+ returns the parent node of the current node or else nil if it's a root
7
+ # * +children+ returns an +Array+ of all children of this node or an empty +Array+ if it is a leaf node
8
+ #
9
+ module SimpleTree
10
+ def parent() raise "parent must be overridden"; end
11
+ def children() raise "children must be overridden"; end
12
+
13
+
14
+ #
15
+ # Return whether this node is a leaf node in the hierarchy.
16
+ #
17
+ def is_leaf?
18
+ if children.size == 0
19
+ true
20
+ else
21
+ false
22
+ end
23
+ end
24
+
25
+
26
+ #
27
+ # Determine whether this is the root node in the hierarchy.
28
+ #
29
+ def is_root?
30
+ if parent
31
+ false
32
+ else
33
+ true
34
+ end
35
+ end
36
+
37
+
38
+ #
39
+ # Determine whether the node has a parent.
40
+ #
41
+ def has_parent?
42
+ not is_root?
43
+ end
44
+
45
+
46
+ #
47
+ # Determine whether the node has children.
48
+ #
49
+ def has_children?
50
+ not is_leaf?
51
+ end
52
+
53
+
54
+ #
55
+ # Return the height of this subtree. A single node has height 1.
56
+ #
57
+ def height
58
+ heights = children.map {|child| child.height}
59
+ return 1 if heights.size == 0
60
+ return heights.max + 1
61
+ end
62
+
63
+
64
+ #
65
+ # Return an array containing the siblings of the current node.
66
+ #
67
+ def siblings
68
+ # handle case where this is the root node
69
+ return Array.new unless parent
70
+
71
+ sibs = Array.new
72
+ parent.children.each do |child|
73
+ next if child.id == self.id
74
+ sibs << child
75
+ end
76
+
77
+ sibs
78
+ end
79
+
80
+
81
+ #
82
+ # Return every node descending from this node's parent (except this node).
83
+ # This include all of the node's descendants.
84
+ #
85
+ def family
86
+ if parent
87
+ fam = [parent] + parent.descendants
88
+ else
89
+ fam = descendants
90
+ end
91
+
92
+ fam.delete(self)
93
+ fam
94
+ end
95
+
96
+
97
+ #
98
+ # Return an array containing every descendant of the current node.
99
+ #
100
+ def descendants
101
+ d = Array.new
102
+
103
+ children.each do |child|
104
+ d << child
105
+ d << child.descendants
106
+ end
107
+
108
+ d.flatten
109
+ end
110
+
111
+
112
+ #
113
+ # Return the grandparent of this node.
114
+ #
115
+ def grandparent
116
+ parent.parent if parent # returns nil by default if no parent
117
+ end
118
+
119
+
120
+ #
121
+ # Return all the leaf nodes having the current node as an ancestor.
122
+ #
123
+ def leaves
124
+ outp = Array.new
125
+ children.each do |child|
126
+ if child.is_leaf?
127
+ outp << child
128
+ else
129
+ outp << child.leaves
130
+ end
131
+ end
132
+
133
+ outp.flatten
134
+ end
135
+
136
+
137
+ #
138
+ # Helper method for to_s, returns a tree representation of the subtree
139
+ # rooted at this node. This assumes some sort of identifier is specified
140
+ # for the object being called (self.name, self.identifier, etc)
141
+ #
142
+ def tree_rep(depth=0)
143
+ if self.name
144
+ ident = self.name
145
+ elsif self.identifier
146
+ ident = self.identifier
147
+ else
148
+ ident = self.object_id
149
+ end
150
+ if depth > 0
151
+ outp = " #{([" "] * (depth - 1)).join("|")}\\- #{ident}\n"
152
+ else
153
+ outp = "#{ident}\n"
154
+ end
155
+ children.each do |child|
156
+ outp += child.tree_rep(depth + 1)
157
+ end
158
+ outp
159
+ end
160
+
161
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ealdent-simple-tree
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason M. Adams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-19 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Interface for very simple trees. You only have to implement parent (returns the single parent of the current node) and children (returns an Array of all the children of this node) and initialize. Drop this class in via include and presto!
17
+ email: jasonmadams@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - LICENSE
26
+ - lib/simple_tree.rb
27
+ has_rdoc: true
28
+ homepage: http://github.com/ealdent/simple-tree
29
+ post_install_message:
30
+ rdoc_options: []
31
+
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: "0"
39
+ version:
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ requirements: []
47
+
48
+ rubyforge_project:
49
+ rubygems_version: 1.2.0
50
+ signing_key:
51
+ specification_version: 2
52
+ summary: Interface for very simple trees.
53
+ test_files: []
54
+