Skip to content

Commit 9190b1d

Browse files
authored
Merge pull request #36 from datastructures-js/development
lowerBound & upperBound
2 parents 4e50d08 + 4398584 commit 9190b1d

File tree

5 files changed

+121
-1
lines changed

5 files changed

+121
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
9+
## [4.1.0] - 2021-04-21
10+
11+
### Added
12+
- `.lowerBound(k)` to find the node with biggest key less or equal a value k.
13+
- `.upperBound(k)` to find the node with smallest key bigger than a value k.
14+
815
## [4.0.1] - 2021-04-15
916
### Fixed
1017
- exported AvlTreeNode path.

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript
3232
* [.find(key)](#findkey)
3333
* [.min()](#min)
3434
* [.max()](#max)
35+
* [.lowerBound(k)](#lowerboundk)
36+
* [.upperBound(k)](#upperboundk)
3537
* [.root()](#root)
3638
* [.count()](#count)
3739
* [.traverseInOrder(cb)](#traverseinordercb)
@@ -202,6 +204,49 @@ const max = bst.max();
202204
console.log(max.getKey()); // 90
203205
console.log(max.getValue()); // v4
204206
```
207+
208+
### .lowerBound(k)
209+
finds the node with the biggest key less or equal a given value k.
210+
211+
<table>
212+
<tr>
213+
<th align="center">params</th>
214+
<th align="center">return</th>
215+
<th align="center">runtime</th>
216+
</tr>
217+
<tr>
218+
<td>k: number | string</td>
219+
<td align="center"><a href="#binarysearchtreenode">BinarySearchTreeNode</a> | <a href="#avltreenode">AvlTreeNode</a></td>
220+
<td align="center">O(log(n))</td>
221+
</tr>
222+
</table>
223+
224+
```js
225+
console.log(bst.lowerBound(60).getKey()); // 50
226+
console.log(bst.lowerBound(10)); // null
227+
```
228+
229+
### .upperBound(k)
230+
finds the node with the smallest key bigger than a given value k.
231+
232+
<table>
233+
<tr>
234+
<th align="center">params</th>
235+
<th align="center">return</th>
236+
<th align="center">runtime</th>
237+
</tr>
238+
<tr>
239+
<td>k: number | string</td>
240+
<td align="center"><a href="#binarysearchtreenode">BinarySearchTreeNode</a> | <a href="#avltreenode">AvlTreeNode</a></td>
241+
<td align="center">O(log(n))</td>
242+
</tr>
243+
</table>
244+
245+
```js
246+
console.log(bst.upperBound(75).getKey()); // 80
247+
console.log(bst.upperBound(110)); // null
248+
```
249+
205250
### .root()
206251
returns the root node of the tree.
207252

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@datastructures-js/binary-search-tree",
3-
"version": "4.0.1",
3+
"version": "4.1.0",
44
"description": "binary search tree & avl tree (self balancing tree) implementation in javascript",
55
"main": "index.js",
66
"scripts": {

src/binarySearchTree.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,54 @@ class BinarySearchTree {
142142
return current;
143143
}
144144

145+
/**
146+
* Returns the node with the biggest key less or equal to k
147+
* @public
148+
* @param {number|string} k
149+
* @return {BinarySearchTreeNode|null}
150+
*/
151+
lowerBound(k, current = this._root) {
152+
if (current === null) {
153+
return null;
154+
}
155+
156+
if (current.getKey() === k) {
157+
return current;
158+
}
159+
160+
if (current.getKey() > k) {
161+
return this.lowerBound(k, current.getLeft());
162+
}
163+
164+
if (current.hasRight() && current.getRight().getKey() <= k) {
165+
return this.lowerBound(k, current.getRight());
166+
}
167+
168+
return current;
169+
}
170+
171+
/**
172+
* Returns the node with the smallest key bigger than k
173+
* @public
174+
* @param {number|string} k
175+
* @return {BinarySearchTreeNode|null}
176+
*/
177+
upperBound(k, current = this._root) {
178+
if (current === null) {
179+
return null;
180+
}
181+
182+
if (current.getKey() <= k) {
183+
return this.upperBound(k, current.getRight());
184+
}
185+
186+
if (current.hasLeft() && current.getLeft().getKey() > k) {
187+
return this.upperBound(k, current.getLeft());
188+
}
189+
190+
return current;
191+
}
192+
145193
/**
146194
* Returns the root node
147195
* @public

test/binarySearchTree.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,26 @@ describe('BinarySearchTree tests', () => {
8080
});
8181
});
8282

83+
describe('.lowerBound(k)', () => {
84+
it('gets the node with biggest key less or equal k', () => {
85+
expect(bst.lowerBound(60).getKey()).to.equal(50);
86+
});
87+
88+
it('returns null when k is less than all tree keys', () => {
89+
expect(bst.lowerBound(10)).to.equal(null);
90+
});
91+
});
92+
93+
describe('.upperBound(k)', () => {
94+
it('gets the node with smallest key bigger than k', () => {
95+
expect(bst.upperBound(75).getKey()).to.equal(80);
96+
});
97+
98+
it('returns null when k is bigger than all tree keys', () => {
99+
expect(bst.upperBound(110)).to.equal(null);
100+
});
101+
});
102+
83103
describe('.traverseInOrder(cb)', () => {
84104
it('traverse the tree in-order', () => {
85105
const keys = [];

0 commit comments

Comments
 (0)