Class Tree::TreeNode
In: lib/tree.rb
Parent: Object

TreeNode Class Description

The node class for the tree representation. the nodes are named and have a place-holder for the node data (i.e., the `content’ of the node). The node names are expected to be unique. In addition, the node provides navigation methods to traverse the tree.

The nodes can have any number of child nodes attached to it. Note that while this implementation does not support directed graphs, the class itself makes no restrictions on associating a node‘s CONTENT with multiple parent nodes.

Example

 The following code-snippet implements this tree structure:

                   +------------+
                   |    ROOT    |
                   +-----+------+
           +-------------+------------+
           |                          |
   +-------+-------+          +-------+-------+
   |  CHILD 1      |          |  CHILD 2      |
   +-------+-------+          +---------------+
           |
           |
   +-------+-------+
   | GRANDCHILD 1  |
   +---------------+

require ‘tree‘

myTreeRoot = Tree::TreeNode.new("ROOT", "Root Content")

myTreeRoot << Tree::TreeNode.new("CHILD1", "Child1 Content") << Tree::TreeNode.new("GRANDCHILD1", "GrandChild1 Content")

myTreeRoot << Tree::TreeNode.new("CHILD2", "Child2 Content")

myTreeRoot.printTree

child1 = myTreeRoot["CHILD1"]

grandChild1 = myTreeRoot["CHILD1"]["GRANDCHILD1"]

siblingsOfChild1Array = child1.siblings

immediateChildrenArray = myTreeRoot.children

# Process all nodes

myTreeRoot.each { |node| node.content.reverse }

myTreeRoot.remove!(child1) # Remove the child

Methods

Included Modules

Enumerable

Attributes

content  [R] 
content  [W] 
name  [R] 
parent  [R] 

Public Class methods

Constructor which expects the name of the node

Name of the node is expected to be unique across the tree.

The content can be of any type, and is defaulted to nil.

Public Instance methods

Convenience synonym for TreeNode#add method. This method allows a convenient method to add children hierarchies in the tree.

E.g. root << child << grand_child

Provides a comparision operation for the nodes. Comparision is based on the natural character-set ordering for the node names.

Returns the requested node from the set of immediate children.

If the parameter is numeric, then the in-sequence array of children is accessed (see Tree#children). If the parameter is not numeric, then it is assumed to be the name of the child node to be returned.

Adds the specified child node to the receiver node. The child node‘s parent is set to be the receiver. The child is added as the last child in the current list of children for the receiver node.

Returns breadth of the tree at this node level. A single node has a breadth of 1.

Performs breadth first traversal of the tree rooted at this node. The traversal in a given level is from left to right.

Returns an array of all the immediate children. If a block is given, yields each child node to the block.

Returns depth of the tree from this node. A single leaf node has a depth of 1.

Returns a copy of this node, with the parent and children links removed.

Returns every node (including the receiver node) from the tree to the specified block. The traversal is depth first and from left to right in pre-ordered sequence.

Yields all leaf nodes from this node to the specified block. May yield this node as well if this is a leaf node. Leaf traversal depth first and left to right.

Returns the first child of this node. Will return nil if no children are present.

Returns the first sibling for this node. If this is the root node, returns itself.

Freezes all nodes in the tree

Indicates whether this node has any immediate child nodes.

Indicates whether this node has any associated content.

Returns true if this node is the first sibling.

Returns true if his node is the last sibling

Indicates whether this node is a ‘leaf’ - i.e., one without any children

Returns true if this node is the only child of its parent

Indicates whether this node is a root node. Note that orphaned children will also be reported as root nodes.

Returns the last child of this node. Will return nil if no children are present.

Returns the last sibling for this node. If this node is the root, returns itself.

Convenience synonym for Tree#size

Creates the marshal-dump represention of the tree rooted at this node.

Loads a marshalled dump of the tree and returns the root node of the reconstructed tree. See the Marshal class for additional details.

Returns the next sibling for this node. Will return nil if no subsequent node is present.

Returns an array of ancestors in reversed order (the first element is the immediate parent). Returns nil if this is a root node.

Traverses the tree in a pre-ordered sequence. This is equivalent to TreeNode#each

Returns the previous sibling for this node. Will return nil if no subsequent node is present.

Pretty prints the tree starting with the receiver node.

Removes the specified child node from the receiver node. The removed children nodes are orphaned but available if an alternate reference exists.

Returns the child node.

Removes all children from the receiver node.

Removes this node from its parent. If this is the root node, then does nothing.

Returns the root for this tree. Root‘s root is itself.

Returns an array of siblings for this node. If a block is provided, yields each of the sibling nodes to the block. The root always has nil siblings.

Returns the total number of nodes in this tree, rooted at the receiver node.

Print the string representation of this node.

Protected Instance methods

Creates a dump representation and returns the same as a hash.

Protected method to set the parent node. This method should NOT be invoked by client code.

Protected method which sets this node as a root node.

[Validate]