Skip to content

AvlTree remove - value not updating #31

@KD0NKS

Description

@KD0NKS

This is related to #29 only for AvlTree. When removing a node, the key is updated, but the value is not. The example below can be run in codepen.

import { AvlTree } from "https://cdn.skypack.dev/@datastructures-js/binary-search-tree@3.1.7";
import { assert, expect } from "https://cdn.skypack.dev/chai@4.3.4";

const tree = new AvlTree()
function getAll(isFullTree) {
  const res = []

  tree.traverseInOrder(function(node) {
    if(isFullTree)
      res.push(node)
    else
      res.push(node.getValue())
  })

  return res
}

const doc1 = { a: 5, tf: 'rock' }
const doc2 = { a: 8, tf: 'paper' }
const doc3 = { a: 2, tf: 'scissor' }
const doc4 = { a: 23, tf: 'paper' }

tree.insert(doc1['tf'], doc1)
tree.insert(doc2['tf'], doc2)
tree.insert(doc3['tf'], doc3)
tree.insert(doc4['tf'], doc4)

assert.equal(3, tree.count())
console.log(getAll(true))
assert.equal(doc1, tree.find('rock').getValue())
assert.deepEqual({ a: 5, tf: 'rock' }, tree.find('rock').getValue())
assert.equal(doc3, tree.find('scissor').getValue())
assert.deepEqual({ a: 2, tf: 'scissor' }, tree.find('scissor').getValue())
assert.equal(doc4, tree.find('paper').getValue())
assert.deepEqual({ a: 2, tf: 'scissor' }, tree.find('scissor').getValue())

// Remove rock - doc1
tree.remove('rock')

console.log(getAll(true))
assert.equal(false, tree.has('rock'))
assert.equal(doc4, tree.find('paper').getValue())

assert.equal(true, tree.has('scissor'))
assert.equal('scissor', tree.find('scissor').getKey())

// scissor now has the value of rock
assert.equal(tree.find('scissor').getValue(), doc1)   // doc 1 is rock - should throw error
assert.equal(tree.find('scissor').getValue(), doc3)   // throws an error
assert.deepEqual({ a: 2, tf: 'scissor' }, tree.find('scissor').getValue())  // throws an error

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions