数据结构——树

深度优先/广度优先遍历

深度优先:

  1. 访问根节点

  1. 对根节点的 children 挨个进行深度优先遍历

const tree = {
  val: "a",
  children: [
    {
      val: "b",
      children: [
        {
          val: "d",
          children: [],
        },
        {
          val: "e",
          children: [],
        },
      ],
    },
    {
      val: "c",
      children: [
        {
          val: "f",
          children: [],
        },
        {
          val: "g",
          children: [],
        },
      ],
    },
  ],
};

const dfs = (root) => {
  console.log(root.val);
  root.children.forEach((child) => {
    dfs(child);
  });
};

dfs(tree);

广度优先:

  1. 新建立一个队列,根节点入队

  1. 对头出队并访问

  1. 把对头的children挨个入队

  1. 重复2,3,直到队列为空

const tree = {
  val: "a",
  children: [
    {
      val: "b",
      children: [
        {
          val: "d",
          children: [],
        },
        {
          val: "e",
          children: [],
        },
      ],
    },
    {
      val: "c",
      children: [
        {
          val: "f",
          children: [],
        },
        {
          val: "g",
          children: [],
        },
      ],
    },
  ],
};

const bfs = (root) => {
  const q = [root];
  while (q.length > 0) {
    const n = q.shift();
    console.log(n.val);
    n.children.forEach((child) => {
      q.push(child);
    });
  }
};

bfs(tree);

二叉树的先中后序遍历

二叉树:每个节点最多只能有两个节点

先序遍历(根、左、右):

  1. 访问根节点

  1. 对根节点的左子树进行先序遍历

  1. 对根节点的右子树进行先序遍历

1、2、3、4、5、6、7

const tree = {
  val: "1",
  left: {
    val: "2",
    left: {
      val: "3",
      left: null,
      right: null,
    },
    right: {
      val: "4",
      left: {
        val: "5",
      },
      right: null,
    },
  },
  right: {
    val: "6",
    left: null,
    right: {
      val: "7",
      right: null,
      left: null,
    },
  },
};

const preorder = (root) => {
  if (!root) return;
  console.log(root.val);
  preorder(root.left);
  preorder(root.right);
};
preorder(tree);

中序遍历(左、中、右):

  1. 对根节点左子树遍历

  1. 访问根节点

  1. 对根节点右子树遍历

1、2、3、4、5、6、7

const tree = {
  val: "5",
  left: {
    val: "2",
    left: {
      val: "1",
      left: null,
      right: null,
    },
    right: {
      val: "4",
      left: {
        val: "3",
      },
      right: null,
    },
  },
  right: {
    val: "6",
    left: null,
    right: {
      val: "7",
      right: null,
      left: null,
    },
  },
};

const inorder = (root) => {
  if (!root) return;
  inorder(root.left);
  console.log(root.val);
  inorder(root.right);
};
inorder(tree);

后序遍历(左、右、根):

  1. 对根节点左子树遍历

  1. 对根节点右子树遍历

  1. 访问根节点

1、2、3、4、5、6、7

const tree = {
  val: "7",
  left: {
    val: "4",
    left: {
      val: "1",
      left: null,
      right: null,
    },
    right: {
      val: "3",
      left: {
        val: "2",
      },
      right: null,
    },
  },
  right: {
    val: "6",
    left: null,
    right: {
      val: "5",
      right: null,
      left: null,
    },
  },
};

const postorder = (root) => {
  if (!root) return;
  postorder(root.left);
  postorder(root.right);
  console.log(root.val);
};
postorder(tree);

非递归写法

递归调用函数,会不断的入栈出栈,所以考虑用栈实现。

先序遍历:

// 递归
const preorder = (root) => {
  if (!root) return;
  console.log(root.val);
  preorder(root.left);
  preorder(root.right);
};

// 非递归
const preorder = (root) => {
    if (!root) return;
    const stack = [root]
    while(stack.length){
        const n = stack.pop()
        console.log(n.val)
         // 栈后进先出,先右后左
        if(n.right) stack.push(n.right)
        if(n.left) stack.push(n.left)
    }
}

中序遍历:

// 递归
const inorder = (root) => {
  if (!root) return;
  inorder(root.left);
  console.log(root.val);
  inorder(root.right);
};
// 非递归
const inorder = (root) => {
  if (!root) return;
  const stack = [];
  let p = root;
  while (stack.length || p) {
    // 所有的左子树进栈
    while (p) {
      stack.push(p);
      p = p.left;
    }
    //最尽头的左子树出栈
    const n = stack.pop();
    console.log(n.val);
    p = n.right;
  }
};

后续遍历:

const postorder = (root) => {
  if (!root) return;
  postorder(root.left);
  postorder(root.right);
  console.log(root.val);
};
// 先序遍历是 根、左、右,后续遍历时:左、右、根,倒过来是根、右、左,只需要把先序遍历的后面两个颠倒顺序

const postorder = (root) => {
  if (!root) return;
  const outputStack = [];
  const stack = [root];
  while (stack.length) {
    const n = stack.pop();
    outputStack.push(n);
    if (n.left) stack.push(n.left);
    if (n.right) stack.push(n.right);
  }
  while (outputStack.length) {
    const n = outputStack.pop();
    console.log(n.val);
  }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值