From d37adadf44537cff90278e061849281595aa2145 Mon Sep 17 00:00:00 2001 From: "m.maksyutin" Date: Fri, 1 Jun 2018 15:19:27 +0300 Subject: [PATCH 1/3] Fix LinkedList --- src/data-structures/linked-list/LinkedList.js | 2 +- src/data-structures/linked-list/__test__/LinkedList.test.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/data-structures/linked-list/LinkedList.js b/src/data-structures/linked-list/LinkedList.js index b428ad7395..4e82ab0d7f 100644 --- a/src/data-structures/linked-list/LinkedList.js +++ b/src/data-structures/linked-list/LinkedList.js @@ -60,7 +60,7 @@ export default class LinkedList { let deletedNode = null; // If the head must be deleted then make 2nd node to be a head. - if (this.compare.equal(this.head.value, value)) { + while (this.head && this.compare.equal(this.head.value, value)) { deletedNode = this.head; this.head = this.head.next; } diff --git a/src/data-structures/linked-list/__test__/LinkedList.test.js b/src/data-structures/linked-list/__test__/LinkedList.test.js index 1b938e1b10..61f018fee6 100644 --- a/src/data-structures/linked-list/__test__/LinkedList.test.js +++ b/src/data-structures/linked-list/__test__/LinkedList.test.js @@ -32,6 +32,7 @@ describe('LinkedList', () => { expect(linkedList.delete(5)).toBeNull(); + linkedList.append(1); linkedList.append(1); linkedList.append(2); linkedList.append(3); @@ -45,10 +46,10 @@ describe('LinkedList', () => { const deletedNode = linkedList.delete(3); expect(deletedNode.value).toBe(3); - expect(linkedList.toString()).toBe('1,2,4,5'); + expect(linkedList.toString()).toBe('1,1,2,4,5'); linkedList.delete(3); - expect(linkedList.toString()).toBe('1,2,4,5'); + expect(linkedList.toString()).toBe('1,1,2,4,5'); linkedList.delete(1); expect(linkedList.toString()).toBe('2,4,5'); From 8d85d3d443d34ff383fd52762f8efff059aac85c Mon Sep 17 00:00:00 2001 From: "m.maksyutin" Date: Sun, 3 Jun 2018 14:17:42 +0300 Subject: [PATCH 2/3] Fix the prepend method for the LinkedList --- src/data-structures/linked-list/LinkedList.js | 8 +++++++- .../linked-list/__test__/LinkedList.test.js | 8 ++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/data-structures/linked-list/LinkedList.js b/src/data-structures/linked-list/LinkedList.js index 4e82ab0d7f..7d330f7bfd 100644 --- a/src/data-structures/linked-list/LinkedList.js +++ b/src/data-structures/linked-list/LinkedList.js @@ -21,7 +21,13 @@ export default class LinkedList { */ prepend(value) { // Make new node to be a head. - this.head = new LinkedListNode(value, this.head); + const newNode = new LinkedListNode(value, this.head); + this.head = newNode; + + // If there is no tail yet let's make new node a tail. + if (!this.tail) { + this.tail = newNode; + } return this; } diff --git a/src/data-structures/linked-list/__test__/LinkedList.test.js b/src/data-structures/linked-list/__test__/LinkedList.test.js index 61f018fee6..384e44fa5b 100644 --- a/src/data-structures/linked-list/__test__/LinkedList.test.js +++ b/src/data-structures/linked-list/__test__/LinkedList.test.js @@ -21,10 +21,14 @@ describe('LinkedList', () => { it('should prepend node to linked list', () => { const linkedList = new LinkedList(); - linkedList.append(1); linkedList.prepend(2); + expect(linkedList.head.toString()).toBe('2'); + expect(linkedList.tail.toString()).toBe('2'); + + linkedList.append(1); + linkedList.prepend(3); - expect(linkedList.toString()).toBe('2,1'); + expect(linkedList.toString()).toBe('3,2,1'); }); it('should delete node by value from linked list', () => { From efe4f8119224e95c170c6c51ed038c6cf6a2b46c Mon Sep 17 00:00:00 2001 From: "m.maksyutin" Date: Mon, 4 Jun 2018 11:38:26 +0300 Subject: [PATCH 3/3] Fix the remove method for the MinHeap --- src/data-structures/heap/MinHeap.js | 2 +- .../heap/__test__/MinHeap.test.js | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/data-structures/heap/MinHeap.js b/src/data-structures/heap/MinHeap.js index bf23f415dd..5f32f53f74 100644 --- a/src/data-structures/heap/MinHeap.js +++ b/src/data-structures/heap/MinHeap.js @@ -141,8 +141,8 @@ export default class MinHeap { */ remove(item, customFindingComparator) { // Find number of items to remove. - const numberOfItemsToRemove = this.find(item).length; const customComparator = customFindingComparator || this.compare; + const numberOfItemsToRemove = this.find(item, customComparator).length; for (let iteration = 0; iteration < numberOfItemsToRemove; iteration += 1) { // We need to find item index to remove each time after removal since diff --git a/src/data-structures/heap/__test__/MinHeap.test.js b/src/data-structures/heap/__test__/MinHeap.test.js index f4ce4ac381..b2b8a7b157 100644 --- a/src/data-structures/heap/__test__/MinHeap.test.js +++ b/src/data-structures/heap/__test__/MinHeap.test.js @@ -1,4 +1,5 @@ import MinHeap from '../MinHeap'; +import Comparator from '../../../utils/comparator/Comparator'; describe('MinHeap', () => { it('should create an empty min heap', () => { @@ -147,4 +148,25 @@ describe('MinHeap', () => { expect(minHeap.remove(3).toString()).toEqual('4'); expect(minHeap.remove(4).toString()).toEqual(''); }); + + it('should be possible to remove items from heap with custom finding comparator', () => { + const minHeap = new MinHeap(); + minHeap.add('dddd'); + minHeap.add('ccc'); + minHeap.add('bb'); + minHeap.add('a'); + + expect(minHeap.toString()).toBe('a,bb,ccc,dddd'); + + const comparator = new Comparator((a, b) => { + if (a.length === b.length) { + return 0; + } + + return a.length < b.length ? -1 : 1; + }); + + minHeap.remove('hey', comparator); + expect(minHeap.toString()).toBe('a,bb,dddd'); + }); });