annotate vendor/gems/rubytree-0.5.2/lib/tree/.svn/text-base/binarytree.rb.svn-base @ 8:0c83d98252d9 yuya

* Add custom repo prefix and proper auth realm, remove auth cache (seems like an unwise feature), pass DB handle around, various other bits of tidying
author Chris Cannam
date Thu, 12 Aug 2010 15:31:37 +0100
parents 513646585e45
children
rev   line source
Chris@0 1 # binarytree.rb
Chris@0 2 #
Chris@0 3 # $Revision: 1.5 $ by $Author: anupamsg $
Chris@0 4 # $Name: $
Chris@0 5 #
Chris@0 6 # = binarytree.rb - Binary Tree implementation
Chris@0 7 #
Chris@0 8 # Provides a generic tree data structure with ability to
Chris@0 9 # store keyed node elements in the tree. The implementation
Chris@0 10 # mixes in the Enumerable module.
Chris@0 11 #
Chris@0 12 # Author:: Anupam Sengupta (anupamsg@gmail.com)
Chris@0 13 #
Chris@0 14
Chris@0 15 # Copyright (c) 2007 Anupam Sengupta
Chris@0 16 #
Chris@0 17 # All rights reserved.
Chris@0 18 #
Chris@0 19 # Redistribution and use in source and binary forms, with or without modification,
Chris@0 20 # are permitted provided that the following conditions are met:
Chris@0 21 #
Chris@0 22 # - Redistributions of source code must retain the above copyright notice, this
Chris@0 23 # list of conditions and the following disclaimer.
Chris@0 24 #
Chris@0 25 # - Redistributions in binary form must reproduce the above copyright notice, this
Chris@0 26 # list of conditions and the following disclaimer in the documentation and/or
Chris@0 27 # other materials provided with the distribution.
Chris@0 28 #
Chris@0 29 # - Neither the name of the organization nor the names of its contributors may
Chris@0 30 # be used to endorse or promote products derived from this software without
Chris@0 31 # specific prior written permission.
Chris@0 32 #
Chris@0 33 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Chris@0 34 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Chris@0 35 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Chris@0 36 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
Chris@0 37 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Chris@0 38 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Chris@0 39 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
Chris@0 40 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Chris@0 41 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Chris@0 42 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@0 43 #
Chris@0 44
Chris@0 45 require 'tree'
Chris@0 46
Chris@0 47 module Tree
Chris@0 48
Chris@0 49 # Provides a Binary tree implementation. This tree node allows only two child
Chris@0 50 # nodes (left and right childs). It also provides direct access to the left
Chris@0 51 # and right children, including assignment to the same.
Chris@0 52 class BinaryTreeNode < TreeNode
Chris@0 53
Chris@0 54 # Adds the specified child node to the receiver node. The child node's
Chris@0 55 # parent is set to be the receiver. The child nodes are added in the order
Chris@0 56 # of addition, i.e., the first child added becomes the left node, and the
Chris@0 57 # second child will be the second node.
Chris@0 58 # If only one child is present, then this will be the left child.
Chris@0 59 def add(child)
Chris@0 60 raise "Already has two child nodes" if @children.size == 2
Chris@0 61
Chris@0 62 super(child)
Chris@0 63 end
Chris@0 64
Chris@0 65 # Returns the left child node. Note that
Chris@0 66 # left Child == first Child
Chris@0 67 def leftChild
Chris@0 68 children.first
Chris@0 69 end
Chris@0 70
Chris@0 71 # Returns the right child node. Note that
Chris@0 72 # right child == last child unless there is only one child.
Chris@0 73 # Returns nil if the right child does not exist.
Chris@0 74 def rightChild
Chris@0 75 children[1]
Chris@0 76 end
Chris@0 77
Chris@0 78 # Sets the left child. If a previous child existed, it is replaced.
Chris@0 79 def leftChild=(child)
Chris@0 80 @children[0] = child
Chris@0 81 @childrenHash[child.name] = child if child # Assign the name mapping
Chris@0 82 end
Chris@0 83
Chris@0 84 # Sets the right child. If a previous child existed, it is replaced.
Chris@0 85 def rightChild=(child)
Chris@0 86 @children[1] = child
Chris@0 87 @childrenHash[child.name] = child if child # Assign the name mapping
Chris@0 88 end
Chris@0 89
Chris@0 90 # Returns true if this is the left child of its parent. Always returns false
Chris@0 91 # if this is the root node.
Chris@0 92 def isLeftChild?
Chris@0 93 return nil if isRoot?
Chris@0 94 self == parent.leftChild
Chris@0 95 end
Chris@0 96
Chris@0 97 # Returns true if this is the right child of its parent. Always returns false
Chris@0 98 # if this is the root node.
Chris@0 99 def isRightChild?
Chris@0 100 return nil if isRoot?
Chris@0 101 self == parent.rightChild
Chris@0 102 end
Chris@0 103
Chris@0 104 # Swaps the left and right children with each other
Chris@0 105 def swap_children
Chris@0 106 tempChild = leftChild
Chris@0 107 self.leftChild= rightChild
Chris@0 108 self.rightChild= tempChild
Chris@0 109 end
Chris@0 110 end
Chris@0 111
Chris@0 112 end
Chris@0 113
Chris@0 114 # $Log: binarytree.rb,v $
Chris@0 115 # Revision 1.5 2007/12/18 23:11:29 anupamsg
Chris@0 116 # Minor documentation changes in the binarytree class.
Chris@0 117 #
Chris@0 118 # Revision 1.4 2007/08/30 22:08:58 anupamsg
Chris@0 119 # Added a new swap_children method for Binary Tree. Also added minor
Chris@0 120 # documentation and test updates.
Chris@0 121 #
Chris@0 122 # Revision 1.3 2007/07/21 03:24:25 anupamsg
Chris@0 123 # Minor edits to parameter names. User visible functionality does not change.
Chris@0 124 #
Chris@0 125 # Revision 1.2 2007/07/18 20:15:06 anupamsg
Chris@0 126 # Added two predicate methods in BinaryTreeNode to determine whether a node
Chris@0 127 # is a left or a right node.
Chris@0 128 #
Chris@0 129 # Revision 1.1 2007/07/18 19:33:27 anupamsg
Chris@0 130 # Added a new binary tree implementation.
Chris@0 131 #