From 1c911aadf009e9ee59773728c7a3082d898ef205 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Tue, 3 Apr 2018 07:50:30 +0300 Subject: [PATCH] Add binary search tree. --- src/data-structures/tree/BinaryTreeNode.js | 5 ++++- .../tree/__test__/BinaryTreeNode.test.js | 15 +++++++++++++++ .../tree/binary-search-tree/BinarySearchTree.js | 2 +- .../binary-search-tree/BinarySearchTreeNode.js | 4 ++-- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/data-structures/tree/BinaryTreeNode.js b/src/data-structures/tree/BinaryTreeNode.js index c4edba28..74e77866 100644 --- a/src/data-structures/tree/BinaryTreeNode.js +++ b/src/data-structures/tree/BinaryTreeNode.js @@ -1,17 +1,20 @@ export default class BinaryTreeNode { - constructor(value = null) { + constructor(value = null, parent = null) { this.left = null; this.right = null; + this.parent = parent; this.value = value; } setLeft(node) { this.left = node; + this.left.parent = this; return this; } setRight(node) { this.right = node; + this.right.parent = this; return this; } diff --git a/src/data-structures/tree/__test__/BinaryTreeNode.test.js b/src/data-structures/tree/__test__/BinaryTreeNode.test.js index 65ec5977..7f68015e 100644 --- a/src/data-structures/tree/__test__/BinaryTreeNode.test.js +++ b/src/data-structures/tree/__test__/BinaryTreeNode.test.js @@ -23,6 +23,21 @@ describe('BinaryTreeNode', () => { expect(rootNode.right.value).toBe(3); }); + it('should set parent', () => { + const leftNode = new BinaryTreeNode(1); + const rightNode = new BinaryTreeNode(3); + const rootNode = new BinaryTreeNode(2); + + rootNode + .setLeft(leftNode) + .setRight(rightNode); + + expect(rootNode.parent).toBeNull(); + expect(rootNode.left.parent.value).toBe(2); + expect(rootNode.right.parent.value).toBe(2); + expect(rootNode.right.parent).toEqual(rootNode); + }); + it('should traverse node', () => { const leftNode = new BinaryTreeNode(1); const rightNode = new BinaryTreeNode(3); diff --git a/src/data-structures/tree/binary-search-tree/BinarySearchTree.js b/src/data-structures/tree/binary-search-tree/BinarySearchTree.js index be440d70..904650ee 100644 --- a/src/data-structures/tree/binary-search-tree/BinarySearchTree.js +++ b/src/data-structures/tree/binary-search-tree/BinarySearchTree.js @@ -14,7 +14,7 @@ export default class BinarySearchTree { } remove(value) { - const nodeToRemove = this.findNode(value); + const nodeToRemove = this.root.find(value); if (!nodeToRemove) { throw new Error('Item not found in the tree'); diff --git a/src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js b/src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js index 584ac182..144c250a 100644 --- a/src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js +++ b/src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js @@ -12,14 +12,14 @@ export default class BinarySearchTreeNode extends BinaryTreeNode { if (this.left) { this.left.insert(value); } else { - this.left = new BinarySearchTreeNode(value); + this.setLeft(new BinarySearchTreeNode(value)); } } else if (value > this.value) { // Insert to the right. if (this.right) { this.right.insert(value); } else { - this.right = new BinarySearchTreeNode(value); + this.setRight(new BinarySearchTreeNode(value)); } }