From 1b0e27ab86dbae97b99620db3ecacffafd17d98a Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Fri, 11 Dec 2020 09:14:48 +0100 Subject: [PATCH] Add getValues() method to HashTable and update LinkedList READMEs. --- .../doubly-linked-list/README.md | 2 +- src/data-structures/hash-table/HashTable.js | 11 +++++----- .../hash-table/__test__/HashTable.test.js | 20 ++++++++++++++++++- src/data-structures/linked-list/README.md | 3 ++- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/data-structures/doubly-linked-list/README.md b/src/data-structures/doubly-linked-list/README.md index ec603a8f..c64b5126 100644 --- a/src/data-structures/doubly-linked-list/README.md +++ b/src/data-structures/doubly-linked-list/README.md @@ -12,7 +12,7 @@ consists of a set of sequentially linked records called nodes. Each node contain two fields, called links, that are references to the previous and to the next node in the sequence of nodes. The beginning and ending nodes' previous and next links, respectively, point to some kind of terminator, typically a sentinel -node or null, to facilitate traversal of the list. If there is only one +node or null, to facilitate the traversal of the list. If there is only one sentinel node, then the list is circularly linked via the sentinel node. It can be conceptualized as two singly linked lists formed from the same data items, but in opposite sequential orders. diff --git a/src/data-structures/hash-table/HashTable.js b/src/data-structures/hash-table/HashTable.js index 6ac83a52..b8b523ea 100644 --- a/src/data-structures/hash-table/HashTable.js +++ b/src/data-structures/hash-table/HashTable.js @@ -107,14 +107,15 @@ export default class HashTable { } /** - * Gets the list of all the stored values in the hash table in the order of - * the keys map. + * Gets the list of all the stored values in the hash table. * * @return {*[]} */ getValues() { - const keys = this.getKeys(); - - return keys.map(key => this.buckets[this.hash(key)].head.value.value); + return this.buckets.reduce((values, bucket) => { + const bucketValues = bucket.toArray() + .map((linkedListNode) => linkedListNode.value.value); + return values.concat(bucketValues); + }, []); } } diff --git a/src/data-structures/hash-table/__test__/HashTable.test.js b/src/data-structures/hash-table/__test__/HashTable.test.js index cc8d45d5..86bbf3ad 100644 --- a/src/data-structures/hash-table/__test__/HashTable.test.js +++ b/src/data-structures/hash-table/__test__/HashTable.test.js @@ -94,6 +94,24 @@ describe('HashTable', () => { hashTable.set('b', 'beta'); hashTable.set('c', 'gamma'); - expect(hashTable.getValues()).toEqual(['alpha', 'beta', 'gamma']); + expect(hashTable.getValues()).toEqual(['gamma', 'alpha', 'beta']); + }); + + it('should get all the values from empty hash table', () => { + const hashTable = new HashTable(); + expect(hashTable.getValues()).toEqual([]); + }); + + it('should get all the values in case of hash collision', () => { + const hashTable = new HashTable(3); + + // Keys `ab` and `ba` in current implementation should result in one hash (one bucket). + // We need to make sure that several items from one bucket will be serialized. + hashTable.set('ab', 'one'); + hashTable.set('ba', 'two'); + + hashTable.set('ac', 'three'); + + expect(hashTable.getValues()).toEqual(['one', 'two', 'three']); }); }); diff --git a/src/data-structures/linked-list/README.md b/src/data-structures/linked-list/README.md index 3fd10e24..2b8a7507 100644 --- a/src/data-structures/linked-list/README.md +++ b/src/data-structures/linked-list/README.md @@ -4,7 +4,8 @@ _Read this in other languages:_ [_简体中文_](README.zh-CN.md), [_Русский_](README.ru-RU.md), [_日本語_](README.ja-JP.md), -[_Português_](README.pt-BR.md) +[_Português_](README.pt-BR.md), +[_한국어_](README.ko-KR.md) In computer science, a **linked list** is a linear collection of data elements, in which linear order is not given by