0
0
DSA Javascriptprogramming~30 mins

Why Trees Exist and What Linked Lists and Arrays Cannot Do in DSA Javascript - See It Work

Choose your learning style9 modes available
Why Trees Exist and What Linked Lists and Arrays Cannot Do
📖 Scenario: Imagine you are organizing a family reunion. You want to keep track of family members and their relationships, like parents and children. Using simple lists or arrays makes it hard to show these connections clearly. Trees help us organize such data naturally.
🎯 Goal: You will build a simple tree structure using JavaScript objects to represent family members and their children. This will show why trees are useful when linked lists and arrays cannot easily represent hierarchical relationships.
📋 What You'll Learn
Create a JavaScript object called familyTree representing a root family member with children.
Add a variable called rootName to store the root member's name.
Write a function called getChildrenNames that returns an array of the root member's children names.
Print the root member's name and the list of children names.
💡 Why This Matters
🌍 Real World
Trees are used to represent data with hierarchy like file systems, organization charts, and family trees.
💼 Career
Understanding trees is essential for software development roles involving data organization, search algorithms, and UI component trees.
Progress0 / 4 steps
1
Create the family tree object
Create a JavaScript object called familyTree with a name property set to 'Grandparent' and a children property which is an array containing two objects. The first child object should have name set to 'Parent1' and an empty children array. The second child object should have name set to 'Parent2' and an empty children array.
DSA Javascript
Hint

Use nested objects and arrays to represent the family tree structure.

2
Add a variable for the root member's name
Create a variable called rootName and set it to the name property of the familyTree object.
DSA Javascript
Hint

Access the name property of the familyTree object and assign it to rootName.

3
Write a function to get children names
Write a function called getChildrenNames that takes the familyTree object as a parameter and returns an array of the name properties of its children.
DSA Javascript
Hint

Use the map method on the children array to get their names.

4
Print the root name and children names
Print the rootName variable and then print the result of calling getChildrenNames(familyTree).
DSA Javascript
Hint

Use console.log to print the values.