Skip to content

Add separate chaining to HashTable, add tests for HashTable, CircularLinkedList and Trie#256

Merged
loiane merged 4 commits into
mainfrom
loiane/p1-hash-and-tests
Jul 1, 2026
Merged

Add separate chaining to HashTable, add tests for HashTable, CircularLinkedList and Trie#256
loiane merged 4 commits into
mainfrom
loiane/p1-hash-and-tests

Conversation

@loiane

@loiane loiane commented Jul 1, 2026

Copy link
Copy Markdown
Owner

Completes the remaining P1 items from the improvement plan.

HashTable — separate chaining collision handling

hash-table.ts previously silently overwrote values when two keys hashed to the same bucket. Replaced with a proper separate chaining implementation:

  • Backing store changed from a sparse array to Map<number, KeyValuePair<V>[]>
  • put: appends a new {key, value} to the bucket's chain, or updates the value if the key already exists
  • get: traverses the chain at the bucket and returns the matching value, or undefined
  • remove: removes the matching pair from the chain; deletes the bucket if it becomes empty
  • toString: formats as {hash => [key: value, key: value, ...]} per bucket

Trie — bug fix in remove

Pre-existing bug: removing a word that is an extension of another word (e.g. 'card') would incorrectly prune the shared-prefix node and break search('car'). Fixed by adding an && !node.isEndOfWord guard in #removeWord so intermediate nodes that are themselves word endpoints are never deleted.

New test files (10 suites, 123 tests total)

File Tests What's covered
08-dictionary-hash/__test__/hash-table.test.ts 16 put/get/remove, collision handling, key update, toString
06-linked-list/__test__/circular-linked-list.test.ts 16 append/prepend/insert/remove/indexOf/clear/reverse, circular structure validation
12-trie/__test__/trie.test.ts 14 insert/search/startsWith/remove, prefix edge cases, shared-prefix removal

loiane added 4 commits June 30, 2026 20:46
…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.
@loiane loiane merged commit cebf338 into main Jul 1, 2026
1 check passed
@loiane loiane deleted the loiane/p1-hash-and-tests branch July 2, 2026 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant