diff --git a/src/_DataStructures_/Heaps/MaxHeap/MaxHeap.test.js b/src/_DataStructures_/Heaps/MaxHeap/MaxHeap.test.js index 5fee6e35..7d94e4fb 100644 --- a/src/_DataStructures_/Heaps/MaxHeap/MaxHeap.test.js +++ b/src/_DataStructures_/Heaps/MaxHeap/MaxHeap.test.js @@ -38,8 +38,11 @@ describe('MaxHeap', () => { }); it('Should return `null` when heap is empty', () => { - [1, 34].forEach(el => mh.add(el)); - expect(mh.getMax()).toEqual(34); + [1, 34, 43, 54, 123].forEach(el => mh.add(el)); + mh.remove(); + mh.remove(); + mh.remove(); + mh.remove(); mh.remove(); mh.remove(); expect(mh.getMax()).toEqual(null); diff --git a/src/_DataStructures_/Heaps/MinHeap/MinHeap.test.js b/src/_DataStructures_/Heaps/MinHeap/MinHeap.test.js index 7c47bc6c..bd578925 100644 --- a/src/_DataStructures_/Heaps/MinHeap/MinHeap.test.js +++ b/src/_DataStructures_/Heaps/MinHeap/MinHeap.test.js @@ -38,10 +38,14 @@ describe('MinHeap', () => { }); it('Should return `null` when heap is empty', () => { - [1, 34].forEach(el => mh.add(el)); + [1, 34, 43, 54, 123].forEach(el => mh.add(el)); expect(mh.getMin()).toEqual(1); mh.remove(); mh.remove(); + mh.remove(); + mh.remove(); + mh.remove(); + mh.remove(); expect(mh.getMin()).toEqual(null); }); diff --git a/src/_DataStructures_/LinkedList/LinkedList.test.js b/src/_DataStructures_/LinkedList/LinkedList.test.js index b237f698..85157be0 100644 --- a/src/_DataStructures_/LinkedList/LinkedList.test.js +++ b/src/_DataStructures_/LinkedList/LinkedList.test.js @@ -224,8 +224,11 @@ describe('Data Structures: Linked Lists', () => { }); it('Should remove and return the element at given index value', () => { - expect(list.removeAt(3).data).toEqual('Welcome'); - expect(list.removeAt(2).data).toEqual('There!'); + list.delete(); + [1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(el => list.addAtBeginning(el)); + expect(list.removeAt(10).data).toEqual(1); + expect(list.removeAt(0).data).toEqual(9); + expect(list.removeAt(5).data).toEqual(3); }); }); }); diff --git a/src/_DataStructures_/Trees/BinaryTree/bottom-view-binary-tree/BottomViewBinaryTree.test.js b/src/_DataStructures_/Trees/BinaryTree/bottom-view-binary-tree/BottomViewBinaryTree.test.js index ea178f79..6ba4591e 100644 --- a/src/_DataStructures_/Trees/BinaryTree/bottom-view-binary-tree/BottomViewBinaryTree.test.js +++ b/src/_DataStructures_/Trees/BinaryTree/bottom-view-binary-tree/BottomViewBinaryTree.test.js @@ -1,8 +1,8 @@ -const BinaryTree = require("../index"); +const BinaryTree = require('../index'); const bottomView = require('.'); describe('Bottom View Binary Tree', () => { - let btree + let btree; beforeEach(() => { btree = new BinaryTree([1, 2, 3, 4, 5, 6]); diff --git a/src/_DataStructures_/Trees/BinaryTree/btree-traversals.test.js b/src/_DataStructures_/Trees/BinaryTree/btree-traversals.test.js index 27009989..34aa2e1e 100644 --- a/src/_DataStructures_/Trees/BinaryTree/btree-traversals.test.js +++ b/src/_DataStructures_/Trees/BinaryTree/btree-traversals.test.js @@ -1,15 +1,20 @@ -const BinaryTree = require("./index"); +const BinaryTree = require('./index'); -describe("Binary Tree Preorder Traversal", () => { +describe('Binary Tree Preorder Traversal', () => { let btree; let preOrderTraversal; - describe("Creates BTree", () => { + describe('Creates BTree', () => { + it('Should throw error if argument is not array', () => { + expect(() => { + btree = new BinaryTree('Hello tree'); + }).toThrow('Invalid argument to create a Binary Tree'); + }); btree = new BinaryTree([1, 2, 3, 4, 5, 6]); }); - describe("BTree Traversals", () => { - it("should compute the Preorder traversal for the above created binary tree", () => { + describe('BTree Traversals', () => { + it('Should compute the Preorder traversal for the above created binary tree', () => { preOrderTraversal = btree.preOrder(); expect(preOrderTraversal).toEqual([1, 2, 4, 5, 3, 6]); }); diff --git a/src/_DataStructures_/Trees/Trie/Node.js b/src/_DataStructures_/Trees/Trie/Node.js index ab6cfc27..35f5de29 100644 --- a/src/_DataStructures_/Trees/Trie/Node.js +++ b/src/_DataStructures_/Trees/Trie/Node.js @@ -13,10 +13,6 @@ class TrieNode { this.isEndOfWord = true; } - unmarkAsLeaf() { - this.isEndOfWord = false; - } - increaseCount() { this.wordCount += 1; } diff --git a/src/_DataStructures_/Trees/Trie/all-words-in-trie/all-words-in-trie.test.js b/src/_DataStructures_/Trees/Trie/all-words-in-trie/all-words-in-trie.test.js index ae9b2e99..b6d11516 100644 --- a/src/_DataStructures_/Trees/Trie/all-words-in-trie/all-words-in-trie.test.js +++ b/src/_DataStructures_/Trees/Trie/all-words-in-trie/all-words-in-trie.test.js @@ -2,6 +2,12 @@ const allWordsInTrie = require('./index'); const Trie = require('../index'); describe('Data Structure : Trie : All Words In Tree', () => { + it('Should return empty array', () => { + const trie = new Trie(); + const result = allWordsInTrie(trie.root); + expect(result.length).toEqual(0); + }); + it('Should return all words sorted alphabetically', () => { const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed']; const trie = new Trie(); diff --git a/src/_DataStructures_/Trees/Trie/all-words-in-trie/index.js b/src/_DataStructures_/Trees/Trie/all-words-in-trie/index.js index d46a66ec..8f3cab23 100644 --- a/src/_DataStructures_/Trees/Trie/all-words-in-trie/index.js +++ b/src/_DataStructures_/Trees/Trie/all-words-in-trie/index.js @@ -5,8 +5,6 @@ const TrieNode = require('../Node'); function getAllWords(root, level, word) { let result = []; - if (!root) return result; - if (root.isEndOfWord) { let temp = ''; for (let i = 0; i < level; i += 1) { diff --git a/src/_DataStructures_/Trees/Trie/get-unique-words/index.js b/src/_DataStructures_/Trees/Trie/get-unique-words/index.js index 1daedd91..72e0d3d9 100644 --- a/src/_DataStructures_/Trees/Trie/get-unique-words/index.js +++ b/src/_DataStructures_/Trees/Trie/get-unique-words/index.js @@ -4,8 +4,6 @@ const TrieNode = require('../Node'); function getAllUniqueWords(root, level, word) { let result = []; - if (!root) return result; - if (root.isEndOfWord) { let temp = ''; for (let i = 0; i < level; i += 1) { @@ -25,7 +23,7 @@ function getAllUniqueWords(root, level, word) { } function allUniqueWordsFromTrie(root) { - if (!(root instanceof TrieNode)) { + if (!(root instanceof TrieNode)) { throw new Error('Invalid argument: Root of Trie is required'); } const word = []; // char arr to store a word diff --git a/src/_DataStructures_/Trees/Trie/search.test.js b/src/_DataStructures_/Trees/Trie/search.test.js new file mode 100644 index 00000000..11140ef6 --- /dev/null +++ b/src/_DataStructures_/Trees/Trie/search.test.js @@ -0,0 +1,31 @@ +const Trie = require('./index'); + +describe('Data Structure : Trie', () => { + describe('Trie Instance', () => { + it('Should be a class', () => { + expect(typeof Trie.prototype.constructor).toEqual('function'); + }); + }); + + describe('Trie API', () => { + const words = ['bed', 'ball', 'apple', 'java', 'javascript']; + let trie; + it('Should insert string', () => { + trie = new Trie(); + words.forEach(word => trie.insert(word)); + }); + + it('Should return `True` if string present', () => { + expect(trie.search(words[0])).toEqual(true); + }); + + it('Should return `False` if string present', () => { + expect(trie.search('Ashu')).toEqual(false); + expect(trie.search('be')).toEqual(false); + }); + + it('Should return `False` if argument is not pass', () => { + expect(trie.search()).toEqual(false); + }); + }); +}); diff --git a/src/_Problems_/binary-tree-to-binary-search-tree/binary-tree-to-binary-search-tree.test.js b/src/_Problems_/binary-tree-to-binary-search-tree/binary-tree-to-binary-search-tree.test.js index 95913a7e..07693576 100644 --- a/src/_Problems_/binary-tree-to-binary-search-tree/binary-tree-to-binary-search-tree.test.js +++ b/src/_Problems_/binary-tree-to-binary-search-tree/binary-tree-to-binary-search-tree.test.js @@ -4,11 +4,14 @@ const BinaryTree = require('../../_DataStructures_/Trees/BinaryTree'); describe('Binary tree to binary search tree', () => { let tree; - describe('Create Binary Tree', () => { - tree = new BinaryTree([10, 30, 15, 20, null, null, 5]); + it('Should return `null` if root is null', () => { + tree = new BinaryTree([1]); + tree.root = null; + expect(binaryTreeToBST(tree)).toEqual(null); }); it('Should converted binary tree to binary search tree', () => { + tree = new BinaryTree([10, 30, 15, 20, null, null, 5]); const bTree = binaryTreeToBST(tree); expect(storeInorder(bTree)).toEqual([5, 10, 15, 20, 30]); }); diff --git a/src/_Problems_/next-greater-element/index.js b/src/_Problems_/next-greater-element/index.js index 7d98dc7d..38088074 100644 --- a/src/_Problems_/next-greater-element/index.js +++ b/src/_Problems_/next-greater-element/index.js @@ -20,9 +20,7 @@ function nextGreaterElement(arr) { for (let i = arr.length - 1; i >= 0; i -= 1) { if (s1.peek()) { let top = s1.peek(); - while (top <= arr[i]) { - // if the stack is empty, break the while loop - if (!s1.peek()) break; + while (top && top <= arr[i]) { // pop the elements s1.pop(); // get the new top diff --git a/src/_Problems_/next-greater-element/next-greater-element.test.js b/src/_Problems_/next-greater-element/next-greater-element.test.js index e729f0db..82950b00 100644 --- a/src/_Problems_/next-greater-element/next-greater-element.test.js +++ b/src/_Problems_/next-greater-element/next-greater-element.test.js @@ -1,38 +1,38 @@ const { nextGreaterElement } = require('.'); describe('Next greater element', () => { - it('returns next greater elements collection', () => { - const input = [4, 6, 3, 2, 8, 1] - const greaterElements = [6, 8, 8, 8, -1, -1] + it('Should returns next greater elements collection', () => { + const input = [4, 11, 6, 3, 2, 8, 1]; + const greaterElements = [11, -1, 8, 8, 8, -1, -1]; expect(nextGreaterElement(input)).toEqual(greaterElements); }); - it('returns and empty collection for an empty array', () => { + it('Should returns and empty collection for an empty array', () => { expect(nextGreaterElement([])).toEqual([]); }); - it('returns an array with -1 if the input has only one element', () => { + it('Should returns an array with -1 if the input has only one element', () => { expect(nextGreaterElement([0])).toEqual([-1]); }); - it('returns a collection of -1 if there is no greater element', () => { - const input = [90, 40, 15, 7, -1, -10] - const greaterElements = [-1, -1, -1, -1, -1, -1] + it('Should returns a collection of -1 if there is no greater element', () => { + const input = [90, 40, 15, 7, -1, -10]; + const greaterElements = [-1, -1, -1, -1, -1, -1]; expect(nextGreaterElement(input)).toEqual(greaterElements); }); - it('uses -1 if the numbers are the same', () => { - const input = [90, 90] - const greaterElements = [-1, -1] + it('Should uses -1 if the numbers are the same', () => { + const input = [90, 90]; + const greaterElements = [-1, -1]; expect(nextGreaterElement(input)).toEqual(greaterElements); }); - it('throws an error if the input is not an array', () => { + it('Should throws an error if the input is not an array', () => { expect(() => { - nextGreaterElement('xunda') + nextGreaterElement('xunda'); }).toThrowError('Invalid Argument'); }); });