data_structure

This is an old revision of the document!


Tree

export function inorderTraversal<T>(
  root: node<T>,
  fn: (n: T) => boolean,
  newTree?: node<T>
) {
  newTree = {
    value: root.value,
    children: [],
  };
  console.log(root.value);
  if (root.children) {
    for (const n of root.children) {
      if (fn(root.value)) {
        if (fn(n.value)) {
          newTree.children!.push(inorderTraversal(n, fn, newTree, newTree));
        } else {
          for (const nn of n.children) {
            newTree.children!.push(inorderTraversal(nn, fn, newTree, newTree));
          }
        }
      }
    }
  }

  return newTree;
}

  • data_structure.1716728627.txt.gz
  • Last modified: 2024/05/26 13:03
  • by root1