Skip to content

Commit 28e1e01

Browse files
committed
--fix : suffix tree
1 parent d59cc09 commit 28e1e01

File tree

1 file changed

+30
-4
lines changed
  • src/_DataStructures_/Trees/SuffixTree

1 file changed

+30
-4
lines changed

src/_DataStructures_/Trees/SuffixTree/index.js

+30-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable max-len */
2+
/* eslint-disable no-restricted-syntax */
13
/* eslint-disable no-plusplus */
24
/*
35
Implemented by watching this conceptually video: https://www.youtube.com/watch?v=VA9m_l6LpwI
@@ -17,6 +19,7 @@ If found then return the index, else return -1
1719
1820
*/
1921

22+
const alphabets = 'abcdefghijklmnopqrstuvwxyz';
2023
class Node {
2124
constructor(value, isEnd, index) {
2225
this.data = value;
@@ -41,7 +44,12 @@ class SuffixTree {
4144
let currentNode = this.head;
4245
while (j < currentString.length) {
4346
if (!currentNode.next.has(currentString[j])) {
44-
currentNode.next.set(currentString[j], new Node(currentString, true, i));
47+
let nextString = '';
48+
while (j < currentString.length) {
49+
nextString += currentString[j];
50+
j++;
51+
}
52+
currentNode.next.set(nextString[0], new Node(nextString, true, i));
4553
break;
4654
} else {
4755
let k = 0;
@@ -60,9 +68,20 @@ class SuffixTree {
6068
diffString += partialMatchString[k];
6169
k++;
6270
}
71+
6372
partialMatchNode.data = matchString;
6473
if (diffString) {
65-
partialMatchNode.next.set(diffString[0], new Node(diffString, true, partialMatchNode.index));
74+
const oldMap = partialMatchNode.next;
75+
const newNode = new Node(diffString, partialMatchNode.isEnd, partialMatchNode.index);
76+
const alphabetsArray = alphabets.split('');
77+
78+
for (const char of alphabetsArray) {
79+
if (oldMap.has(char)) {
80+
newNode.next.set(char, oldMap.get(char));
81+
}
82+
}
83+
partialMatchNode.next = new Map();
84+
partialMatchNode.next.set(diffString[0], newNode);
6685
partialMatchNode.isEnd = false;
6786
partialMatchNode.index = null;
6887
}
@@ -112,10 +131,17 @@ class SuffixTree {
112131
}
113132
}
114133

115-
// const s = new SuffixTree('banana');
134+
// const st = 'asdjkxhcjbzdmnsjakdhasdbajw';
135+
// const s = new SuffixTree(st);
116136
// s.constructSuffixTree();
137+
// // console.log(s.head.next);
117138

118-
// console.log(s.findSubstring('nana'));
119139

140+
// for (let i = 0; i < st.length; i++) {
141+
// const e = st.substring(i);
142+
// if (s.findSubstring(e) !== i) {
143+
// console.log(e, i, s.findSubstring(e));
144+
// }
145+
// }
120146

121147
module.exports = SuffixTree;

0 commit comments

Comments
 (0)