处理树形结构遍历的工具--Tree

/**
 * 处理树结构遍历工具
 * 所有遍历都是基于递归且深度优先
 */
export default class Tree<T> {
  private _value: T[] = [];

  private childrenKey: string = 'children';

  constructor(tree: T[], { childrenKey = 'children' }: { childrenKey?: string } = {}) {
    this._value = tree;
    this.childrenKey = childrenKey;
  }

  forEach(callback: (item: T, parent?: T) => void) {
    const stack: { node: T; parent?: T }[] = this._value.map(node => ({ node }));
    while (stack.length) {
      const { node, parent } = stack.shift()!;
      callback(node, parent);
      const children: T[] | undefined = (node as any)[this.childrenKey];
      if (children?.length)
        stack.unshift(...children.map(child => ({ node: child, parent: node })));
    }
    return this;
  }

  map<P>(callback: (item: T, parent?: P) => P, newChildrenKey: string = this.childrenKey) {
    const stack: { node: T; parent?: P }[] = this._value.map(node => ({ node }));
    const newTree: P[] = [];
    while (stack.length) {
      const { node, parent } = stack.shift()!;
      const newNode = callback(node, parent);
      if (!parent) newTree.push(newNode);
      else {
        (parent as any)[newChildrenKey] = (parent as any)[newChildrenKey] || [];
        (parent as any)[newChildrenKey].push(newNode);
      }
      const children: T[] | undefined = (node as any)[this.childrenKey];
      if (children?.length) {
        (newNode as any)[newChildrenKey] = [];
        stack.unshift(...children.map(child => ({ node: child, parent: newNode })));
      }
    }
    return new Tree(newTree, { childrenKey: newChildrenKey });
  }

  filter(callback: (item: T, parent?: T) => boolean) {
    const stack: { node: T; parent?: T }[] = this._value.map(node => ({ node }));
    const newTree: T[] = [];
    while (stack.length) {
      const { node, parent } = stack.shift()!;
      if (callback(node, parent)) {
        if (!parent) newTree.push(node);
        else {
          (parent as any)[this.childrenKey] = (parent as any)[this.childrenKey] || [];
          (parent as any)[this.childrenKey].push(node);
        }
      }
      const children: T[] | undefined = (node as any)[this.childrenKey];
      if (children?.length) {
        (node as any)[this.childrenKey] = [];
        stack.unshift(...children.map(child => ({ node: child, parent: node })));
      }
    }
    return new Tree(newTree, { childrenKey: this.childrenKey });
  }

  find(callback: (item: T, parent?: T) => boolean) {
    const stack: { node: T; parent?: T }[] = this._value.map(node => ({ node }));
    while (stack.length) {
      const { node, parent } = stack.shift()!;
      if (callback(node, parent)) return node;
      const children: T[] | undefined = (node as any)[this.childrenKey];
      if (children?.length)
        stack.unshift(...children.map(child => ({ node: child, parent: node })));
    }
  }

  findAll(callback: (item: T, parent?: T) => boolean) {
    const target: T[] = [];
    this.forEach(item => {
      if (callback(item)) target.push(item);
    });
    return new Tree(target, { childrenKey: this.childrenKey });
  }

  get value() {
    return this._value;
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

eno_zeng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值