|
| 1 | +/* |
| 2 | + * EJERCICIO: |
| 3 | + * ¡La Casa del Dragón ha finalizado y no volverá hasta 2026! |
| 4 | + * ¿Alguien se entera de todas las relaciones de parentesco |
| 5 | + * entre personajes que aparecen en la saga? |
| 6 | + * Desarrolla un árbol genealógico para relacionarlos (o invéntalo). |
| 7 | + * Requisitos: |
| 8 | + * 1. Estará formado por personas con las siguientes propiedades: |
| 9 | + * - Identificador único (obligatorio) |
| 10 | + * - Nombre (obligatorio) |
| 11 | + * - Pareja (opcional) |
| 12 | + * - Hijos (opcional) |
| 13 | + * 2. Una persona sólo puede tener una pareja (para simplificarlo). |
| 14 | + * 3. Las relaciones deben validarse dentro de lo posible. |
| 15 | + * Ejemplo: Un hijo no puede tener tres padres. |
| 16 | + * Acciones: |
| 17 | + * 1. Crea un programa que permita crear y modificar el árbol. |
| 18 | + * - Añadir y eliminar personas |
| 19 | + * - Modificar pareja e hijos |
| 20 | + * 2. Podrás imprimir el árbol (de la manera que consideres). |
| 21 | + * |
| 22 | + * NOTA: Ten en cuenta que la complejidad puede ser alta si |
| 23 | + * se implementan todas las posibles relaciones. Intenta marcar |
| 24 | + * tus propias reglas y límites para que te resulte asumible. |
| 25 | +*/ |
| 26 | + |
| 27 | +class Person { |
| 28 | + constructor(id, name) { |
| 29 | + this.id = id; |
| 30 | + this.name = name; |
| 31 | + this.partner = null; |
| 32 | + this.children = []; |
| 33 | + this.has_parents = false; |
| 34 | + } |
| 35 | + |
| 36 | + addPartner(partner) { |
| 37 | + if (this.partner !== null) { |
| 38 | + console.log(`${this.name} ya tiene pareja: ${this.partner.name}.`); |
| 39 | + } else { |
| 40 | + this.partner = partner; |
| 41 | + partner.partner = this; |
| 42 | + |
| 43 | + console.log(`${this.name} es pareja de ${partner.name}.`); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + addChild(child) { |
| 48 | + if (!this.children.includes(child)) { |
| 49 | + this.children.push(child); |
| 50 | + |
| 51 | + console.log(`${child.name} es hijo/a de ${this.name}`); |
| 52 | + } else { |
| 53 | + console.log(`${child.name} es hijo/a de ${this.name}.`); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +class FamilyTree { |
| 59 | + constructor() { |
| 60 | + this.people = {}; |
| 61 | + } |
| 62 | + |
| 63 | + addPerson(id, name) { |
| 64 | + if (id in this.people) { |
| 65 | + console.log(`La persona con ID: ${id} ya existe.`); |
| 66 | + } else { |
| 67 | + const person = new Person(id, name); |
| 68 | + |
| 69 | + this.people[id] = person; |
| 70 | + |
| 71 | + console.log(`${name} (ID: ${id}) se agregó al árbol.`); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + removePerson(id) { |
| 76 | + if (id in this.people) { |
| 77 | + const person = this.people[id]; |
| 78 | + |
| 79 | + delete this.people[id]; |
| 80 | + |
| 81 | + console.log(`${person.name} (ID: ${id}) se eliminó del árbol.`); |
| 82 | + } else { |
| 83 | + console.log(`La persona con ID: ${id} no existe en el árbol.`); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + setPartner(id1, id2) { |
| 88 | + if (id1 in this.people && id2 in this.people) { |
| 89 | + const person1 = this.people[id1]; |
| 90 | + const person2 = this.people[id2]; |
| 91 | + |
| 92 | + person1.addPartner(person2); |
| 93 | + } else { |
| 94 | + console.log("Algún ID no existe en el árbol."); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + addChild(parent_id, child_id) { |
| 99 | + if (parent_id in this.people && child_id in this.people) { |
| 100 | + if (parent_id === child_id) { |
| 101 | + console.log("Los ID no pueden ser iguales a la hora de asignar un hijo."); |
| 102 | + } else { |
| 103 | + const parent = this.people[parent_id]; |
| 104 | + |
| 105 | + if (parent.partner === null) { |
| 106 | + console.log("Se necesita una pareja para poder tener un hijo."); |
| 107 | + } else { |
| 108 | + const child = this.people[child_id]; |
| 109 | + |
| 110 | + if (child.has_parents) { |
| 111 | + console.log(`${child.name} (ID: ${child.id}) ya tiene padres.`); |
| 112 | + } else { |
| 113 | + child.has_parents = true; |
| 114 | + parent.addChild(child); |
| 115 | + parent.partner.addChild(child); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } else { |
| 120 | + console.log("Algún ID no existe en el árbol."); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + printTree() { |
| 125 | + const visited = new Set(); |
| 126 | + const printPerson = (person, level = 0) => { |
| 127 | + if (visited.has(person.id)) { |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + visited.add(person.id); |
| 132 | + |
| 133 | + const indent = "\t".repeat(level); |
| 134 | + |
| 135 | + console.log(`${indent} - ${person.name} (ID: ${person.id})`); |
| 136 | + |
| 137 | + if (person.partner) { |
| 138 | + visited.add(person.partner.id); |
| 139 | + |
| 140 | + console.log(`${indent} Pareja: ${person.partner.name} (ID: ${person.partner.id})`); |
| 141 | + } |
| 142 | + |
| 143 | + if (person.children.length > 0) { |
| 144 | + console.log(`${indent} Hijos:`); |
| 145 | + |
| 146 | + for (const child of person.children) { |
| 147 | + printPerson(child, level + 1); |
| 148 | + } |
| 149 | + } |
| 150 | + }; |
| 151 | + |
| 152 | + for (const person of Object.values(this.people)) { |
| 153 | + const is_child = person.has_parents; |
| 154 | + |
| 155 | + if (!is_child) { |
| 156 | + printPerson(person); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +let tree = new FamilyTree(); |
| 163 | + |
| 164 | +tree.addPerson(0, "Raditz"); |
| 165 | +tree.addPerson(1, "Goku"); |
| 166 | +tree.addPerson(2, "Milk"); |
| 167 | +tree.addPerson(3, "Gohan"); |
| 168 | +tree.addPerson(4, "Goten"); |
| 169 | +tree.addPerson(5, "Videl"); |
| 170 | +tree.addPerson(6, "Pan"); |
| 171 | + |
| 172 | +tree.removePerson(0); |
| 173 | + |
| 174 | +tree.setPartner(1, 2); |
| 175 | +tree.setPartner(3, 5); |
| 176 | + |
| 177 | +tree.addChild(1, 3); |
| 178 | +tree.addChild(1, 4); |
| 179 | +tree.addChild(3, 6); |
| 180 | + |
| 181 | +tree.printTree(); |
0 commit comments