01-树的JavaScript实现

1. 树结构

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

2. 深度优先遍历DFS

  • 访问根节点
  • 对根节点的children挨个进行深度优先遍历
const dfs = (root) => {
     console.log(root.val);
     root.children.forEach(dfs);
 }
 dfs(tree)
 >a
 >b
 >d
 >e
 >c
 >f
 >g

3. 广度优先遍历BFS

  • 新建一个队列,把根节点入队;
  • 把队头出队并访问;
  • 把队头的children挨个入队;
  • 重复第二、三步,直到队列为空
const bfs = (root) => {
    const q = [root];
    while (q.length) {
        const n = q.shift();
        console.log(n.val);
        n.children.forEach(child => {
            q.push(child);
        });
    }
}
bfs(tree);
>a
>b
>c
>d
>e
>f
>g

4. JavaScript中的二叉树

const binaryTree = {
    val: 1,
    left: {
        val: 2,
        left: null,
        right: null
    },
    right: {
        val: 3,
        left: null,
        right: null
    }
};

5. 二叉树的先序遍历

  • 访问根节点
  • 对根节点的左子树进行先序遍历
  • 对根节点的右子树进行先序遍历
// 树结构
const bt = {
    val: 1,
    left: {
        val: 2,
        left: {
            val: 4,
            left: null,
            right: null,
        },
        right: {
            val: 5,
            left: null,
            right: null,
        },
    },
    right: {
        val: 3,
        left: {
            val: 6,
            left: null,
            right: null,
        },
        right: {
            val: 7,
            left: null,
            right: null,
        },
    }
};

5-1. 先序遍历递归代码

// 先序遍历递归代码
const preorder = (root) => {
    if (!root) { return; }
    console.log(root.val);
    preorder(root.left);
    preorder(root.right);
}
preorder(bt);

5-2. 先序遍历非递归代码

// 先序遍历非递归代码
const preorder = (root) => {
    const stack = [];
    while(root || stack.length){
        while(root){
            console.log(root.val);
            stack.push(root);
            root = root.left;
        }
        if(stack.length){
            root = stack.pop();
            root = root.right;
        }
    }
}
// 输出
>1
>2
>4
>5
>3
>6
>7

6. 二叉树的中序遍历

  • 对根节点的左子树进行中序遍历
  • 访问根节点
  • 对根节点的右子树进行中序遍历
// 树结构
const bt = {
    val: 1,
    left: {
        val: 2,
        left: {
            val: 4,
            left: null,
            right: null,
        },
        right: {
            val: 5,
            left: null,
            right: null,
        },
    },
    right: {
        val: 3,
        left: {
            val: 6,
            left: null,
            right: null,
        },
        right: {
            val: 7,
            left: null,
            right: null,
        },
    }
};

6-1. 中序遍历递归代码

// 中序遍历递归代码
const inorder = (root) => {
    if (!root) { return; }
    inorder(root.left);
    console.log(root.val);
    inorder(root.right);
}
inorder(bt);

6-2. 中序遍历非递归代码

// 中序遍历非递归代码
const inorder = (root) => {
    const stack = [];
    while(root || stack.length){
        while(root){
            stack.push(root);
            root = root.left;
        }
        if(stack.length){
            root = stack.pop();
            console.log(root.val);
            root = root.right;
        }
    }
}
>4
>2
>5
>1
>6
>3
>7

7. 二叉树的后序遍历

  • 对根节点的左子树进行中序遍历
  • 对根节点的右子树进行中序遍历
  • 访问根节点
// 树结构
const bt = {
    val: 1,
    left: {
        val: 2,
        left: {
            val: 4,
            left: null,
            right: null,
        },
        right: {
            val: 5,
            left: null,
            right: null,
        },
    },
    right: {
        val: 3,
        left: {
            val: 6,
            left: null,
            right: null,
        },
        right: {
            val: 7,
            left: null,
            right: null,
        },
    }
};

7-1. 后续遍历递归代码

// 后序遍历递归代码
const postorder = (root) => {
    if (!root) { return; }
    postorder(root.left);
    postorder(root.right);
    console.log(root.val);
}
postorder(bt);

7-2. 后续遍历非递归代码

// 后续遍历非递归代码
const postorder = (root) => {
	if (!root) { return; }
    const stack = [root];
    const OutputStack = [];
    while (stack.length) {
        const node = stack.pop();
        OutputStack.push(node);
        if (node.left) {
            stack.push(node.left);
        }
        if (node.right) {
            stack.push(node.right);
        }
    }
    while (OutputStack.length) {
        const node = OutputStack.pop();
        console.log(node.val);
    }
}
>4
>5
>2
>6
>7
>3
>1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值