美文网首页前端面试基础必备JS学习笔记
JS二叉树遍历(前序、中序、后序、深度优先、广度优先)

JS二叉树遍历(前序、中序、后序、深度优先、广度优先)

作者: puxiaotaoc | 来源:发表于2018-09-17 01:26 被阅读22次
function Tree() {
   // 定义树结点
   var Node = function(element) {
      this.element = element;
      this.left = null;
      this.right = null;
   }
   // 定义根结点
   var root = null;
}
// 前序遍历 根左右 
Tree.prototype.preOrderTraverse = function(callback) {
    preOrder(root, callback);
  }

  var preOrder = function(node, callback) {
    if (node !== null) {
      callback(node.element);
      preOrder(node.left, callback);
      preOrder(node.right, callback);
    }
  }
// DOM的前序遍历 递归方法
var preOrder = function(node,callback) {  
    callback(node);  
    if(node.firstElementChild) {//先判断子元素节点是否存在  
         this.preOrder(node.firstElementChild,callback);  
    }  
    if(node.lastElementChild) {  
        this.preOrder(node.lastElementChild,callback);  
    }  
};  
// DOM的前序遍历 非递归方法 借助于栈
Tree.prototype.preOrder = function(node,callback) {  
        var stack=[];  
        while(node!== null || stack.length!=0){  
            while(node!==null){  
                stack.push(node);  
                callback(node);  
                node=node.firstElementChild;  
            }  
            node=stack.pop();  
            node=node.lastElementChild;  
        }  
    };  
// 中序遍历 左根右
Tree.prototype.inOrderTraverse = function(callback) {
    inOrder(root, callback);
  }
  var inOrder = function(node, callback) {
    if (node !== null) {
      inOrder(node.left, callback);
      callback(node);
      inOrder(node.right, callback);
    }
  }
// DOM中序遍历 左根右 递归方法
var inOrder = function(node, callback) {
    if (node.firstElementChild) {
      this.inOrder(node.firstElementChild,callback);
    }
    callback(node);
    if (node.lastElementChild) {
      this.inOrder(node.lastElementChild,callback);
    }
  }
// DOM中序遍历 左根右 非递归方法
Tree.prototype.inOrder = function(node,callback) {  
        var stack=[];  
        while(node!== null || stack.length!=0){  
            while(node!==null){  
                stack.push(node);  
                node=node.firstElementChild;  
            }  
            node=stack.pop();  
            callback(node);  
            node=node.lastElementChild;  
        }  
    };  
// 后序遍历 左右根
Tree.prototype.postOrderTraverse = function(callback){  
    postOrder(root, callback);  
}  
var postOrder = function(node,callback){  
    if(node !== null){  
        postOrder(node.left,callback);     
        postOrder(node.right, calback);  
        callback(node.key);     
    }  
}  
// DOM的后序遍历 递归方法
var postOrder = function(node,callback){  
    if(node.firstElementChild) {  
    this.postOrder(node.firstElementChild);  
    }   
    if(node.lastElementChild) {  
    this.postOrder(node.lastElementChild);  
    }  
    callback(node);   
}  
// DOM的后序遍历 非递归方法
Tree.prototype.postOrder = function(node, callback) { //非递归实现
    var stack = [];
    stack.push(node);
    stack.push(node);
    while (stack.length != 0) {
      node = stack.pop();
      if (stack.length != 0 && node == stack[stack.length - 1]) {
        if (node.lastElementChild) {
          stack.push(node.lastElementChild);
          stack.push(node.lastElementChild);
        }
        if (node.firstElementChild) {
          stack.push(node.firstElementChild);
          stack.push(node.firstElementChild);
        }
      } else
        callback(node);
    }
  }
// 多叉树的遍历,广度优先遍历
// 层序遍历,借助队列,非递归方式
Tree.prototype.BFSearch = function(node, callback) {
    var queue = [];
    while (node != null) {
      callback(node);
      if (node.children.length != 0) {
        for (var i = 0; i < node.children.length; i++) {
          queue.push(node.children[i]); //借助于队列,暂存当前节点的所有子节点  
        }
      }
      node = queue.shift(); //先入先出,借助于数据结构:队列  
    }
  };
// 多叉树的遍历,深度优先遍历
// 借助栈,首先遍历根节点,然后沿着一条路径遍历到最深的一层,最后在逐层返回
Tree.prototype.DFSearch = function(node, callback) {
    var stack = [];
    while (node != null) {
      callback(node);
      if (node.children.length != 0) {
        for (var i = node.children.length - 1; i >= 0; i--) { //按照相反的子节点顺序压入栈  
          stack.push(node.children[i]); //将该节点的所有子节点压入栈  
        }
      }
      node = stack.pop(); //弹出栈的子节点顺序就是原来的正确顺序(因为栈是先入后出的)        
    }
  };

参考链接:
JS实现DOM树的遍历

相关文章

  • 二叉树遍历

    二叉树的遍历,分为深度优先遍历和广度优先遍历,其中深度优先遍历又分为有前序、中序、后序遍历,广度优先遍历就是按层遍...

  • Binary Tree - Swift 相关实现

    原文参考 节点 翻转二叉树 前序遍历 中序遍历 后序遍历 层次遍历/广度优先遍历 深度优先遍历 判断二叉排序树

  • 树的几种遍历方式

    主要记录一下对于二叉树,进行遍历的几种方式,包括: 前序遍历 中序遍历 后序遍历 深度优先遍历 广度优先遍历 我们...

  • 算法之二叉树遍历

    二叉树遍历可以使用深度优先周游二叉树和广度优先周游二叉树,深度优先又可以分为前序、中序、后序三种方式遍历,每种方式...

  • 实现二叉树前序、中序、后序遍历及广度优先遍历、层序遍历

    构造一个树的数据结构tree,如下: 前序遍历 中序遍历 后序遍历 注:上述三种遍历都是深度优先遍历 广度优先遍历...

  • js二叉树(前中后序遍历)+多叉树(深度优先遍历和广度优先遍历)

    ?二叉树三种遍历 和 多叉树 深度优先遍历和广度优先遍历 二叉树遍历 先序遍历(根左右) 中序遍历(左根右) 后序...

  • 二叉树遍历/搜索

    遍历 1.宽度优先遍历BFS 利用队列 2.深度优先遍历DFS 1.递归:前序中序后序 2.非递归:前序中序后序 利用栈

  • 算法-二叉树的遍历实现

    简述 二叉树的遍历分 DFS【深度优先遍历】 和 BFS【广度优先遍历】 两类,其中 DFS 又分为前序遍历,中序...

  • 5. 深度优先、广度优先

    1. 二叉树的深度优先遍历和广度优先遍历2. 深度优先搜索递归和非递归实现 深度优先(DFS):前序遍历 广度优先...

  • 二叉树遍历

    1.遍历方式 深度优先遍历:前序遍历、中序遍历、后续遍历 广度优先遍历:层序遍历 2.前序遍历 输出顺序:根节点、...

网友评论

    本文标题:JS二叉树遍历(前序、中序、后序、深度优先、广度优先)

    本文链接:https://www.haomeiwen.com/subject/ofgqnftx.html