Skip to main content
Loading...

More JavaScript Posts

class TreeNode {
  constructor(key, value = key, parent = null) {
    this.key = key;
    this.value = value;
    this.parent = parent;
    this.children = [];
  }

  get isLeaf() {
    return this.children.length === 0;
  }

  get hasChildren() {
    return !this.isLeaf;
  }
}

class Tree {
  constructor(key, value = key) {
    this.root = new TreeNode(key, value);
  }

  *preOrderTraversal(node = this.root) {
    yield node;
    if (node.children.length) {
      for (let child of node.children) {
        yield* this.preOrderTraversal(child);
      }
    }
  }

  *postOrderTraversal(node = this.root) {
    if (node.children.length) {
      for (let child of node.children) {
        yield* this.postOrderTraversal(child);
      }
    }
    yield node;
  }

  insert(parentNodeKey, key, value = key) {
    for (let node of this.preOrderTraversal()) {
      if (node.key === parentNodeKey) {
        node.children.push(new TreeNode(key, value, node));
        return true;
      }
    }
    return false;
  }

  remove(key) {
    for (let node of this.preOrderTraversal()) {
      const filtered = node.children.filter(c => c.key !== key);
      if (filtered.length !== node.children.length) {
        node.children = filtered;
        return true;
      }
    }
    return false;
  }

  find(key) {
    for (let node of this.preOrderTraversal()) {
      if (node.key === key) return node;
    }
    return undefined;
  }
}

const tree = new Tree(1, 'AB');

tree.insert(1, 11, 'AC');
tree.insert(1, 12, 'BC');
tree.insert(12, 121, 'BG');

[...tree.preOrderTraversal()].map(x => x.value);
// ['AB', 'AC', 'BC', 'BCG']

tree.root.value;              // 'AB'
tree.root.hasChildren;        // true

tree.find(12).isLeaf;         // false
tree.find(121).isLeaf;        // true
tree.find(121).parent.value;  // 'BC'

tree.remove(12);

[...tree.postOrderTraversal()].map(x => x.value);
// ['AC', 'AB']
results =
{
 "11345240":"Media Comment-80e47004-1719-493a-af69-a43b2652b41c",
 "11345282":"zoom_3_22_21.mp4",
 "11345303":"Recording-2.m4a",
 "11345348":"February 12%2C 2021.mp4",
 "11345826":"Lecture_Rousso_Chapter_06-2.pptx",
 "11345933":"Themes_InClassAssigment.pptx",
 "11346193":"Agenda – Week of March23.pptx",
 "11346318":"MUJS 2370 SP2021 Updated Rhythm Changes Drills Bb - Score.pdf",
 "11346319":"MUJS 2370 SP2021 Updated Rhythm Changes Drills C - Score.pdf",
 "11346322":"MUJS 2370 SP2021 Updated Rhythm Changes Drills Eb - Score.pdf",
 "11346323":"MUJS 2370 Updated Rhythm Changes Drills Bass - Score.pdf",
 "11346381":"Anthropology with colored highlighter analysis - Mar 22 2021 - 10-36 AM.pdf",
 "11346449":"Anthropology with colored highlighter analysis - Mar 22 2021 - 10-36 AM.pdf",
 "11346450":"MUJS 2370 SP2021 Updated Rhythm Changes Drills Bb - Score.pdf",
 "11346451":"MUJS 2370 SP2021 Updated Rhythm Changes Drills C - Score.pdf",
 "11346453":"MUJS 2370 SP2021 Updated Rhythm Changes Drills Eb - Score.pdf",
 "11346454":"MUJS 2370 Updated Rhythm Changes Drills Bass - Score.pdf",
 "11346495":"Steinel chromatic ornamentation étude rhythm changes - Aug 23 2020 - 12-19 PM.pdf",
 "11346532":"Media Comment-136242b4-7f2c-4d5d-8aa4-091f07e797cf",
 "11346581":"2019 Trust Based Relationship Intervention Manual.pdf",
 "11346584":"TBRI ppt.pptx",
 "11346593":"Trauma informed care.ppt"
}

function NewTab(testing) {
  window.open(testing, "_blank");
}

for(test in results) {
  var testing = 'https://unt.instructure.com/files/' + test + '/download';
  NewTab(testing);
  //window.open = 'https://unt.instructure.com/files/' + test + '/download';
}