To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / vendor / gems / rubytree-0.5.2 / lib / tree / binarytree.rb @ 442:753f1380d6bc
History | View | Annotate | Download (4.5 KB)
| 1 |
# binarytree.rb
|
|---|---|
| 2 |
#
|
| 3 |
# $Revision: 1.5 $ 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 |
# Returns nil if the right child does not exist.
|
| 74 |
def rightChild |
| 75 |
children[1]
|
| 76 |
end
|
| 77 |
|
| 78 |
# Sets the left child. If a previous child existed, it is replaced.
|
| 79 |
def leftChild=(child) |
| 80 |
@children[0] = child |
| 81 |
@childrenHash[child.name] = child if child # Assign the name mapping |
| 82 |
end
|
| 83 |
|
| 84 |
# Sets the right child. If a previous child existed, it is replaced.
|
| 85 |
def rightChild=(child) |
| 86 |
@children[1] = child |
| 87 |
@childrenHash[child.name] = child if child # Assign the name mapping |
| 88 |
end
|
| 89 |
|
| 90 |
# Returns true if this is the left child of its parent. Always returns false
|
| 91 |
# if this is the root node.
|
| 92 |
def isLeftChild? |
| 93 |
return nil if isRoot? |
| 94 |
self == parent.leftChild
|
| 95 |
end
|
| 96 |
|
| 97 |
# Returns true if this is the right child of its parent. Always returns false
|
| 98 |
# if this is the root node.
|
| 99 |
def isRightChild? |
| 100 |
return nil if isRoot? |
| 101 |
self == parent.rightChild
|
| 102 |
end
|
| 103 |
|
| 104 |
# Swaps the left and right children with each other
|
| 105 |
def swap_children |
| 106 |
tempChild = leftChild |
| 107 |
self.leftChild= rightChild
|
| 108 |
self.rightChild= tempChild
|
| 109 |
end
|
| 110 |
end
|
| 111 |
|
| 112 |
end
|
| 113 |
|
| 114 |
# $Log: binarytree.rb,v $
|
| 115 |
# Revision 1.5 2007/12/18 23:11:29 anupamsg
|
| 116 |
# Minor documentation changes in the binarytree class.
|
| 117 |
#
|
| 118 |
# Revision 1.4 2007/08/30 22:08:58 anupamsg
|
| 119 |
# Added a new swap_children method for Binary Tree. Also added minor
|
| 120 |
# documentation and test updates.
|
| 121 |
#
|
| 122 |
# Revision 1.3 2007/07/21 03:24:25 anupamsg
|
| 123 |
# Minor edits to parameter names. User visible functionality does not change.
|
| 124 |
#
|
| 125 |
# Revision 1.2 2007/07/18 20:15:06 anupamsg
|
| 126 |
# Added two predicate methods in BinaryTreeNode to determine whether a node
|
| 127 |
# is a left or a right node.
|
| 128 |
#
|
| 129 |
# Revision 1.1 2007/07/18 19:33:27 anupamsg
|
| 130 |
# Added a new binary tree implementation.
|
| 131 |
#
|