const itinerary = (tree, startPoint, endPoint) => {
const graph = buildGraph(tree);
const searchQueue = [];
searchQueue.push(startPoint);
const searched = [];
const path = {};
while(searchQueue.length > 0) {
const city = searchQueue.shift()
for(const neighbor of graph[city]) {
if(!searched.includes(neighbor)) {
path[neighbor] = city;
searchQueue.push(neighbor)
searched.push(neighbor)
if(neighbor === endPoint) return path;
}
}
};
};
console.log(itinerary(tree, 'Borisovka', 'Kurchatov'));
{
Belgorod: 'Borisovka',
Kursk: 'Belgorod',
Borisovka: 'Belgorod',
Voronezh: 'Kursk',
Kurchatov: 'Kursk'
}