From b73aa7fe47160cf07a89fb93dbc69e71b368229e Mon Sep 17 00:00:00 2001 From: Kim Jong Hyen Date: Mon, 30 Jul 2018 18:10:06 +0900 Subject: [PATCH 01/10] Korean translation fix. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 어떤 종료의 -> 어떤 종류의 --- README.ko-KR.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.ko-KR.md b/README.ko-KR.md index 580c46d9..135d7728 100644 --- a/README.ko-KR.md +++ b/README.ko-KR.md @@ -48,7 +48,7 @@ _Read this in other languages:_ ## 알고리즘 -알고리즘은 어떤 종료의 문제를 풀 수 있는 정확한 방법이며, +알고리즘은 어떤 종류의 문제를 풀 수 있는 정확한 방법이며, 일련의 작업을 정확하게 정의해 놓은 규칙들입니다. `B` - 입문자, `A` - 숙련자 From 6e897a0a7ae5e3b5284f0e15b931e15b045de2f1 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Mon, 30 Jul 2018 14:18:56 +0300 Subject: [PATCH 02/10] Minor README updates. --- src/data-structures/bloom-filter/README.md | 2 +- src/data-structures/graph/README.md | 2 +- src/data-structures/hash-table/README.md | 8 ++++---- src/data-structures/heap/README.md | 2 +- src/data-structures/linked-list/README.md | 2 +- src/data-structures/priority-queue/README.md | 2 +- src/data-structures/queue/README.md | 2 +- src/data-structures/stack/README.md | 2 +- src/data-structures/tree/README.md | 2 +- src/data-structures/tree/avl-tree/README.md | 2 +- src/data-structures/tree/binary-search-tree/README.md | 2 +- src/data-structures/tree/red-black-tree/README.md | 2 +- src/data-structures/tree/segment-tree/README.md | 2 +- src/data-structures/trie/README.md | 2 +- 14 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/data-structures/bloom-filter/README.md b/src/data-structures/bloom-filter/README.md index 9690413f..4e852dd8 100644 --- a/src/data-structures/bloom-filter/README.md +++ b/src/data-structures/bloom-filter/README.md @@ -1,6 +1,6 @@ # Bloom Filter -A bloom filter is a space-efficient probabilistic +A **bloom filter** is a space-efficient probabilistic data structure designed to test whether an element is present in a set. It is designed to be blazingly fast and use minimal memory at the cost of potential diff --git a/src/data-structures/graph/README.md b/src/data-structures/graph/README.md index 380db3aa..c9b8ec4f 100644 --- a/src/data-structures/graph/README.md +++ b/src/data-structures/graph/README.md @@ -1,6 +1,6 @@ # Graph -In computer science, a graph is an abstract data type +In computer science, a **graph** is an abstract data type that is meant to implement the undirected graph and directed graph concepts from mathematics, specifically the field of graph theory diff --git a/src/data-structures/hash-table/README.md b/src/data-structures/hash-table/README.md index 59b51946..cdcad78b 100644 --- a/src/data-structures/hash-table/README.md +++ b/src/data-structures/hash-table/README.md @@ -1,9 +1,9 @@ # Hash Table -In computing, a hash table (hash map) is a data -structure which implements an associative array -abstract data type, a structure that can map keys -to values. A hash table uses a hash function to +In computing, a **hash table** (hash map) is a data +structure which implements an *associative array* +abstract data type, a structure that can *map keys +to values*. A hash table uses a *hash function* to compute an index into an array of buckets or slots, from which the desired value can be found diff --git a/src/data-structures/heap/README.md b/src/data-structures/heap/README.md index 8986b19e..204bb676 100644 --- a/src/data-structures/heap/README.md +++ b/src/data-structures/heap/README.md @@ -1,6 +1,6 @@ # Heap (data-structure) -In computer science, a heap is a specialized tree-based +In computer science, a **heap** is a specialized tree-based data structure that satisfies the heap property described below. diff --git a/src/data-structures/linked-list/README.md b/src/data-structures/linked-list/README.md index 3f258cfd..3fd0e45c 100644 --- a/src/data-structures/linked-list/README.md +++ b/src/data-structures/linked-list/README.md @@ -1,6 +1,6 @@ # Linked List -In computer science, a linked list is a linear collection +In computer science, a **linked list** is a linear collection of data elements, in which linear order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure diff --git a/src/data-structures/priority-queue/README.md b/src/data-structures/priority-queue/README.md index ecb52228..9704f7b2 100644 --- a/src/data-structures/priority-queue/README.md +++ b/src/data-structures/priority-queue/README.md @@ -1,6 +1,6 @@ # Priority Queue -In computer science, a priority queue is an abstract data type +In computer science, a **priority queue** is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before diff --git a/src/data-structures/queue/README.md b/src/data-structures/queue/README.md index a51dc376..3b6895f6 100644 --- a/src/data-structures/queue/README.md +++ b/src/data-structures/queue/README.md @@ -1,6 +1,6 @@ # Queue -In computer science, a queue is a particular kind of abstract data +In computer science, a **queue** is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principle (or only) operations on the collection are the addition of entities to the rear terminal diff --git a/src/data-structures/stack/README.md b/src/data-structures/stack/README.md index 305248d7..efec55bc 100644 --- a/src/data-structures/stack/README.md +++ b/src/data-structures/stack/README.md @@ -1,6 +1,6 @@ # Stack -In computer science, a stack is an abstract data type that serves +In computer science, a **stack** is an abstract data type that serves as a collection of elements, with two principal operations: * **push**, which adds an element to the collection, and diff --git a/src/data-structures/tree/README.md b/src/data-structures/tree/README.md index 42e69cc6..48fe9fe4 100644 --- a/src/data-structures/tree/README.md +++ b/src/data-structures/tree/README.md @@ -6,7 +6,7 @@ * [Segment Tree](segment-tree) - with min/max/sum range queries examples * [Fenwick Tree](fenwick-tree) (Binary Indexed Tree) -In computer science, a tree is a widely used abstract data +In computer science, a **tree** is a widely used abstract data type (ADT) — or data structure implementing this ADT—that simulates a hierarchical tree structure, with a root value and subtrees of children with a parent node, represented as diff --git a/src/data-structures/tree/avl-tree/README.md b/src/data-structures/tree/avl-tree/README.md index e8fb49e2..ff313a18 100644 --- a/src/data-structures/tree/avl-tree/README.md +++ b/src/data-structures/tree/avl-tree/README.md @@ -1,6 +1,6 @@ # AVL Tree -In computer science, an AVL tree (named after inventors +In computer science, an **AVL tree** (named after inventors Adelson-Velsky and Landis) is a self-balancing binary search tree. It was the first such data structure to be invented. In an AVL tree, the heights of the two child subtrees of any diff --git a/src/data-structures/tree/binary-search-tree/README.md b/src/data-structures/tree/binary-search-tree/README.md index cb914db8..21e76391 100644 --- a/src/data-structures/tree/binary-search-tree/README.md +++ b/src/data-structures/tree/binary-search-tree/README.md @@ -1,6 +1,6 @@ # Binary Search Tree -In computer science, binary search trees (BST), sometimes called +In computer science, **binary search trees** (BST), sometimes called ordered or sorted binary trees, are a particular type of container: data structures that store "items" (such as numbers, names etc.) in memory. They allow fast lookup, addition and removal of diff --git a/src/data-structures/tree/red-black-tree/README.md b/src/data-structures/tree/red-black-tree/README.md index 5f7fee8f..add32313 100644 --- a/src/data-structures/tree/red-black-tree/README.md +++ b/src/data-structures/tree/red-black-tree/README.md @@ -1,6 +1,6 @@ # Red–Black Tree -A red–black tree is a kind of self-balancing binary search +A **red–black tree** is a kind of self-balancing binary search tree in computer science. Each node of the binary tree has an extra bit, and that bit is often interpreted as the color (red or black) of the node. These color bits are used diff --git a/src/data-structures/tree/segment-tree/README.md b/src/data-structures/tree/segment-tree/README.md index 67d2fad1..0049daf1 100644 --- a/src/data-structures/tree/segment-tree/README.md +++ b/src/data-structures/tree/segment-tree/README.md @@ -1,6 +1,6 @@ # Segment Tree -In computer science, a segment tree also known as a statistic tree +In computer science, a **segment tree** also known as a statistic tree is a tree data structure used for storing information about intervals, or segments. It allows querying which of the stored segments contain a given point. It is, in principle, a static structure; that is, diff --git a/src/data-structures/trie/README.md b/src/data-structures/trie/README.md index 33a47681..6a8ee662 100644 --- a/src/data-structures/trie/README.md +++ b/src/data-structures/trie/README.md @@ -1,6 +1,6 @@ # Trie -In computer science, a trie, also called digital tree and sometimes +In computer science, a **trie**, also called digital tree and sometimes radix tree or prefix tree (as they can be searched by prefixes), is a kind of search tree—an ordered tree data structure that is used to store a dynamic set or associative array where the keys From 5105898aa7e2ed4c770ec58434c00f4b76539580 Mon Sep 17 00:00:00 2001 From: seIncorp <34153549+seIncorp@users.noreply.github.com> Date: Mon, 30 Jul 2018 14:33:17 +0200 Subject: [PATCH 03/10] Twice defined (#124) * Twice defined Parameter 'deletedTail' were defined twice. * Update LinkedList.js * Update LinkedList.js --- src/data-structures/linked-list/LinkedList.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/data-structures/linked-list/LinkedList.js b/src/data-structures/linked-list/LinkedList.js index 0176b6bf..4d6b9e26 100644 --- a/src/data-structures/linked-list/LinkedList.js +++ b/src/data-structures/linked-list/LinkedList.js @@ -127,18 +127,14 @@ export default class LinkedList { * @return {LinkedListNode} */ deleteTail() { + const deletedTail = this.tail; if (this.head === this.tail) { // There is only one node in linked list. - const deletedTail = this.tail; this.head = null; this.tail = null; return deletedTail; } - - // If there are many nodes in linked list... - const deletedTail = this.tail; - // Rewind to the last node and delete "next" link for the node before the last one. let currentNode = this.head; while (currentNode.next) { From 97926b124317c3b6d9dc651a1f7d38326c250671 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Mon, 30 Jul 2018 15:35:28 +0300 Subject: [PATCH 04/10] Add comments to Linked List code. --- src/data-structures/linked-list/LinkedList.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/data-structures/linked-list/LinkedList.js b/src/data-structures/linked-list/LinkedList.js index 4d6b9e26..ce5a2544 100644 --- a/src/data-structures/linked-list/LinkedList.js +++ b/src/data-structures/linked-list/LinkedList.js @@ -128,6 +128,7 @@ export default class LinkedList { */ deleteTail() { const deletedTail = this.tail; + if (this.head === this.tail) { // There is only one node in linked list. this.head = null; @@ -135,6 +136,9 @@ export default class LinkedList { return deletedTail; } + + // If there are many nodes in linked list... + // Rewind to the last node and delete "next" link for the node before the last one. let currentNode = this.head; while (currentNode.next) { From ecc8c65466e51e7f50a774e2b1b2707ed586ec1f Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Mon, 30 Jul 2018 15:38:04 +0300 Subject: [PATCH 05/10] Add comments to Linked List code. --- src/data-structures/linked-list/LinkedList.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/data-structures/linked-list/LinkedList.js b/src/data-structures/linked-list/LinkedList.js index ce5a2544..587177f9 100644 --- a/src/data-structures/linked-list/LinkedList.js +++ b/src/data-structures/linked-list/LinkedList.js @@ -65,7 +65,8 @@ export default class LinkedList { let deletedNode = null; - // If the head must be deleted then make 2nd node to be a head. + // If the head must be deleted then make next node that is differ + // from the head to be a new head. while (this.head && this.compare.equal(this.head.value, value)) { deletedNode = this.head; this.head = this.head.next; From 7a4b829abecf85b2e1079d89de2049e10b23400f Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Thu, 2 Aug 2018 16:43:54 +0300 Subject: [PATCH 06/10] Fix issue #132 - Wrong Red-Black Tree pictures in README. --- src/data-structures/tree/red-black-tree/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/data-structures/tree/red-black-tree/README.md b/src/data-structures/tree/red-black-tree/README.md index add32313..ed488046 100644 --- a/src/data-structures/tree/red-black-tree/README.md +++ b/src/data-structures/tree/red-black-tree/README.md @@ -74,7 +74,7 @@ unlike ordinary binary search trees. #### Left Right Case (See g, p and x) -![Red Black Tree Balancing](https://www.geeksforgeeks.org/wp-content/uploads/redBlackCase3d.png) +![Red Black Tree Balancing](https://www.geeksforgeeks.org/wp-content/uploads/redBlackCase3b.png) #### Right Right Case (See g, p and x) @@ -82,7 +82,7 @@ unlike ordinary binary search trees. #### Right Left Case (See g, p and x) -![Red Black Tree Balancing](https://www.geeksforgeeks.org/wp-content/uploads/redBlackCase3c.png) +![Red Black Tree Balancing](https://www.geeksforgeeks.org/wp-content/uploads/redBlackCase3d.png) ## References From 0ea24230d4fa77bd7666bc871f1c7682a7b62795 Mon Sep 17 00:00:00 2001 From: Simon <34153549+seIncorp@users.noreply.github.com> Date: Mon, 6 Aug 2018 15:02:46 +0200 Subject: [PATCH 07/10] Patch 5 (#127) * New function 'fromArray' Function get array of Doubly Linked List Nodes, go through and append to currently list. * New Test for new function 'fromArray' * Minor changes Minor changes about coding style. --- .../doubly-linked-list/DoublyLinkedList.js | 9 +++++++++ .../__test__/DoublyLinkedList.test.js | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/data-structures/doubly-linked-list/DoublyLinkedList.js b/src/data-structures/doubly-linked-list/DoublyLinkedList.js index 25fb0508..6b7f908e 100644 --- a/src/data-structures/doubly-linked-list/DoublyLinkedList.js +++ b/src/data-structures/doubly-linked-list/DoublyLinkedList.js @@ -213,6 +213,15 @@ export default class DoublyLinkedList { return nodes; } + /** + * @param {DoublyLinkedListNode[]} array - Array of nodes + * @return {DoublyLinkedListNode[]} + */ + fromArray(arr = []) { + arr.forEach(node => this.append(node.value)); + return this; + } + /** * @param {function} [callback] * @return {string} diff --git a/src/data-structures/doubly-linked-list/__test__/DoublyLinkedList.test.js b/src/data-structures/doubly-linked-list/__test__/DoublyLinkedList.test.js index 4490574e..d036e3f5 100644 --- a/src/data-structures/doubly-linked-list/__test__/DoublyLinkedList.test.js +++ b/src/data-structures/doubly-linked-list/__test__/DoublyLinkedList.test.js @@ -36,6 +36,23 @@ describe('DoublyLinkedList', () => { expect(linkedList.toString()).toBe('3,2,1'); }); + it('should append new nodes from array', () => { + const linkedList1 = new DoublyLinkedList(); + linkedList1.append(1); + linkedList1.append(1); + linkedList1.append(2); + linkedList1.append(3); + linkedList1.append(3); + linkedList1.append(3); + linkedList1.append(4); + linkedList1.append(5); + const array = linkedList1.toArray(); + + const linkedList2 = new DoublyLinkedList(); + linkedList2.fromArray(array); + expect(linkedList2.toString()).toBe('1,1,2,3,3,3,4,5'); + }); + it('should delete node by value from linked list', () => { const linkedList = new DoublyLinkedList(); From 88bbfdc4707e6cafb5b8f9240fc6613356a45029 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Mon, 6 Aug 2018 16:12:31 +0300 Subject: [PATCH 08/10] Add fromArray() methods to LinkedList and DoublyLinkedList. --- .../doubly-linked-list/DoublyLinkedList.js | 9 +++++---- .../__test__/DoublyLinkedList.test.js | 18 ++++-------------- src/data-structures/linked-list/LinkedList.js | 10 ++++++++++ .../linked-list/__test__/LinkedList.test.js | 7 +++++++ 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/data-structures/doubly-linked-list/DoublyLinkedList.js b/src/data-structures/doubly-linked-list/DoublyLinkedList.js index 6b7f908e..4afb404b 100644 --- a/src/data-structures/doubly-linked-list/DoublyLinkedList.js +++ b/src/data-structures/doubly-linked-list/DoublyLinkedList.js @@ -214,11 +214,12 @@ export default class DoublyLinkedList { } /** - * @param {DoublyLinkedListNode[]} array - Array of nodes - * @return {DoublyLinkedListNode[]} + * @param {*[]} values - Array of values that need to be converted to linked list. + * @return {DoublyLinkedList} */ - fromArray(arr = []) { - arr.forEach(node => this.append(node.value)); + fromArray(values) { + values.forEach(value => this.append(value)); + return this; } diff --git a/src/data-structures/doubly-linked-list/__test__/DoublyLinkedList.test.js b/src/data-structures/doubly-linked-list/__test__/DoublyLinkedList.test.js index d036e3f5..5e4e81ea 100644 --- a/src/data-structures/doubly-linked-list/__test__/DoublyLinkedList.test.js +++ b/src/data-structures/doubly-linked-list/__test__/DoublyLinkedList.test.js @@ -36,21 +36,11 @@ describe('DoublyLinkedList', () => { expect(linkedList.toString()).toBe('3,2,1'); }); - it('should append new nodes from array', () => { - const linkedList1 = new DoublyLinkedList(); - linkedList1.append(1); - linkedList1.append(1); - linkedList1.append(2); - linkedList1.append(3); - linkedList1.append(3); - linkedList1.append(3); - linkedList1.append(4); - linkedList1.append(5); - const array = linkedList1.toArray(); + it('should create linked list from array', () => { + const linkedList = new DoublyLinkedList(); + linkedList.fromArray([1, 1, 2, 3, 3, 3, 4, 5]); - const linkedList2 = new DoublyLinkedList(); - linkedList2.fromArray(array); - expect(linkedList2.toString()).toBe('1,1,2,3,3,3,4,5'); + expect(linkedList.toString()).toBe('1,1,2,3,3,3,4,5'); }); it('should delete node by value from linked list', () => { diff --git a/src/data-structures/linked-list/LinkedList.js b/src/data-structures/linked-list/LinkedList.js index 587177f9..dfb17aaf 100644 --- a/src/data-structures/linked-list/LinkedList.js +++ b/src/data-structures/linked-list/LinkedList.js @@ -175,6 +175,16 @@ export default class LinkedList { return deletedHead; } + /** + * @param {*[]} values - Array of values that need to be converted to linked list. + * @return {LinkedList} + */ + fromArray(values) { + values.forEach(value => this.append(value)); + + return this; + } + /** * @return {LinkedListNode[]} */ diff --git a/src/data-structures/linked-list/__test__/LinkedList.test.js b/src/data-structures/linked-list/__test__/LinkedList.test.js index 384e44fa..376a2e01 100644 --- a/src/data-structures/linked-list/__test__/LinkedList.test.js +++ b/src/data-structures/linked-list/__test__/LinkedList.test.js @@ -184,6 +184,13 @@ describe('LinkedList', () => { expect(linkedList.find({ callback: value => value.key === 'test5' })).toBeNull(); }); + it('should create linked list from array', () => { + const linkedList = new LinkedList(); + linkedList.fromArray([1, 1, 2, 3, 3, 3, 4, 5]); + + expect(linkedList.toString()).toBe('1,1,2,3,3,3,4,5'); + }); + it('should find node by means of custom compare function', () => { const comparatorFunction = (a, b) => { if (a.customValue === b.customValue) { From 941feda305fc7648505c63bf472b3a5725ac1881 Mon Sep 17 00:00:00 2001 From: Diego Paiva <32985519+diegopaiva1@users.noreply.github.com> Date: Mon, 6 Aug 2018 10:26:22 -0300 Subject: [PATCH 09/10] Fix kruskal's exception message for directed graph (#138) --- src/algorithms/graph/kruskal/kruskal.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/graph/kruskal/kruskal.js b/src/algorithms/graph/kruskal/kruskal.js index 3f0ac44f..dd9968a0 100644 --- a/src/algorithms/graph/kruskal/kruskal.js +++ b/src/algorithms/graph/kruskal/kruskal.js @@ -10,7 +10,7 @@ export default function kruskal(graph) { // It should fire error if graph is directed since the algorithm works only // for undirected graphs. if (graph.isDirected) { - throw new Error('Prim\'s algorithms works only for undirected graphs'); + throw new Error('Kruskal\'s algorithms works only for undirected graphs'); } // Init new graph that will contain minimum spanning tree of original graph. From 7c9601df3e8ca4206d419ce50b88bd13ff39deb6 Mon Sep 17 00:00:00 2001 From: Oz Weiss Date: Mon, 6 Aug 2018 16:40:23 +0300 Subject: [PATCH 10/10] Update combinationSum.js (#137) fixing a few typos in comments --- src/algorithms/sets/combination-sum/combinationSum.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/algorithms/sets/combination-sum/combinationSum.js b/src/algorithms/sets/combination-sum/combinationSum.js index c36b9f8a..dd396a8c 100644 --- a/src/algorithms/sets/combination-sum/combinationSum.js +++ b/src/algorithms/sets/combination-sum/combinationSum.js @@ -15,20 +15,20 @@ function combinationSumRecursive( ) { if (remainingSum < 0) { // By adding another candidate we've gone below zero. - // This would mean that last candidate was not acceptable. + // This would mean that the last candidate was not acceptable. return finalCombinations; } if (remainingSum === 0) { - // In case if after adding the previous candidate out remaining sum - // became zero we need to same current combination since it is one - // of the answer we're looking for. + // If after adding the previous candidate our remaining sum + // became zero - we need to save the current combination since it is one + // of the answers we're looking for. finalCombinations.push(currentCombination.slice()); return finalCombinations; } - // In case if we haven't reached zero yet let's continue to add all + // If we haven't reached zero yet let's continue to add all // possible candidates that are left. for (let candidateIndex = startFrom; candidateIndex < candidates.length; candidateIndex += 1) { const currentCandidate = candidates[candidateIndex];