Add separate chaining to HashTable, add tests for HashTable, CircularLinkedList and Trie#256
Merged
Conversation
…LinkedList and Trie - Rewrite hash-table.ts with separate chaining collision handling: use Map<number, KeyValuePair<V>[]> as backing store; put creates/appends to chains and updates existing keys; get/remove traverse chains - Fix pre-existing bug in trie.ts remove: recursive deletion was pruning parent nodes that are word endpoints (e.g. removing 'card' would break search for 'car'); add isEndOfWord guard in #removeWord - Add src/08-dictionary-hash/__test__/hash-table.test.ts (16 tests) - Add src/06-linked-list/__test__/circular-linked-list.test.ts (16 tests) - Add src/12-trie/__test__/trie.test.ts (14 tests)
hash-table.ts is intentionally a basic (no collision handling) implementation for educational purposes. Collision handling is demonstrated separately in hash-table-separate-chaining.js and hash-table-linear-probing.js. Remove collision tests from hash-table.test.ts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Completes the remaining P1 items from the improvement plan.
HashTable — separate chaining collision handling
hash-table.tspreviously silently overwrote values when two keys hashed to the same bucket. Replaced with a proper separate chaining implementation:Map<number, KeyValuePair<V>[]>put: appends a new{key, value}to the bucket's chain, or updates the value if the key already existsget: traverses the chain at the bucket and returns the matching value, orundefinedremove: removes the matching pair from the chain; deletes the bucket if it becomes emptytoString: formats as{hash => [key: value, key: value, ...]}per bucketTrie — bug fix in
removePre-existing bug: removing a word that is an extension of another word (e.g.
'card') would incorrectly prune the shared-prefix node and breaksearch('car'). Fixed by adding an&& !node.isEndOfWordguard in#removeWordso intermediate nodes that are themselves word endpoints are never deleted.New test files (10 suites, 123 tests total)
08-dictionary-hash/__test__/hash-table.test.ts06-linked-list/__test__/circular-linked-list.test.ts12-trie/__test__/trie.test.ts