π ΠΠΎΠΈΡΠΊ Π² ΡΠΈΡΠΈΠ½Ρ (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