Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix Stack pop comlexity to be O(1) #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2018
Merged

Conversation

yavorski
Copy link
Contributor

  • By definition Stack push/pop time complexity should be O(1).
  • Fix is applied by removing head instead of tail in pop method.
  • Push method now do preprend instead of append.
  • Fix consistency between toString and toArray methods.

* By definition Stack push/pop time complexity should be O(1).
* Fix is applied by removing head instead of tail in pop method.
* Push method now do preprend instead of append.
* Fix consistency between toString and toArray methods.
@codecov
Copy link

codecov bot commented Sep 23, 2018

Codecov Report

Merging #214 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #214   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files         143    143           
  Lines        2555   2555           
  Branches      422    422           
=====================================
  Hits         2555   2555
Impacted Files Coverage Δ
src/data-structures/stack/Stack.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 45fb2a2...86660ec. Read the comment docs.

@yavorski
Copy link
Contributor Author

yavorski commented Sep 23, 2018

Leaving a small note here:

I noticed that pop method of the Stack is using deleteTail method of a LinkedList, which has a time complexity of O(n).

Due to this fact - I refactored it to use deleteHead, which has a complexity of O(1).
In addition to this push is now using prepend instead of append.

This commit also fixes the consistency of toArray vs toString methods of the Stack, which are printing the stack in different ways.

Best of luck,
Yavorski

@trekhleb
Copy link
Owner

@yavorski thank you for your refactoring! Good job!

@trekhleb trekhleb merged commit 9f3561d into trekhleb:master Sep 24, 2018
harshes53 pushed a commit to harshes53/javascript-algorithms that referenced this pull request Dec 6, 2018
* By definition Stack push/pop time complexity should be O(1).
* Fix is applied by removing head instead of tail in pop method.
* Push method now do preprend instead of append.
* Fix consistency between toString and toArray methods.
shoredata pushed a commit to shoredata/javascript-algorithms that referenced this pull request Mar 28, 2019
* By definition Stack push/pop time complexity should be O(1).
* Fix is applied by removing head instead of tail in pop method.
* Push method now do preprend instead of append.
* Fix consistency between toString and toArray methods.
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.

2 participants