Add setValue and nodeCopy methods to binary tree node.

This commit is contained in:
Oleksii Trekhleb 2018-06-22 08:22:12 +03:00
parent bd5a16be71
commit 2334583635
3 changed files with 69 additions and 8 deletions

View File

@ -85,6 +85,16 @@ export default class BinaryTreeNode {
return this.parent.parent.left; return this.parent.parent.left;
} }
/**
* @param {*} value
* @return {BinaryTreeNode}
*/
setValue(value) {
this.value = value;
return this;
}
/** /**
* @param {BinaryTreeNode} node * @param {BinaryTreeNode} node
* @return {BinaryTreeNode} * @return {BinaryTreeNode}
@ -168,6 +178,16 @@ export default class BinaryTreeNode {
return false; return false;
} }
/**
* @param {BinaryTreeNode} sourceNode
* @param {BinaryTreeNode} targetNode
*/
static copyNode(sourceNode, targetNode) {
targetNode.setValue(sourceNode.value);
targetNode.setLeft(sourceNode.left);
targetNode.setRight(sourceNode.right);
}
/** /**
* @return {*[]} * @return {*[]}
*/ */

View File

@ -257,4 +257,41 @@ describe('BinaryTreeNode', () => {
expect(child.uncle).toBeDefined(); expect(child.uncle).toBeDefined();
expect(child.uncle).toEqual(uncle); expect(child.uncle).toEqual(uncle);
}); });
it('should be possible to set node values', () => {
const node = new BinaryTreeNode('initial_value');
expect(node.value).toBe('initial_value');
node.setValue('new_value');
expect(node.value).toBe('new_value');
});
it('should be possible to copy node', () => {
const root = new BinaryTreeNode('root');
const left = new BinaryTreeNode('left');
const right = new BinaryTreeNode('right');
root
.setLeft(left)
.setRight(right);
expect(root.toString()).toBe('left,root,right');
const newRoot = new BinaryTreeNode('new_root');
const newLeft = new BinaryTreeNode('new_left');
const newRight = new BinaryTreeNode('new_right');
newRoot
.setLeft(newLeft)
.setRight(newRight);
expect(newRoot.toString()).toBe('new_left,new_root,new_right');
BinaryTreeNode.copyNode(root, newRoot);
expect(root.toString()).toBe('left,root,right');
expect(newRoot.toString()).toBe('left,root,right');
});
}); });

View File

@ -99,7 +99,7 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
parent.removeChild(nodeToRemove); parent.removeChild(nodeToRemove);
} else { } else {
// Node has no parent. Just erase current node value. // Node has no parent. Just erase current node value.
nodeToRemove.value = null; nodeToRemove.setValue(undefined);
} }
} else if (nodeToRemove.left && nodeToRemove.right) { } else if (nodeToRemove.left && nodeToRemove.right) {
// Node has two children. // Node has two children.
@ -108,20 +108,24 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
const nextBiggerNode = nodeToRemove.right.findMin(); const nextBiggerNode = nodeToRemove.right.findMin();
if (!this.nodeComparator.equal(nextBiggerNode, nodeToRemove.right)) { if (!this.nodeComparator.equal(nextBiggerNode, nodeToRemove.right)) {
this.remove(nextBiggerNode.value); this.remove(nextBiggerNode.value);
nodeToRemove.value = nextBiggerNode.value; nodeToRemove.setValue(nextBiggerNode.value);
} else { } else {
// In case if next right value is the next bigger one and it doesn't have left child // In case if next right value is the next bigger one and it doesn't have left child
// then just replace node that is going to be deleted with the right node. // then just replace node that is going to be deleted with the right node.
nodeToRemove.value = nodeToRemove.right.value; nodeToRemove.setValue(nodeToRemove.right.value);
nodeToRemove.right = nodeToRemove.right.right; nodeToRemove.setRight(nodeToRemove.right.right);
} }
} else { } else {
// Node has only one child. // Node has only one child.
// Make this child to be a direct child of current node's parent. // Make this child to be a direct child of current node's parent.
const child = nodeToRemove.left || nodeToRemove.right; /** @var BinarySearchTreeNode */
nodeToRemove.value = child.value; const childNode = nodeToRemove.left || nodeToRemove.right;
nodeToRemove.setLeft(child.left);
nodeToRemove.setRight(child.right); if (parent) {
parent.replaceChild(nodeToRemove, childNode);
} else {
BinaryTreeNode.copyNode(childNode, nodeToRemove);
}
} }
return true; return true;