Skip to content

Instantly share code, notes, and snippets.

@rubenhak
Last active January 17, 2023 21:19
Show Gist options
  • Save rubenhak/b12d83af8249c4bb325d1a7958333fb2 to your computer and use it in GitHub Desktop.
Save rubenhak/b12d83af8249c4bb325d1a7958333fb2 to your computer and use it in GitHub Desktop.
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