Last active
January 17, 2023 21:19
-
-
Save rubenhak/b12d83af8249c4bb325d1a7958333fb2 to your computer and use it in GitHub Desktop.
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
class Node | |
{ | |
constructor(val) | |
{ | |
this.value = val; | |
this.children = new Map(); | |
} | |
insert(str) | |
{ | |
if (str.length === 0) { | |
return; | |
} | |
const val = str.charAt(0); | |
if (!this.children.has(val)) | |
{ | |
this.children.set(val, new Node(val)); | |
} | |
this.children.get(val).insert(str.substring(1)); | |
} | |
find(str) | |
{ | |
if (str.length == 0) { | |
return true; | |
} | |
const nextNode = this.children.get(str.charAt(0)); | |
if (!nextNode) { | |
return false; | |
} | |
return nextNode.find(str.substring(1)) + 1; | |
} | |
print(index) | |
{ | |
if (!index) { | |
console.log(`------------------------`); | |
index = 0; | |
} | |
console.log(`${' '.repeat(index + 1)} + ${this.value}`); | |
for(const x of this.children.values()) | |
{ | |
x.print(index + 1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment