<script>
let CHILDREN = 26;
let MAX = 100;
class trie
{
constructor() {
}
}
function
createNode(endOfWord)
{
let temp =
new
trie();
temp.child =
new
trie(CHILDREN);
temp.endOfWord = endOfWord;
for
(let i = 0; i < CHILDREN; i++)
{
temp.child[i] =
null
;
}
return
temp;
}
function
insertRecursively(itr, str, i)
{
if
(i < str.length)
{
let index = str[i].charCodeAt() -
'a'
.charCodeAt();
if
(itr.child[index] ==
null
)
{
itr.child[index] = createNode(
false
);
}
insertRecursively(itr.child[index], str, i + 1);
}
else
{
itr.endOfWord =
true
;
}
}
function
insert(itr, str)
{
insertRecursively(itr, str, 0);
}
function
isLeafNode(root)
{
return
root.endOfWord !=
false
;
}
function
display(root, itr, str, level)
{
let currChild = 0;
for
(let i = 0; i < CHILDREN; i++)
{
if
(itr.child[i] !=
null
)
{
str[level] = String.fromCharCode(i +
'a'
.charCodeAt());
display(root, itr.child[i], str, level + 1);
currChild++;
}
}
if
(currChild > 1 && itr != root)
{
document.write(str[level - 1] +
"</br>"
);
}
}
function
displayJoint(root)
{
let level = 0;
let str =
new
Array(MAX);
display(root, root, str, level);
}
let root = createNode(
false
);
let s = [
"geek"
,
"geeky"
,
"geeks"
,
"gel"
];
for
(let str = 0; str < s.length; str++)
{
insert(root, s[str]);
}
displayJoint(root);
</script>