-
Notifications
You must be signed in to change notification settings - Fork 269
Add Suffix Tree #45
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
Add Suffix Tree #45
Changes from all commits
Commits
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
121 changes: 121 additions & 0 deletions
src/_DataStructures_/SuffixTree/index.js
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,121 @@ | ||
/* eslint-disable no-plusplus */ | ||
/* | ||
Implemented by watching this conceptually video: https://www.youtube.com/watch?v=VA9m_l6LpwI | ||
|
||
Suffix for banana are : | ||
banana | ||
anana | ||
nana | ||
ana | ||
na | ||
a | ||
|
||
Constructing a suffix tree is O(n*d) where d is length of max string | ||
|
||
Searching a suffix of a string is O(d) where d is length of suffix string. | ||
If found then return the index, else return -1 | ||
|
||
*/ | ||
|
||
class Node { | ||
constructor(value, isEnd, index) { | ||
this.data = value; | ||
this.isEnd = isEnd; | ||
this.index = index; | ||
this.next = new Map(); | ||
} | ||
} | ||
|
||
class SuffixTree { | ||
constructor(string) { | ||
this.head = new Node(); | ||
this.string = string; | ||
} | ||
|
||
constructSuffixTree() { | ||
const { string } = this; | ||
let currentString = ''; | ||
for (let i = string.length - 1; i >= 0; i -= 1) { | ||
currentString = string[i] + currentString; | ||
let j = 0; | ||
let currentNode = this.head; | ||
while (j < currentString.length) { | ||
if (!currentNode.next.has(currentString[j])) { | ||
currentNode.next.set(currentString[j], new Node(currentString, true, i)); | ||
break; | ||
} else { | ||
let k = 0; | ||
const partialMatchNode = currentNode.next.get(currentString[j]); | ||
const partialMatchString = partialMatchNode.data; | ||
|
||
let matchString = ''; | ||
while (k < partialMatchString.length && j < currentString.length && partialMatchString[k] === currentString[j]) { | ||
matchString += currentString[j]; | ||
k++; | ||
j++; | ||
} | ||
|
||
let diffString = ''; | ||
while (k < partialMatchString.length) { | ||
diffString += partialMatchString[k]; | ||
k++; | ||
} | ||
partialMatchNode.data = matchString; | ||
if (diffString) { | ||
partialMatchNode.next.set(diffString[0], new Node(diffString, true, partialMatchNode.index)); | ||
partialMatchNode.isEnd = false; | ||
partialMatchNode.index = null; | ||
} | ||
|
||
if (partialMatchNode.next.has(currentString[j])) { | ||
currentNode = partialMatchNode; | ||
} else { | ||
let nextString = ''; | ||
while (j < currentString.length) { | ||
nextString += currentString[j]; | ||
j++; | ||
} | ||
partialMatchNode.next.set(nextString[0], new Node(nextString, true, i)); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
findSubstring(string) { | ||
if (!this.head.next.has(string[0])) { | ||
return -1; | ||
} | ||
|
||
let currentNode = this.head.next.get(string[0]); | ||
let currentNodeValue = currentNode.data; | ||
|
||
let i = 0; let j = 0; | ||
|
||
while (i < string.length) { | ||
j = 0; | ||
while (i < string.length && j < currentNodeValue.length && string[i++] === currentNodeValue[j++]); | ||
|
||
if (i === string.length && j === currentNodeValue.length && currentNode.isEnd) { | ||
return currentNode.index; | ||
} | ||
|
||
if (currentNode.next.has(string[i])) { | ||
currentNode = currentNode.next.get(string[i]); | ||
currentNodeValue = currentNode.data; | ||
} else { | ||
return -1; | ||
} | ||
} | ||
return -1; | ||
} | ||
} | ||
|
||
// const s = new SuffixTree('banana'); | ||
// s.constructSuffixTree(); | ||
|
||
// console.log(s.findSubstring('nana')); | ||
|
||
|
||
module.exports = SuffixTree; |
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.