Skip to content

Commit

Permalink
RBT(fix): update node values when swapping contents; fixes frptools#44
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Jul 12, 2017
1 parent 84d7bdb commit 98e08dc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/red-black-tree/src/internals/ops.ts
Expand Up @@ -136,10 +136,12 @@ export function rotateRightLeft<K, V>(upper: PathNode<K, V>, grandParent: Node<K
export function swapNodeContents<K, V>(upper: Node<K, V>, lower: Node<K, V> /* ## DEV [[ */, tree: RedBlackTreeStructure<K, V> /* ]] ## */): void {
log(`[swapNodeContents] upper: ${keyOf(upper)}, lower: ${keyOf(lower)}`); // ## DEV ##
var key = upper.key;
var value = upper.value;
upper.key = lower.key;
upper.value = lower.value;

lower.key = key;
lower.value = value;
(<any>upper).__flag = 'swap-key'; // ## DEV ##
(<any>lower).__flag = 'swap-key'; // ## DEV ##
log(tree, false, `swap node contents; ${upper.key} <==> ${lower.key}`); // ## DEV ##
Expand Down
5 changes: 5 additions & 0 deletions packages/red-black-tree/tests/functions/remove.ts
Expand Up @@ -85,6 +85,11 @@ suite('[RedBlackTree]', () => {
assert.isTrue(isNone(tree2._root._right));
});

test('should successfully remove the root from a balanced three-node tree', () => {
const tree = remove(2, set(2, 'two', set(3, 'three', set(1, 'one', emptyWithNumericKeys<string>()))));
assert.strictEqual(get(3, tree), 'three');
});

test('should preserve the order of the rest of the tree after a key is removed', () => {
var tree = createTree();
for(var i = 0; i < unsortedValues.length; i++) {
Expand Down

0 comments on commit 98e08dc

Please sign in to comment.