Created
January 17, 2024 10:11
-
-
Save loia5tqd001/646c55b6f84eb02449882844f53d5bac to your computer and use it in GitHub Desktop.
This file contains 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
// https://www.typescriptlang.org/play?#code/C4TwDgpgBAIglgJwgY2AewSKBeKBvAKCmKgG0BrCEALigGdgE4A7AcwF1b4lUMsAfKMwCuAG1EBuAgF8CBZGmYMoLMMOA4yREgHIA9AAs0AWwh6AhmDAWrARh0AabcX1HTN65bAAmR86iuJmZeHgDMfiQBhkF66nCidLHA8fZOkYHucQlJ8b5putHuIV4ALI5QenpQEAAmrNDI5nQQtMAG0KrqKnRCaBp0GMC1UCAQwP4ZZiw1EAAeAHQAVnR+7FIElVBt0IwQ0HRGYjVQomho5CdwlFsGcHTyiso1iFyIKOiYmoSRbi34-pEvLRvpFQcQvLZaCJxPkwYCrN4oWJRLC4eCrKEkTCAXDSliUTjiNJUZEsnRgYTQVlIUJkSS4VlEbTsWjiZSdNM5ksVvj-LJZAQAGbCZioOCKKAAI2E8Rq3HefAAKkgIAAKMDmNrk+iMFisUjsACUrx4HywIOICiUGl2f3lvE+uDw0nWkQ1WvmgowAFFzMgDKr1ZqDIacAA+f5oq3KDUIYA9XDugzzOiiODINW2Q0psBp4Cq-Q6Q2uuGiMZQZDCBBIZjAAByaBmmltJbBsfjnp9foDQbjoewEYtaJUgqgqoAhJXqxBaw2ZqR20bI8OwVOa-XGxAF+Y4+wvi7KZFZCvLVX13PoLg1zON-PF1AmrA3g6QFJWcW+R--EhgFXmFsVSkAVhVFZIJS9BBjE1e0zWVPZVVtE0FUwBwTggAA3CBRCRYxJQgBBNAABmNHUmDYZdiDLDQkDoMQNFwHQdFbKAILHaMNEoLAWAAvZQyHSJ2JUZgZlrTQdAAWh0eYkEgTVVTLTDRA-NEaLoqAAGpcAAAwAEjwTla2kcS9M46QAB1mC05iBMeDR-VlC9mxVCgqDWSk4FHVV7NEGoLz4w8SFU0QNE0liMCg4AYKVFUvNuHyL1QhSsI0qAszfOFjxIAVIh-P8oCC4AgLkQTnlNaK9k0aVZSizA4LVTpgA-QSIIioYagAeXUNR6LCyDoOfWCYtK5CQDqprHjQMt5lOVgCwsnQUpazU2s64BuuLIA | |
type Directory = { | |
[key: string]: Directory | null; | |
} | |
const input = [ | |
'/home/app/app1', | |
'/home/app/app2', | |
'/home/app/app3', | |
'/home/utils/util1', | |
'/home/utils/util2', | |
'/home/app/app4', // edge case: the input is not sorted yet | |
'/home/index.js', | |
]; | |
// the tree should look like this | |
const dir: Directory = { | |
home: { | |
app: { | |
app1: null, | |
app2: null, | |
app3: null, | |
app4: null, | |
}, | |
utils: { | |
util1: null, | |
util2: null, | |
}, | |
'index.js': null | |
} | |
} | |
function buildDirectoryTree(paths: string[]): Directory { | |
const tree: Directory = {}; | |
paths.forEach((path) => { | |
const parts = path.slice(1).split('/'); | |
let currentNode = tree; | |
parts.forEach((part) => { | |
if (!currentNode[part]) { | |
currentNode[part] = {}; | |
} | |
currentNode = currentNode[part] as Directory; | |
}); | |
}); | |
return tree; | |
} | |
function formatDirectoryTree(tree: Directory, level: number = 0): string { | |
let result = ''; | |
for (const key in tree) { | |
const indent = '-'.repeat(level); | |
result += `${indent}-${key}\n`; | |
const childNode = tree[key]; | |
if (childNode) { | |
result += formatDirectoryTree(childNode, level + 1); | |
} | |
} | |
return result; | |
} | |
const directoryTree = buildDirectoryTree(input); | |
const formattedOutput = formatDirectoryTree(directoryTree); | |
console.log('\n' + formattedOutput); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://en.wikipedia.org/wiki/Trie