From f5494e9ca37edc086cef165bc54f237ac40cb99b Mon Sep 17 00:00:00 2001 From: balajipachai Date: Fri, 11 Oct 2019 17:40:17 +0530 Subject: [PATCH 1/2] Added tests for BST insertion --- .../BinarySearchTree/bst-insertion.test.js | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/_DataStructures_/Trees/BinarySearchTree/bst-insertion.test.js diff --git a/src/_DataStructures_/Trees/BinarySearchTree/bst-insertion.test.js b/src/_DataStructures_/Trees/BinarySearchTree/bst-insertion.test.js new file mode 100644 index 00000000..3c2f1b91 --- /dev/null +++ b/src/_DataStructures_/Trees/BinarySearchTree/bst-insertion.test.js @@ -0,0 +1,51 @@ +const BinarySearchTree = require('./index'); + +describe('Binary Search Tree', () => { + let bst; + let rootsLeftChild, rootsRightChild; + let rootsLeftChildsLeftChild, rootsLeftChildsRightChild; + let rootsRightChildsLeftChild, rootsRightChildsRightChild; + + describe('Creates a binary search tree', () => { + it('should create a bst with root 100', () => { + bst = new BinarySearchTree(100); + expect(bst.root.value).toEqual(100); + }); + + it('should add element 20 to the left of root node', () => { + bst.add(20); + rootsLeftChild = bst.root.leftChild; + expect(rootsLeftChild.value).toEqual(20); + }); + + it('should add element 500 to the right of root node', () => { + bst.add(500); + rootsRightChild = bst.root.rightChild; + expect(rootsRightChild.value).toEqual(500); + }); + + it('should add element 10 to the left of root"s left child', () => { + bst.add(10); + rootsLeftChildsLeftChild = bst.root.leftChild.leftChild; + expect(rootsLeftChildsLeftChild.value).toEqual(10); + }); + + it('should add element 30 to the right of root"s left child', () => { + bst.add(30); + rootsLeftChildsRightChild = bst.root.leftChild.rightChild; + expect(rootsLeftChildsRightChild.value).toEqual(30); + }); + + it('should add element 400 to the left of root"s right child', () => { + bst.add(400); + rootsRightChildsLeftChild = bst.root.rightChild.leftChild; + expect(rootsRightChildsLeftChild.value).toEqual(400); + }); + + it('should add element 600 to the right of root"s right child', () => { + bst.add(600); + rootsRightChildsRightChild = bst.root.rightChild.rightChild; + expect(rootsRightChildsRightChild.value).toEqual(600); + }); + }); +}); From 58dcda8946cb4a04477ec499e5cb40dbcf15b714 Mon Sep 17 00:00:00 2001 From: Balaji Pachai Date: Sun, 13 Oct 2019 11:44:04 +0530 Subject: [PATCH 2/2] Added tests for verifying tree was created as expected. --- .../BinarySearchTree/bst-insertion.test.js | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/_DataStructures_/Trees/BinarySearchTree/bst-insertion.test.js b/src/_DataStructures_/Trees/BinarySearchTree/bst-insertion.test.js index 3c2f1b91..8ada776d 100644 --- a/src/_DataStructures_/Trees/BinarySearchTree/bst-insertion.test.js +++ b/src/_DataStructures_/Trees/BinarySearchTree/bst-insertion.test.js @@ -2,9 +2,12 @@ const BinarySearchTree = require('./index'); describe('Binary Search Tree', () => { let bst; - let rootsLeftChild, rootsRightChild; - let rootsLeftChildsLeftChild, rootsLeftChildsRightChild; - let rootsRightChildsLeftChild, rootsRightChildsRightChild; + let rootsLeftChild; + let rootsRightChild; + let rootsLeftChildsLeftChild; + let rootsLeftChildsRightChild; + let rootsRightChildsLeftChild; + let rootsRightChildsRightChild; describe('Creates a binary search tree', () => { it('should create a bst with root 100', () => { @@ -48,4 +51,18 @@ describe('Binary Search Tree', () => { expect(rootsRightChildsRightChild.value).toEqual(600); }); }); + + describe('Check insertion was as expected', () => { + it('Inorder traversal of the created bst should be [ 10, 20, 30, 100, 400, 500, 600 ]', () => { + expect(bst.traverseInorder()).toEqual([10, 20, 30, 100, 400, 500, 600]); + }); + + it('Preorder traversal of the created bst should be [ 100, 20, 10, 30, 500, 400, 600 ]', () => { + expect(bst.traversePreorder()).toEqual([100, 20, 10, 30, 500, 400, 600]); + }); + + it('Postorder traversal of the created bst should be [ 10, 30, 20, 400, 600, 500, 100 ]', () => { + expect(bst.traversePostorder()).toEqual([10, 30, 20, 400, 600, 500, 100]); + }); + }); });