|
| 1 | +# binary_search_node_accessors.rb - This file is part of the RubyTree package. |
| 2 | +# |
| 3 | +# = binary_search_node_accessors.rb - Shared accessors for ordered binary nodes. |
| 4 | +# |
| 5 | +# Provides shared key/min/max helpers for ordered binary node types that use |
| 6 | +# +content+ as the sortable key. |
| 7 | +# |
| 8 | +# Author:: Anupam Sengupta (anupamsg@gmail.com) |
| 9 | +# |
| 10 | + |
| 11 | +# Copyright (c) 2026 Anupam Sengupta. All rights reserved. |
| 12 | +# |
| 13 | +# Redistribution and use in source and binary forms, with or without |
| 14 | +# modification, are permitted provided that the following conditions are met: |
| 15 | +# |
| 16 | +# - Redistributions of source code must retain the above copyright notice, this |
| 17 | +# list of conditions and the following disclaimer. |
| 18 | +# |
| 19 | +# - Redistributions in binary form must reproduce the above copyright notice, |
| 20 | +# this list of conditions and the following disclaimer in the documentation |
| 21 | +# and/or other materials provided with the distribution. |
| 22 | +# |
| 23 | +# - Neither the name of the organization nor the names of its contributors may |
| 24 | +# be used to endorse or promote products derived from this software without |
| 25 | +# specific prior written permission. |
| 26 | +# |
| 27 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 28 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 29 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 30 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 31 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 32 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 33 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 34 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 35 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 36 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 37 | +# |
| 38 | +# frozen_string_literal: true |
| 39 | + |
| 40 | +module Tree |
| 41 | + module Utils |
| 42 | + # Shared key/min/max helpers for ordered binary node variants. |
| 43 | + module BinarySearchNodeAccessors |
| 44 | + # Returns the minimum node in the subtree rooted at this node. |
| 45 | + # |
| 46 | + # @return [Tree::BinaryTreeNode] The minimum node in the subtree. |
| 47 | + def min_node |
| 48 | + current = self |
| 49 | + current = current.left_child while current.left_child |
| 50 | + current |
| 51 | + end |
| 52 | + |
| 53 | + # Returns the maximum node in the subtree rooted at this node. |
| 54 | + # |
| 55 | + # @return [Tree::BinaryTreeNode] The maximum node in the subtree. |
| 56 | + def max_node |
| 57 | + current = self |
| 58 | + current = current.right_child while current.right_child |
| 59 | + current |
| 60 | + end |
| 61 | + |
| 62 | + # Returns the ordered key for this node (the content). |
| 63 | + # |
| 64 | + # @return [Object] The node content used as the key. |
| 65 | + def key |
| 66 | + validate_key!(@content) |
| 67 | + @content |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | +end |
0 commit comments