Chris@909: # binarytree.rb Chris@909: # Chris@909: # $Revision: 1.5 $ by $Author: anupamsg $ Chris@909: # $Name: $ Chris@909: # Chris@909: # = binarytree.rb - Binary Tree implementation Chris@909: # Chris@909: # Provides a generic tree data structure with ability to Chris@909: # store keyed node elements in the tree. The implementation Chris@909: # mixes in the Enumerable module. Chris@909: # Chris@909: # Author:: Anupam Sengupta (anupamsg@gmail.com) Chris@909: # Chris@909: Chris@909: # Copyright (c) 2007 Anupam Sengupta Chris@909: # Chris@909: # All rights reserved. Chris@909: # Chris@909: # Redistribution and use in source and binary forms, with or without modification, Chris@909: # are permitted provided that the following conditions are met: Chris@909: # Chris@909: # - Redistributions of source code must retain the above copyright notice, this Chris@909: # list of conditions and the following disclaimer. Chris@909: # Chris@909: # - Redistributions in binary form must reproduce the above copyright notice, this Chris@909: # list of conditions and the following disclaimer in the documentation and/or Chris@909: # other materials provided with the distribution. Chris@909: # Chris@909: # - Neither the name of the organization nor the names of its contributors may Chris@909: # be used to endorse or promote products derived from this software without Chris@909: # specific prior written permission. Chris@909: # Chris@909: # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" Chris@909: # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE Chris@909: # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE Chris@909: # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR Chris@909: # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES Chris@909: # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; Chris@909: # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON Chris@909: # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT Chris@909: # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS Chris@909: # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Chris@909: # Chris@909: Chris@909: require 'tree' Chris@909: Chris@909: module Tree Chris@909: Chris@909: # Provides a Binary tree implementation. This tree node allows only two child Chris@909: # nodes (left and right childs). It also provides direct access to the left Chris@909: # and right children, including assignment to the same. Chris@909: class BinaryTreeNode < TreeNode Chris@909: Chris@909: # Adds the specified child node to the receiver node. The child node's Chris@909: # parent is set to be the receiver. The child nodes are added in the order Chris@909: # of addition, i.e., the first child added becomes the left node, and the Chris@909: # second child will be the second node. Chris@909: # If only one child is present, then this will be the left child. Chris@909: def add(child) Chris@909: raise "Already has two child nodes" if @children.size == 2 Chris@909: Chris@909: super(child) Chris@909: end Chris@909: Chris@909: # Returns the left child node. Note that Chris@909: # left Child == first Child Chris@909: def leftChild Chris@909: children.first Chris@909: end Chris@909: Chris@909: # Returns the right child node. Note that Chris@909: # right child == last child unless there is only one child. Chris@909: # Returns nil if the right child does not exist. Chris@909: def rightChild Chris@909: children[1] Chris@909: end Chris@909: Chris@909: # Sets the left child. If a previous child existed, it is replaced. Chris@909: def leftChild=(child) Chris@909: @children[0] = child Chris@909: @childrenHash[child.name] = child if child # Assign the name mapping Chris@909: end Chris@909: Chris@909: # Sets the right child. If a previous child existed, it is replaced. Chris@909: def rightChild=(child) Chris@909: @children[1] = child Chris@909: @childrenHash[child.name] = child if child # Assign the name mapping Chris@909: end Chris@909: Chris@909: # Returns true if this is the left child of its parent. Always returns false Chris@909: # if this is the root node. Chris@909: def isLeftChild? Chris@909: return nil if isRoot? Chris@909: self == parent.leftChild Chris@909: end Chris@909: Chris@909: # Returns true if this is the right child of its parent. Always returns false Chris@909: # if this is the root node. Chris@909: def isRightChild? Chris@909: return nil if isRoot? Chris@909: self == parent.rightChild Chris@909: end Chris@909: Chris@909: # Swaps the left and right children with each other Chris@909: def swap_children Chris@909: tempChild = leftChild Chris@909: self.leftChild= rightChild Chris@909: self.rightChild= tempChild Chris@909: end Chris@909: end Chris@909: Chris@909: end Chris@909: Chris@909: # $Log: binarytree.rb,v $ Chris@909: # Revision 1.5 2007/12/18 23:11:29 anupamsg Chris@909: # Minor documentation changes in the binarytree class. Chris@909: # Chris@909: # Revision 1.4 2007/08/30 22:08:58 anupamsg Chris@909: # Added a new swap_children method for Binary Tree. Also added minor Chris@909: # documentation and test updates. Chris@909: # Chris@909: # Revision 1.3 2007/07/21 03:24:25 anupamsg Chris@909: # Minor edits to parameter names. User visible functionality does not change. Chris@909: # Chris@909: # Revision 1.2 2007/07/18 20:15:06 anupamsg Chris@909: # Added two predicate methods in BinaryTreeNode to determine whether a node Chris@909: # is a left or a right node. Chris@909: # Chris@909: # Revision 1.1 2007/07/18 19:33:27 anupamsg Chris@909: # Added a new binary tree implementation. Chris@909: #