-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrees.js
52 lines (37 loc) · 1.14 KB
/
trees.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// console.log("this should work")
// Define a Node class to represent each member of the family
class Node {
constructor(name){
this.name = name;
this.children = [];
}
//add children function
addChildren(Childname){
this.children.push(Childname)
}
// Create the family tree
}
const familyTree = new Node("Grandpa");
const dad = new Node("Dad");
const mom = new Node("Mom");
familyTree.addChildren(dad);
familyTree.addChildren(mom);
const child1 = new Node("Judy");
const child2 = new Node("James");
const child3 = new Node("Joyce");
dad.addChildren(child1);
dad.addChildren(child2);
mom.addChildren(child3);
console.log(familyTree)
// Function to print the family tree recursively
function printFamilyTree(node, level = 0) {
const indentation = " ".repeat(level); // Add spaces for indentation
console.log(`${indentation}- ${node.name}`);
for (const child of node.children) {
printFamilyTree(child, level + 1);
}
}
// // Print the family tree
console.log("Family Tree:");
printFamilyTree(familyTree);
// console.log(familyTree)