forked from CodeYourFuture/Module-Complexity
-
Notifications
You must be signed in to change notification settings - Fork 0
Implement skip list #4
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
Open
AliQassab
wants to merge
2
commits into
main
Choose a base branch
from
implement-skip-list
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import random | ||
|
|
||
|
|
||
| class SkipListNode: | ||
| """Node in a skip list with multiple forward pointers""" | ||
| def __init__(self, value, levels): | ||
| self.value = value | ||
| self.forward = [None] * levels | ||
|
|
||
|
|
||
| class SkipList: | ||
| def __init__(self): | ||
| self.max_level = 16 | ||
| self.head = SkipListNode(float('-inf'), self.max_level) | ||
| self.level = 0 | ||
|
|
||
| def _random_level(self): | ||
| """Generate random level for a new node (coin flip)""" | ||
| level = 0 | ||
| while random.random() < 0.5 and level < self.max_level - 1: | ||
| level += 1 | ||
| return level | ||
|
|
||
| def insert(self, value): | ||
| """Insert value in sorted order - O(log n) time complexity""" | ||
| update = [None] * self.max_level | ||
| current = self.head | ||
|
|
||
| for i in range(self.level, -1, -1): | ||
| while current.forward[i] and current.forward[i].value < value: | ||
| current = current.forward[i] | ||
| update[i] = current | ||
|
|
||
| current = current.forward[0] | ||
| new_level = self._random_level() | ||
|
|
||
| if new_level > self.level: | ||
| for i in range(self.level + 1, new_level + 1): | ||
| update[i] = self.head | ||
| self.level = new_level | ||
|
|
||
| new_node = SkipListNode(value, new_level + 1) | ||
|
|
||
| for i in range(new_level + 1): | ||
| new_node.forward[i] = update[i].forward[i] | ||
| update[i].forward[i] = new_node | ||
|
|
||
| def contains(self, value) -> bool: | ||
| """Check if value exists - O(log n) time complexity""" | ||
| current = self.head | ||
|
|
||
| for i in range(self.level, -1, -1): | ||
| while current.forward[i] and current.forward[i].value < value: | ||
| current = current.forward[i] | ||
|
|
||
| current = current.forward[0] | ||
| return current is not None and current.value == value | ||
|
|
||
| def to_list(self) -> list: | ||
| """Convert to sorted list - O(n) time complexity""" | ||
| result = [] | ||
| current = self.head.forward[0] | ||
| while current: | ||
| result.append(current.value) | ||
| current = current.forward[0] | ||
| return result | ||
|
|
||
| def __contains__(self, value) -> bool: | ||
| return self.contains(value) | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line reassigns
currenttocurrent.forward[0], but the variable is never read again — making it dead code. It looks like a leftover from a duplicate-check pattern. Since the assignment doesn't require uniqueness you can safely remove this line, or if you do want to guard against duplicates you could add:Either way, leaving an unused variable assignment makes the code harder to read and may mislead future readers into thinking the value is checked.