JavaScript算法学习-树

//分层数据抽象

//JS 中没有树,用Array和Object构建树

//DOM,级联选择,树形控件

// 深度优先遍历:访问根节点、对根节点进行深度优先遍历,广度优先遍历

bfs
//bfs
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);

bt
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,
        },
    },
};

module.exports = bt;
dfs
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(dfs);
};

dfs(tree);
inorder
const bt = require('./bt');

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;
//     }
// };

inorder(bt);
json
//遍历JSON所有节点
const json = {
    a: { b: { c: 1}},
    d:[1,2],
};
//帮不合理的后端JSON传值擦屁股
const dfs = (n, path) => {
    console.log(n, path);
    Object.keys(n).forEach(k =>{
        dfs(n[k],path.concat(k));
    });
};

dfs(json, []);
postOrder
const bt = require('./bt');

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);
//     }
// };

postorder(bt);
preorder
const bt = require('./bt');

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);
//     }
// };

preorder(bt);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
学习JavaScript算法非常重要,因为它是一种用于解决问题的编程语言。通过学习JavaScript算法,我们可以更加了解数据结构、算法和编程逻辑。 首先,我们需要掌握JavaScript基本语法和语法规则。了解如何声明变量、使用条件语句、循环语句和函数等是非常重要的。这些知识将为我们编写算法提供基础。 其次,我们需要了解常见的算法和数据结构。学习排序算法(如冒泡排序、快速排序)、搜索算法(如二叉搜索、哈希表)以及链表、队列和栈等数据结构。这些算法和数据结构是解决问题的重要工具。 在学习算法时,我们要注重正确性和效率。我们应该注意算法的时间复杂度和空间复杂度,力求编写出高效的算法。此外,我们还可以学习一些优化技巧,如贪心算法和动态规划,以提高算法的效率。 除了学习传统的算法和数据结构之外,我们还可以学习一些JavaScript特定的算法。例如,学习如何使用递归、闭包和高阶函数来解决问题。这些JavaScript特性可以帮助我们编写更加简洁和灵活的算法。 最后,为了加深对算法的理解和应用,我们可以尝试解决一些经典的算法问题。这些问题可以帮助我们锻炼思维能力和编程技巧。解决这些问题将使我们能够更好地应对实际中的问题。 总之,学习JavaScript算法是程序员的必备技能之一。通过掌握基本语法、常见算法和数据结构,以及考虑正确性和效率,我们将能够写出高质量的JavaScript代码,并更好地解决问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值