I need some help to create a javascript algorithm that builds a tree out of a word. The nodes of the tree are the letters of the word that are always in alphabetical order. Ex. 'balance' should be this object:
const tree = {
b: {
l: {
n: {}
},
n: {}
},
a: {
l: {
n: {
}
},
n: {
},
c: {
e: {
}
},
e: {
}
}
....
}
}
const asArray = a.split('')
const tree = {}
for (let i = 0; i < a.length; i++) {
const letter = array[i];
const greaterThan = asArray.filter((value, index) => {
return value > letter && index > i
})
tree[letter] = {}
for (let j = 0; j < greaterThan.length; j++) {
const gt = greaterThan[j];
tree[letter][gt] = {}
}
}
A javascript object, which the keys are the letters.