DFS普通递归实现

<html>

<head>
    <!-- <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> -->
    <style>
    </style>
</head>
<div id="onMouseover"></div>

<body>
    <script>
        const tree = [{
            name: 'root',
            children: [
                {
                    name: 'c1',
                    children: [
                        {
                            name: 'c11',
                            children: []
                        },
                        {
                            name: 'c12',
                            children: []
                        }
                    ]
                },
                {
                    name: 'c2',
                    children: [
                        {
                            name: 'c21',
                            children: []
                        },
                        {
                            name: 'c22',
                            children: []
                        }
                    ]
                }
            ]
        }]

        // 深度优先的方式遍历 打印 name
        // ['root', 'c1','c11', 'c12', 'c2', 'c21', 'c22']

        let arr = []
        function DFS(item) {
            let getLength = item.length
            if (getLength < 1 && !item.name) {
                return arr
            }
            for (let i = 0; i < getLength; i++) {
                arr.push(item[i].name)
                if (item[i].children.length > 0) {
                    DFS(item[i].children)
                }
            }
            return arr
        }
        console.log(DFS(tree))
    </script>
</body>

</html>

 

<html>

<head>
    <!-- <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> -->
    <style>
    </style>
</head>
<div id='root'>
    <span>123
        <a href="#">
            sdsd
        </a>
        <div>sdsd<a>这是一个a标签</a></div>
    </span>
    <span>456
        <p>这是一个p标签</p>
    </span>
</div>

<body>
    <script>
        // 深度优先算法 DFS,递归实现
        function deepTraversal(node, nodeList) {
            if (node) {
                nodeList.push(node);
                var children = node.children;
                for (var i = 0; i < children.length; i++)
                    //每次递归的时候将  需要遍历的节点  和 节点所存储的数组传下去
                    deepTraversal(children[i], nodeList);
            }
            return nodeList;
        }
        var root = document.getElementById('root')
        console.log(deepTraversal(root, nodeList = []))
        // 深度优先算法 DFS
        function deepTraversal(node) {
            var nodeList = [];
            if (node) {
                var stack = [];
                stack.push(node);
                while (stack.length != 0) {
                    var childrenItem = stack.pop();
                    nodeList.push(childrenItem);
                    var childrenList = childrenItem.children;
                    for (var i = childrenList.length - 1; i >= 0; i--)
                        stack.push(childrenList[i]);
                }
            }
            return nodeList;
        }
        var root = document.getElementById('root')
        console.log(deepTraversal(root))
        // 广度优先算法 BFS
        function wideTraversal(node) {
            var nodes = [];
            if (node != null) {
                var queue = [];
                queue.unshift(node);
                while (queue.length != 0) {
                    var item = queue.shift();
                    nodes.push(item);
                    var children = item.children;
                    for (var i = 0; i < children.length; i++)
                        queue.push(children[i]);
                }
            }
            return nodes;
        }
        var root = document.getElementById('root');
        console.log(wideTraversal(root)); 
    </script>
</body>

</html>

 

posted @ 2020-08-17 11:46  问问大将军  阅读(308)  评论(0编辑  收藏  举报