Skip to content

Instantly share code, notes, and snippets.

@alex-hladun
Last active June 11, 2020 16:52
Show Gist options
  • Save alex-hladun/b98783153c0b18f683d2bfc7c6426384 to your computer and use it in GitHub Desktop.
Save alex-hladun/b98783153c0b18f683d2bfc7c6426384 to your computer and use it in GitHub Desktop.
Uses tree data structure and methods to determine required information. Also uses recursion for tree traversal.
class Vampire {
constructor(name, yearConverted) {
this.name = name;
this.yearConverted = yearConverted;
this.offspring = [];
this.creator = null;
}
/** Simple tree methods **/
// Adds the vampire as an offspring of this vampire
addOffspring(vampire) {
this.offspring.push(vampire);
vampire.creator = this;
}
// Returns the total number of vampires created by that vampire
get numberOfOffspring() {
return this.offspring.length;
}
// Returns the number of vampires away from the original vampire this vampire is
get numberOfVampiresFromOriginal() {
let numOfVamps = 0;
let currentVamp = this;
while (currentVamp.creator) {
currentVamp = currentVamp.creator;
numOfVamps ++;
}
return numOfVamps;
}
// Returns true if this vampire is more senior than the other vampire. (Who is closer to the original vampire)
isMoreSeniorThan(vampire) {
return this.numberOfVampiresFromOriginal < vampire.numberOfVampiresFromOriginal
}
/** Tree traversal methods **/
// Returns the vampire object with that name, or null if no vampire exists with that name
vampireWithName(name) {
// console.log(name)
if (this.name === name) {
return this;
}
for (const offspring of this.offspring) {
if (offspring.vampireWithName(name) !== null)
return offspring.vampireWithName(name);
}
return null;
}
// Returns the total number of vampires that exist
get totalDescendents() {
let descendents = 0;
for (const offspring of this.offspring) {
descendents += 1;
let newDesc = offspring.totalDescendents;
descendents += newDesc;
}
return descendents;
}
// Returns an array of all the vampires that were converted after 1980
get allMillennialVampires() {
let milArray = [];
if (this.yearConverted > 1980) {
milArray.push(this);
}
for (const offspring of this.offspring) {
const newMils = offspring.allMillennialVampires;
milArray = milArray.concat(newMils);
}
return milArray;
}
/** Stretch **/
// Returns the closest common ancestor of two vampires.
// The closest common anscestor should be the more senior vampire if a direct ancestor is used.
// For example:
// * when comparing Ansel and Sarah, Ansel is the closest common anscestor.
// * when comparing Ansel and Andrew, Ansel is the closest common anscestor.
closestCommonAncestor(vampire) {
let vamp1Array = [this];
let currentVamp = this;
while (currentVamp.creator) {
currentVamp = currentVamp.creator;
vamp1Array.push(currentVamp);
}
currentVamp = vampire;
let vamp2Array = [vampire];
while (currentVamp.creator) {
currentVamp = currentVamp.creator;
vamp2Array.push(currentVamp);
}
for (const vamp of vamp1Array) {
for (const vamp2 of vamp2Array) {
if (vamp === vamp2) {
return vamp;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment