Skip to content

Commit c24cb81

Browse files
authored
performance: improves avl tree traversal and fixes #895 (#973)
1 parent c0755a4 commit c24cb81

File tree

1 file changed

+12
-7
lines changed
  • packages/orama/src/trees

1 file changed

+12
-7
lines changed

packages/orama/src/trees/avl.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-disable no-extra-semi */
22
/* eslint-disable @typescript-eslint/no-this-alias */
33
import { Nullable } from '../types.js'
4-
import { setUnion } from '../utils.js'
54

65
export class AVLNode<K, V> {
76
public k: K
@@ -374,7 +373,7 @@ export class AVLTree<K, V> {
374373
}
375374

376375
public rangeSearch(min: K, max: K): Set<V> {
377-
let result: Set<V> = new Set()
376+
const result: Set<V> = new Set()
378377
const stack: Array<AVLNode<K, V>> = []
379378
let current = this.root
380379

@@ -385,7 +384,9 @@ export class AVLTree<K, V> {
385384
}
386385
current = stack.pop()!
387386
if (current.k >= min && current.k <= max) {
388-
result = setUnion(result, current.v)
387+
for (const value of current.v) {
388+
result.add(value)
389+
}
389390
}
390391
if (current.k > max) {
391392
break
@@ -397,7 +398,7 @@ export class AVLTree<K, V> {
397398
}
398399

399400
public greaterThan(key: K, inclusive = false): Set<V> {
400-
let result: Set<V> = new Set()
401+
const result: Set<V> = new Set()
401402
const stack: Array<AVLNode<K, V>> = []
402403
let current = this.root
403404

@@ -408,7 +409,9 @@ export class AVLTree<K, V> {
408409
}
409410
current = stack.pop()!
410411
if ((inclusive && current.k >= key) || (!inclusive && current.k > key)) {
411-
result = setUnion(result, current.v)
412+
for (const value of current.v) {
413+
result.add(value)
414+
}
412415
} else if (current.k <= key) {
413416
break // Since we're traversing in descending order, we can break early
414417
}
@@ -419,7 +422,7 @@ export class AVLTree<K, V> {
419422
}
420423

421424
public lessThan(key: K, inclusive = false): Set<V> {
422-
let result: Set<V> = new Set()
425+
const result: Set<V> = new Set()
423426
const stack: Array<AVLNode<K, V>> = []
424427
let current = this.root
425428

@@ -430,7 +433,9 @@ export class AVLTree<K, V> {
430433
}
431434
current = stack.pop()!
432435
if ((inclusive && current.k <= key) || (!inclusive && current.k < key)) {
433-
result = setUnion(result, current.v)
436+
for (const value of current.v) {
437+
result.add(value)
438+
}
434439
} else if (current.k > key) {
435440
break // Since we're traversing in ascending order, we can break early
436441
}

0 commit comments

Comments
 (0)