Chris@0
|
1
|
Chris@0
|
2 __ _ _
|
Chris@0
|
3 /__\_ _| |__ _ _| |_ _ __ ___ ___
|
Chris@0
|
4 / \// | | | '_ \| | | | __| '__/ _ \/ _ \
|
Chris@0
|
5 / _ \ |_| | |_) | |_| | |_| | | __/ __/
|
Chris@0
|
6 \/ \_/\__,_|_.__/ \__, |\__|_| \___|\___|
|
Chris@0
|
7 |___/
|
Chris@0
|
8
|
Chris@0
|
9 (c) 2006, 2007 Anupam Sengupta
|
Chris@0
|
10 http://rubytree.rubyforge.org
|
Chris@0
|
11
|
Chris@0
|
12 Rubytree is a simple implementation of the generic Tree data structure. This
|
Chris@0
|
13 implementation is node-centric, where the individual nodes on the tree are the
|
Chris@0
|
14 primary objects and drive the structure.
|
Chris@0
|
15
|
Chris@0
|
16 == INSTALL:
|
Chris@0
|
17
|
Chris@0
|
18 Rubytree is an open source project and is hosted at:
|
Chris@0
|
19
|
Chris@0
|
20 http://rubytree.rubyforge.org
|
Chris@0
|
21
|
Chris@0
|
22 Rubytree can be downloaded as a Rubygem or as a tar/zip file from:
|
Chris@0
|
23
|
Chris@0
|
24 http://rubyforge.org/frs/?group_id=1215&release_id=8817
|
Chris@0
|
25
|
Chris@0
|
26 The file-name is one of:
|
Chris@0
|
27
|
Chris@0
|
28 rubytree-<VERSION>.gem - The Rubygem
|
Chris@0
|
29 rubytree-<VERSION>.tgz - GZipped source files
|
Chris@0
|
30 rubytree-<VERSION>.zip - Zipped source files
|
Chris@0
|
31
|
Chris@0
|
32 Download the appropriate file-type for your system.
|
Chris@0
|
33
|
Chris@0
|
34 It is recommended to install Rubytree as a Ruby Gem, as this is an easy way to
|
Chris@0
|
35 keep the version updated, and keep multiple versions of the library available on
|
Chris@0
|
36 your system.
|
Chris@0
|
37
|
Chris@0
|
38 === Installing the Gem
|
Chris@0
|
39
|
Chris@0
|
40 To Install the Gem, from a Terminal/CLI command prompt, issue the command:
|
Chris@0
|
41
|
Chris@0
|
42 gem install rubytree
|
Chris@0
|
43
|
Chris@0
|
44 This should install the gem file for Rubytree. Note that you may need to be a
|
Chris@0
|
45 super-user (root) to successfully install the gem.
|
Chris@0
|
46
|
Chris@0
|
47 === Installing from the tgz/zip file
|
Chris@0
|
48
|
Chris@0
|
49 Extract the archive file (tgz or zip) and run the following command from the
|
Chris@0
|
50 top-level source directory:
|
Chris@0
|
51
|
Chris@0
|
52 ruby ./setup.rb
|
Chris@0
|
53
|
Chris@0
|
54 You may need administrator/super-user privileges to complete the setup using
|
Chris@0
|
55 this method.
|
Chris@0
|
56
|
Chris@0
|
57 == DOCUMENTATION:
|
Chris@0
|
58
|
Chris@0
|
59 The primary class for this implementation is Tree::TreeNode. See the
|
Chris@0
|
60 class documentation for an usage example.
|
Chris@0
|
61
|
Chris@0
|
62 From a command line/terminal prompt, you can issue the following command to view
|
Chris@0
|
63 the text mode ri documentation:
|
Chris@0
|
64
|
Chris@0
|
65 ri Tree::TreeNode
|
Chris@0
|
66
|
Chris@0
|
67 Documentation on the web is available at:
|
Chris@0
|
68
|
Chris@0
|
69 http://rubytree.rubyforge.org/rdoc
|
Chris@0
|
70
|
Chris@0
|
71 == EXAMPLE:
|
Chris@0
|
72
|
Chris@0
|
73 The following code-snippet implements this tree structure:
|
Chris@0
|
74
|
Chris@0
|
75 +------------+
|
Chris@0
|
76 | ROOT |
|
Chris@0
|
77 +-----+------+
|
Chris@0
|
78 +-------------+------------+
|
Chris@0
|
79 | |
|
Chris@0
|
80 +-------+-------+ +-------+-------+
|
Chris@0
|
81 | CHILD 1 | | CHILD 2 |
|
Chris@0
|
82 +-------+-------+ +---------------+
|
Chris@0
|
83 |
|
Chris@0
|
84 |
|
Chris@0
|
85 +-------+-------+
|
Chris@0
|
86 | GRANDCHILD 1 |
|
Chris@0
|
87 +---------------+
|
Chris@0
|
88
|
Chris@0
|
89 require 'tree'
|
Chris@0
|
90
|
Chris@0
|
91 myTreeRoot = Tree::TreeNode.new("ROOT", "Root Content")
|
Chris@0
|
92
|
Chris@0
|
93 myTreeRoot << Tree::TreeNode.new("CHILD1", "Child1 Content") << Tree::TreeNode.new("GRANDCHILD1", "GrandChild1 Content")
|
Chris@0
|
94
|
Chris@0
|
95 myTreeRoot << Tree::TreeNode.new("CHILD2", "Child2 Content")
|
Chris@0
|
96
|
Chris@0
|
97 myTreeRoot.printTree
|
Chris@0
|
98
|
Chris@0
|
99 child1 = myTreeRoot["CHILD1"]
|
Chris@0
|
100
|
Chris@0
|
101 grandChild1 = myTreeRoot["CHILD1"]["GRANDCHILD1"]
|
Chris@0
|
102
|
Chris@0
|
103 siblingsOfChild1Array = child1.siblings
|
Chris@0
|
104
|
Chris@0
|
105 immediateChildrenArray = myTreeRoot.children
|
Chris@0
|
106
|
Chris@0
|
107 # Process all nodes
|
Chris@0
|
108
|
Chris@0
|
109 myTreeRoot.each { |node| node.content.reverse }
|
Chris@0
|
110
|
Chris@0
|
111 myTreeRoot.remove!(child1) # Remove the child
|
Chris@0
|
112
|
Chris@0
|
113 == LICENSE:
|
Chris@0
|
114
|
Chris@0
|
115 Rubytree is licensed under BSD license.
|
Chris@0
|
116
|
Chris@0
|
117 Copyright (c) 2006, 2007 Anupam Sengupta
|
Chris@0
|
118
|
Chris@0
|
119 All rights reserved.
|
Chris@0
|
120
|
Chris@0
|
121 Redistribution and use in source and binary forms, with or without modification,
|
Chris@0
|
122 are permitted provided that the following conditions are met:
|
Chris@0
|
123
|
Chris@0
|
124 - Redistributions of source code must retain the above copyright notice, this
|
Chris@0
|
125 list of conditions and the following disclaimer.
|
Chris@0
|
126
|
Chris@0
|
127 - Redistributions in binary form must reproduce the above copyright notice, this
|
Chris@0
|
128 list of conditions and the following disclaimer in the documentation and/or
|
Chris@0
|
129 other materials provided with the distribution.
|
Chris@0
|
130
|
Chris@0
|
131 - Neither the name of the organization nor the names of its contributors may
|
Chris@0
|
132 be used to endorse or promote products derived from this software without
|
Chris@0
|
133 specific prior written permission.
|
Chris@0
|
134
|
Chris@0
|
135 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
Chris@0
|
136 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
Chris@0
|
137 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
Chris@0
|
138 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
Chris@0
|
139 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
Chris@0
|
140 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
Chris@0
|
141 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
Chris@0
|
142 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
Chris@0
|
143 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
Chris@0
|
144 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
Chris@0
|
145
|
Chris@0
|
146
|
Chris@0
|
147 (Document Revision: $Revision: 1.16 $ by $Author: anupamsg $)
|