Skip to content

Commit 8b52f3f

Browse files
committed
Add non-breaking API aliases for consistency
Introduce lookup/query/pop/search aliases across selected tree types to improve cross-family API consistency without changing existing behavior, and add regression tests for alias contracts.
1 parent 863f1d3 commit 8b52f3f

9 files changed

Lines changed: 141 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ Changes section to scan for breaking or behavioral changes.
88

99
### 3.0.0pre / 2026-02-10
1010

11+
* Add non-breaking API aliases for consistency:
12+
`Tree::AATree#lookup`, `Tree::BTree#lookup`,
13+
`Tree::FenwickTree#query`, `Tree::SegmentTree#query`,
14+
`Tree::BinaryHeapNode#pop`, `Tree::BinaryMaxHeapNode#pop`,
15+
and `Tree::TrieNode#search`.
16+
* Add alias-focused regression tests to lock these compatibility contracts.
1117
* Make `Tree::TrieNode#<<` use trie word-insert semantics (`insert(word)`)
1218
instead of generic child-node attachment semantics.
1319
* Add `<<` insertion shorthand to `Tree::AATree` and `Tree::BTree` for

lib/tree/aatree.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ def search(key)
115115
nil
116116
end
117117

118+
# Alias for {#search} to support lookup-oriented naming.
119+
#
120+
# @param [Object] key The key to search for.
121+
# @return [Object, nil] The matching value, or +nil+.
122+
def lookup(key)
123+
search(key)
124+
end
125+
118126
# Delete a key from the AA tree.
119127
#
120128
# @param [Object] key The key to delete.

lib/tree/binaryheap.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ def extract
125125
removed
126126
end
127127

128+
# Alias for {#extract} using priority-queue naming.
129+
#
130+
# @return [Object, nil] The removed minimum value, or +nil+ if empty.
131+
def pop
132+
extract
133+
end
134+
128135
# Searches for a node matching the specified key (content).
129136
#
130137
# @param [Object] key The search key (node content).

lib/tree/binarymaxheap.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ def extract
125125
removed
126126
end
127127

128+
# Alias for {#extract} using priority-queue naming.
129+
#
130+
# @return [Object, nil] The removed maximum value, or +nil+ if empty.
131+
def pop
132+
extract
133+
end
134+
128135
# Searches for a node matching the specified key (content).
129136
#
130137
# @param [Object] key The search key (node content).

lib/tree/btree.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ def search(key)
176176
entry&.value
177177
end
178178

179+
# Alias for {#search} to support lookup-oriented naming.
180+
#
181+
# @param [Object] key The key to search for.
182+
# @return [Object, nil] The matching value, or +nil+.
183+
def lookup(key)
184+
search(key)
185+
end
186+
179187
# Delete a key from the B-tree.
180188
#
181189
# @param [Object] key The key to delete.

lib/tree/fenwicktree.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ def range_sum(left, right)
169169
sum(right) - sum(left - 1)
170170
end
171171

172+
# Alias for {#range_sum} with query-oriented naming.
173+
#
174+
# @param [Integer] left The starting index.
175+
# @param [Integer] right The ending index.
176+
# @return [Numeric] Sum in the specified range.
177+
def query(left, right)
178+
range_sum(left, right)
179+
end
180+
172181
# Read the value at the specified index.
173182
#
174183
# @param [Integer] index Zero-based index.

lib/tree/segmenttree.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@ def range_sum(left, right)
142142
query_sum(1, 0, @size - 1, left, right)
143143
end
144144

145+
# Alias for {#range_sum} with query-oriented naming.
146+
#
147+
# @param [Integer] left The starting index.
148+
# @param [Integer] right The ending index.
149+
# @return [Numeric] Sum in the specified range.
150+
def query(left, right)
151+
range_sum(left, right)
152+
end
153+
145154
# Read the value at the specified index.
146155
#
147156
# @param [Integer] index Zero-based index.

lib/tree/trie.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ def include?(word)
122122
node&.terminal? == true
123123
end
124124

125+
# Alias for {#include?} to support search-oriented naming.
126+
alias search include?
127+
125128
# Returns +true+ if the trie includes the specified prefix.
126129
#
127130
# @param [String] prefix The prefix to look up.

test/test_api_alias_consistency.rb

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# test_api_alias_consistency.rb - This file is part of the RubyTree package.
2+
#
3+
# Copyright (c) 2026 Anupam Sengupta
4+
#
5+
# All rights reserved.
6+
#
7+
# Redistribution and use in source and binary forms, with or without modification,
8+
# are permitted provided that the following conditions are met:
9+
#
10+
# - Redistributions of source code must retain the above copyright notice, this
11+
# list of conditions and the following disclaimer.
12+
#
13+
# - Redistributions in binary form must reproduce the above copyright notice, this
14+
# list of conditions and the following disclaimer in the documentation and/or
15+
# other materials provided with the distribution.
16+
#
17+
# - Neither the name of the organization nor the names of its contributors may
18+
# be used to endorse or promote products derived from this software without
19+
# specific prior written permission.
20+
#
21+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
25+
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28+
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
#
32+
# frozen_string_literal: true
33+
34+
require 'test/unit'
35+
require_relative '../lib/tree/aatree'
36+
require_relative '../lib/tree/binaryheap'
37+
require_relative '../lib/tree/binarymaxheap'
38+
require_relative '../lib/tree/btree'
39+
require_relative '../lib/tree/fenwicktree'
40+
require_relative '../lib/tree/segmenttree'
41+
require_relative '../lib/tree/trie'
42+
43+
module TestTree
44+
# Alias-consistency tests for Phase 2 API harmonization.
45+
class TestApiAliasConsistency < Test::Unit::TestCase
46+
def test_key_value_lookup_alias
47+
aa = Tree::AATree.new([[10, 'a']])
48+
bt = Tree::BTree.new(2, [{ key: 10, value: 'a' }])
49+
50+
assert_equal(aa.search(10), aa.lookup(10))
51+
assert_equal(bt.search(10), bt.lookup(10))
52+
end
53+
54+
def test_aggregate_query_alias
55+
fenwick = Tree::FenwickTree.new(4, [1, 2, 3, 4])
56+
segment = Tree::SegmentTree.new(4, [1, 2, 3, 4])
57+
58+
assert_equal(fenwick.range_sum(1, 3), fenwick.query(1, 3))
59+
assert_equal(segment.range_sum(1, 3), segment.query(1, 3))
60+
end
61+
62+
def test_heap_pop_alias
63+
min_heap = Tree::BinaryHeapNode.new('root', 3)
64+
min_heap.insert('a', 1)
65+
min_heap.insert('b', 2)
66+
max_heap = Tree::BinaryMaxHeapNode.new('root', 3)
67+
max_heap.insert('a', 9)
68+
max_heap.insert('b', 8)
69+
70+
assert_equal(1, min_heap.pop)
71+
assert_equal(2, min_heap.extract)
72+
assert_equal(9, max_heap.pop)
73+
assert_equal(8, max_heap.extract)
74+
end
75+
76+
def test_trie_search_alias
77+
trie = Tree::TrieNode.new('')
78+
trie.insert('cat')
79+
80+
assert_equal(trie.include?('cat'), trie.search('cat'))
81+
assert_equal(trie.include?('dog'), trie.search('dog'))
82+
end
83+
end
84+
end

0 commit comments

Comments
 (0)