Skip to content
This repository

A General Purpose Tree Data Structure for Ruby

No longer require globally unique node names.

This fixes issue#24. The node names need to be unique only among sibling nodes.

The node comparison is still based on node names in this release.
latest commit 0432c2dd55
Anupam Sengupta authored February 01, 2014
Octocat-spinner-32 .bundle Removed a spurious file from the gemspec. February 26, 2013
Octocat-spinner-32 examples Added example code to the Gem. December 28, 2013
Octocat-spinner-32 gemfiles Removed the version requirement for rubysl. January 03, 2014
Octocat-spinner-32 lib No longer require globally unique node names. February 01, 2014
Octocat-spinner-32 test No longer require globally unique node names. February 01, 2014
Octocat-spinner-32 .gemtest Added support for gem-test. August 21, 2012
Octocat-spinner-32 .gitignore Minor cleanup of .gitignore. January 11, 2014
Octocat-spinner-32 .travis.yml Removed the restriction on the rubygems version. January 12, 2014
Octocat-spinner-32 API-CHANGES.rdoc No longer require globally unique node names. February 01, 2014
Octocat-spinner-32 Gemfile Added a platform dependency for coveralls. January 03, 2014
Octocat-spinner-32 Gemfile.lock No longer require globally unique node names. February 01, 2014
Octocat-spinner-32 History.rdoc No longer require globally unique node names. February 01, 2014
Octocat-spinner-32 Migrated the README and LICENSE to markdown format. January 02, 2014
Octocat-spinner-32 Documentation updates. January 02, 2014
Octocat-spinner-32 Rakefile Modified the default Rake task to invoke coverage conditionally. January 03, 2014
Octocat-spinner-32 Removed the restriction on the rubygems version. January 12, 2014
Octocat-spinner-32 rubytree.gemspec No longer require globally unique node names. February 01, 2014
Octocat-spinner-32 setup.rb Deleted the subdirectory for rubytree and moved the source code to tr… January 01, 2010


Gem Version Travis Build Status Dependency Status Code Climate Coverage Status alpha


RubyTree is a pure Ruby implementation of the generic tree data structure. It provides a node-based model to store named nodes in the tree, and provides simple APIs to access, modify and traverse the structure.

The implementation is node-centric, where individual nodes in the tree are the primary structural elements. All common tree-traversal methods (pre-order, post-order, and breadth-first) are supported.

The library mixes in the Enumerable and Comparable modules to allow access to the tree as a standard collection (iteration, comparison, etc.).

A Binary tree is also provided, which provides the in-order traversal in addition to the other methods.

RubyTree supports importing from, and exporting to JSON, and also supports the Ruby's standard object marshaling.

This is a BSD licensed open source project, and is hosted at, and is available as a standard gem from

The home page for RubyTree is at


See History for the detailed Changelog.

See API-CHANGES for the detailed description of API level changes.


This is a basic usage example of the library to create and manipulate a tree. See the API documentation for more details.

#!/usr/bin/env ruby
# example_basic.rb:: Basic usage of the tree library.
# Author:  Anupam Sengupta
# Time-stamp: <2013-12-28 12:14:20 anupam>
# Copyright (C) 2013 Anupam Sengupta <>
# The following example implements this tree structure:
#                    +------------+
#                    |    ROOT    |
#                    +-----+------+
#            +-------------+------------+
#            |                          |
#    +-------+-------+          +-------+-------+
#    |  CHILD 1      |          |  CHILD 2      |
#    +-------+-------+          +---------------+
#            |
#            |
#    +-------+-------+
#    | GRANDCHILD 1  |
#    +---------------+

# ..... Example starts.
require 'tree'                 # Load the library

# ..... Create the root node first.  Note that every node has a name and an optional content payload.
root_node ="ROOT", "Root Content")

# ..... Now insert the child nodes.  Note that you can "chain" the child insertions for a given path to any depth.
root_node <<"CHILD1", "Child1 Content") <<"GRANDCHILD1", "GrandChild1 Content")
root_node <<"CHILD2", "Child2 Content")

# ..... Lets print the representation to stdout.  This is primarily used for debugging purposes.

# ..... Lets directly access children and grandchildren of the root.  The can be "chained" for a given path to any depth.
child1       = root_node["CHILD1"]
grand_child1 = root_node["CHILD1"]["GRANDCHILD1"]

# ..... Now lets retrieve siblings of the current node as an array.
siblings_of_child1 = child1.siblings

# ..... Lets retrieve immediate children of the root node as an array.
children_of_root = root_node.children

# ..... This is a depth-first and L-to-R pre-ordered traversal.
root_node.each { |node| node.content.reverse }

# ..... Lets remove a child node from the root node.

This example can also be found at examples/example_basic.rb.


  • Ruby 1.8.x, 1.9.x, 2.0.x, or 2.1.0x.

  • Run-time Dependencies:

  • Development dependencies (not required for installing the gem):

    • Bundler for creating the stable build environment
    • Rake for building the package
    • Yard for the documentation


To install the gem, run this command from a terminal/shell:

$ gem install rubytree

This should install the gem file for RubyTree. Note that you might need to have super-user privileges (root/sudo) to successfully install the gem.

Installing from the tgz/zip file

RubyTree also be downloaded as a tar/zip file (or as a gem) from:

The file-name is one of:

  • rubytree-<VERSION>.gem - The rubygem

  • rubytree-<VERSION>.tgz - GZipped source files

  • rubytree-<VERSION>.zip - Zipped source files

Download the appropriate file-type for your system.

Extract the archive file (tgz or zip) and run the following command from the top-level source directory:

$ ruby ./setup.rb

You may need administrator/super-user privileges to complete the setup using this method. Note that the source code contains the Rakefile for building using Rake, which might an easier mechanism for building and installing the gem.


The primary class RubyTree is {Tree::TreeNode}. See the class documentation for an example of using the library.

If the ri documentation was generated during install, you can use this command at the terminal to view the text mode ri documentation:

$ ri Tree::TreeNode

Documentation for the latest released version is available at:

Documentation for the latest git HEAD is available at:

Note that the documentation is formatted using Yard.


This section is only for modifying RubyTree itself. It is not required for using the library!

You can download the latest released source code as a tar or zip file, as mentioned above in the installation section.

Alternatively, you can checkout the latest commit/revision from the Version Control System. Note that RubyTree's primary SCM is git and is hosted on

Using the git Repository

The git repository is available at

For cloning the git repository, use one of the following commands:

$ git clone git://


$ git clone

Setting up the Development Environment

RubyTree uses Bundler to manage its dependencies. This allows for a simplified dependency management, for both run-time as well as during build.

After checking out the source, run:

$ gem install bundler
$ bundle install
$ rake test
$ rake doc:yard
$ rake gem:package

These steps will install any missing dependencies, run the tests/specs, generate the documentation, and finally generate the gem file.

Note that the documentation uses Yard, which will be downloaded and installed automatically by Bundler.


A big thanks to the following contributors for helping improve RubyTree:

  1. Dirk Breuer for contributing the JSON conversion code.
  2. Vincenzo Farruggia for contributing the (sub)tree cloning code.
  3. Eric Cline for the Rails JSON encoding fix.
  4. Darren Oakley for the tree merge methods.
  5. Youssef Rebahi-Gilbert for the code to check duplicate node names in the tree (globally unique names).
  6. Paul de Courcel for adding the postordered_each method.


RubyTree is licensed under the terms of the BSD license. See for details.

{} __ _ _ /_ _| | _ | | _ __ ___ ___ / \// | | | '_ | | | | | '/ _ \/ _ \ / _ \ || | |) | || | || | | / __/ \/ _/\,|./ \, |__|| __|___| |___/

Something went wrong with that request. Please try again.