Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/orama/src/trees/avl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,12 @@ export class AVLTree<K, V> {
}
} else {
// Node with two children: Get the inorder successor
path.push(current)
let successorParent = current
let successor = current.r

while (successor.l !== null) {
path.push(successor)
successorParent = successor
successor = successor.l
}
Expand All @@ -346,12 +348,9 @@ export class AVLTree<K, V> {
} else {
successorParent.r = successor.r
}

current = successorParent
}

// Update heights and rebalance
path.push(current)
for (let i = path.length - 1; i >= 0; i--) {
const currentNode = path[i]
currentNode.updateHeight()
Expand Down
20 changes: 20 additions & 0 deletions packages/orama/tests/tree.avl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,24 @@ t.test('AVL Tree', async (t) => {

t.strictSame(tree.lessThan(10), new Set(['foo', 'bar', 'quux']))
})

t.test('remove root with 0, 1, and 2 children', async (t) => {
const single = new AVLTree(0, ['a'])
single.remove(0)
t.strictSame(single.greaterThan(-1), new Set())

const oneChild = new AVLTree(0, ['a'])
oneChild.insertMultiple(2, ['b'])
oneChild.remove(0)
t.strictSame(oneChild.greaterThan(-5), new Set(['b']))
t.equal(oneChild.find(0), null)

const twoChildren = new AVLTree(3, ['d0'])
twoChildren.insertMultiple(1, ['d1'])
twoChildren.insertMultiple(1.5, ['d2'])
twoChildren.remove(1)
t.strictSame(twoChildren.greaterThan(-2), new Set(['d0', 'd2']))
t.strictSame(twoChildren.find(1.5), new Set(['d2']))
t.equal(twoChildren.find(1), null)
})
})