ΠŸΠ΅Ρ€Π΅ΠΉΡ‚ΠΈ ΠΊ основному содСрТимому

πŸ“„ Поиск Π² ΡˆΠΈΡ€ΠΈΠ½Ρƒ (BFS)

ОписаниС задачи​

Алгоритм поиска Π² ΡˆΠΈΡ€ΠΈΠ½Ρƒ

Π˜ΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠΉΡ‚Π΅ Breadth-First Search для ΠΎΠ±Ρ…ΠΎΠ΄Π° Π³ΠΎΡ€ΠΎΠ΄ΠΎΠ² англосаксонской Π³Π΅ΠΏΡ‚Π°Ρ€Ρ…ΠΈΠΈ Π² Англии.

const heptarchyTree = {
value: 'England',
children: [
{
value: 'Northumbria',
children: [
{
value: 'Bamburgh',
children: [
{
value: 'Yeavering',
children: [],
},
],
},
{
value: 'Lindisfarne',
children: [],
},
],
},
{
value: 'Mercia',
children: [
{
value: 'Tamworth',
children: [],
},
{
value: 'Repton',
children: [],
},
],
},
],
};

function travelThroughHeptarchy(heptarchyTree) {
const scroll = []; // Use a scroll to record the order of visited locations
scroll.push(heptarchyTree); // Add England as the starting point to the scroll

// While there are still towns on the scroll, continue traveling
while (scroll.length > 0) {
const kingdom = scroll.shift(); // Take the next kingdom or town from the scroll
console.log(kingdom.value); // Record the name of the visited kingdom or town

// Visit all sub-regions of the current kingdom or town, adding them to the scroll
for (const child of kingdom.children) {
scroll.push(child);
}
}
}

travelThroughHeptarchy(heptarchyTree); // Begin traveling through the towns of the Anglo-Saxon Heptarchy