Use Map for node meta data.

This commit is contained in:
Oleksii Trekhleb
2018-05-31 07:59:20 +03:00
parent e572de63cb
commit 7a4265403c
5 changed files with 47 additions and 73 deletions

View File

@@ -5,7 +5,7 @@ export default class BinarySearchTree {
* @param {function} [nodeValueCompareFunction]
*/
constructor(nodeValueCompareFunction) {
this.root = new BinarySearchTreeNode(null, null, nodeValueCompareFunction);
this.root = new BinarySearchTreeNode(null, nodeValueCompareFunction);
}
/**

View File

@@ -4,11 +4,10 @@ import Comparator from '../../../utils/comparator/Comparator';
export default class BinarySearchTreeNode extends BinaryTreeNode {
/**
* @param {*} [value] - node value.
* @param {Object} [meta] - any meta information that is attached to the node.
* @param {function} [compareFunction] - comparator function for node values.
*/
constructor(value = null, meta = null, compareFunction = undefined) {
super(value, meta);
constructor(value = null, compareFunction = undefined) {
super(value);
// This comparator is used to compare node values with each other.
this.compareFunction = compareFunction;
@@ -17,13 +16,11 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
/**
* @param {*} value
* @param {Object} [meta]
* @return {BinarySearchTreeNode}
*/
insert(value, meta = null) {
insert(value) {
if (this.nodeValueComparator.equal(this.value, null)) {
this.value = value;
this.meta = meta;
return this;
}
@@ -31,17 +28,23 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
if (this.nodeValueComparator.lessThan(value, this.value)) {
// Insert to the left.
if (this.left) {
this.left.insert(value, meta);
} else {
this.setLeft(new BinarySearchTreeNode(value, meta, this.compareFunction));
return this.left.insert(value);
}
const newNode = new BinarySearchTreeNode(value, this.compareFunction);
this.setLeft(newNode);
return newNode;
} else if (this.nodeValueComparator.greaterThan(value, this.value)) {
// Insert to the right.
if (this.right) {
this.right.insert(value, meta);
} else {
this.setRight(new BinarySearchTreeNode(value, meta, this.compareFunction));
return this.right.insert(value);
}
const newNode = new BinarySearchTreeNode(value, this.compareFunction);
this.setRight(newNode);
return newNode;
}
return this;
@@ -78,6 +81,7 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
/**
* @param {*} value
* @return {BinarySearchTreeNode}
*/
remove(value) {
const nodeToRemove = this.find(value);
@@ -115,6 +119,8 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
parent.replaceChild(nodeToRemove, nodeToRemove.right);
}
}
return nodeToRemove;
}
/**

View File

@@ -20,14 +20,16 @@ describe('BinarySearchTreeNode', () => {
it('should insert nodes in correct order', () => {
const bstNode = new BinarySearchTreeNode(2);
bstNode.insert(1);
const insertedNode1 = bstNode.insert(1);
expect(insertedNode1.value).toBe(1);
expect(bstNode.toString()).toBe('1,2');
expect(bstNode.contains(1)).toBeTruthy();
expect(bstNode.contains(3)).toBeFalsy();
bstNode.insert(3);
const insertedNode2 = bstNode.insert(3);
expect(insertedNode2.value).toBe(3);
expect(bstNode.toString()).toBe('1,2,3');
expect(bstNode.contains(3)).toBeTruthy();
expect(bstNode.contains(4)).toBeFalsy();
@@ -80,20 +82,24 @@ describe('BinarySearchTreeNode', () => {
});
it('should be possible to attach meta information to binary search tree nodes', () => {
const node = new BinarySearchTreeNode(10, { value: 10 });
const node = new BinarySearchTreeNode(10);
node.insert(20, { value: 20 });
node.insert(30, { value: 30 });
node.insert(5, { value: 5 });
node.insert(40, { value: 40 });
node.insert(1, { value: 1 });
node.insert(20);
const node1 = node.insert(30);
node.insert(5);
node.insert(40);
const node2 = node.insert(1);
expect(node.meta.value).toBe(10);
node.meta.set('color', 'red');
node1.meta.set('color', 'black');
node2.meta.set('color', 'white');
expect(node.meta.get('color')).toBe('red');
expect(node.findMin()).not.toBeNull();
expect(node.findMin().value).toBe(1);
expect(node.findMin().meta.value).toBe(1);
expect(node.find(30).meta.value).toBe(30);
expect(node.findMin().meta.get('color')).toBe('white');
expect(node.find(30).meta.get('color')).toBe('black');
});
it('should find node', () => {
@@ -205,7 +211,7 @@ describe('BinarySearchTreeNode', () => {
const obj2 = { key: 'obj2', value: 2, toString: () => 'obj2' };
const obj3 = { key: 'obj3', value: 3, toString: () => 'obj3' };
const bstNode = new BinarySearchTreeNode(obj2, null, nodeValueComparatorCallback);
const bstNode = new BinarySearchTreeNode(obj2, nodeValueComparatorCallback);
bstNode.insert(obj1);
expect(bstNode.toString()).toBe('obj1,obj2');