{"size": 1397, "ext": "js", "max_stars_repo_path": "jirengu/course-projects/data-structure-1/src/tree.js", "max_stars_repo_name": "csdoker/learning", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "jirengu/course-projects/data-structure-1/src/tree.js", "max_issues_repo_name": "csdoker/learning", "max_issues_repo_issues_event_min_datetime": "2021-03-10T20:09:37.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-28T01:55:55.000Z", "max_forks_repo_path": "jirengu/course-projects/data-structure-1/src/tree.js", "max_forks_repo_name": "csdoker/learning", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 55, "max_forks_count": null, "avg_line_length": 19.4027777778, "max_line_length": 52, "alphanum_fraction": 0.5855404438}
const createTree = value => {
  return {
    data: value,
    children: null,
    parent: null
  };
};
const addChild = (node, value) => {
  const newNode = {
    data: value,
    children: null,
    parent: node
  };
  node.children = node.children || [];
  node.children.push(newNode);
  return newNode;
};
const travel = (tree, fn) => {
  fn(tree);
  if (!tree.children) {
    return;
  }
  for (let i = 0; i < tree.children.length; i++) {
    travel(tree.children[i], fn);
  }
};

const find = (tree, node) => {
  if (tree === node) {
    return tree;
  } else if (tree.children) {
    for (let i = 0; i < tree.children.length; i++) {
      const result = find(tree.children[i], node);
      if (result) {
        return result;
      }
    }
    return undefined;
  } else {
    return undefined;
  }
};

const removeNode = (tree, node) => {
  const siblings = node.parent.children;
  let index = 0;
  for (let i = 1; i < siblings.length; i++) {
    if (siblings[i] === node) {
      index = i;
    }
  }
  siblings.splice(index, 1);
};

const tree = createTree(10);
const node2 = addChild(tree, 20);
const node3 = addChild(tree, 30);
addChild(tree, 40);
const node5 = addChild(tree, 50);
addChild(node2, 201);
addChild(node2, 202);
addChild(node2, 203);
addChild(node2, 204);
console.log(tree);

const fn = node => {
  console.log(node.data);
};

removeNode(tree, node5);
console.log(tree);
