JavaScript实现二叉树的先序、中序及后序遍历方法详解


Posted in Javascript onOctober 26, 2017

本文实例讲述了JavaScript实现二叉树的先序、中序及后序遍历方法。分享给大家供大家参考,具体如下:

之前学数据结构的时候,学了二叉树的先序、中序、后序遍历的方法,并用C语言实现了,下文是用js实现二叉树的3种遍历,并以动画的形式展现出遍历的过程。

整个遍历过程还是采用递归的思想,原理很粗暴也很简单

先序遍历的函数:

function preOrder(node){
  if(!(node==null)){
    divList.push(node);
    preOrder(node.firstElementChild);
    preOrder(node.lastElementChild);
  }
}

中序遍历的函数:

function inOrder(node) {
  if (!(node == null)) {
    inOrder(node.firstElementChild);
    divList.push(node);
    inOrder(node.lastElementChild);
  }
}

后序遍历的函数:

function postOrder(node) {
  if (!(node == null)) {
    postOrder(node.firstElementChild);
    postOrder(node.lastElementChild);
    divList.push(node);
  }
}

颜色变化函数:

function changeColor(){
  var i=0;
  divList[i].style.backgroundColor = 'blue';
  timer=setInterval(function(argument){
    i++;
    if(i<divList.length){
      divList[i-1].style.backgroundColor="#fff";
      divList[i].style.backgroundColor="blue";
    }
    else{
      divList[divList.length-1].style.backgroundColor="#fff";
    }
  },500)
}

核心代码如上,本来想写深度优先遍历和广度优先遍历。后来发现二叉树深度优先遍历和先序遍历相同。改日总结一下树的BFS和DFS。

全部代码如下:

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <style>
    .root{
      display: flex;
      padding: 20px;
      width: 1000px;
      height: 300px;border: 1px solid #000000;
      margin: 100px auto;
      margin-bottom: 10px;
      justify-content: space-between;
    }
    .child_1{
      display: flex;
      padding: 20px;
      width: 450px;
      height: 260px;border: 1px solid red;
      justify-content: space-between;
    }
    .child_2{
      display: flex;
      padding: 20px;
      width: 170px;
      height: 220px;border: 1px solid green;
      justify-content: space-between;
    }
    .child_3{
      display: flex;
      padding: 20px;
      width: 35px;
      height: 180px;border: 1px solid blue;
      justify-content: space-between;
    }
    input{
      margin-left: 100px;
      width: 60px;
      height: 40px;
      font:20px italic;
    }
  </style>
</head>
<body>
<div class="root">
  <div class="child_1">
    <div class="child_2">
      <div class="child_3"></div>
      <div class="child_3"></div>
    </div>
    <div class="child_2">
      <div class="child_3"></div>
      <div class="child_3"></div>
    </div>
  </div>
  <div class="child_1">
    <div class="child_2">
      <div class="child_3"></div>
      <div class="child_3"></div>
    </div>
    <div class="child_2">
      <div class="child_3"></div>
      <div class="child_3"></div>
    </div>
  </div>
</div>
<input type="button" value="先序">
<input type="button" value="中序">
<input type="button" value="后序">
<script type="text/javascript" src="遍历.js"></script>
</body>
</html>

js:

/**
 * Created by hp on 2016/12/22.
 */
var btn = document.getElementsByTagName('input'),
  preBtn = btn[0],
  inBtn = btn[1],
  postBtn = btn[2],
  treeRoot = document.getElementsByClassName('root')[0],
  divList = [],
  timer = null;
window.onload=function(){
  preBtn.onclick = function () {
    reset();
    preOrder(treeRoot);
    changeColor();
  }
  inBtn.onclick = function () {
    reset();
    inOrder(treeRoot);
    changeColor();
  }
  postBtn.onclick = function () {
    reset();
    postOrder(treeRoot);
    changeColor();
  }
}
/*先序遍历*/
function preOrder(node){
  if(!(node==null)){
    divList.push(node);
    preOrder(node.firstElementChild);
    preOrder(node.lastElementChild);
  }
}
/*中序遍历*/
function inOrder(node) {
  if (!(node == null)) {
    inOrder(node.firstElementChild);
    divList.push(node);
    inOrder(node.lastElementChild);
  }
}
/*后序遍历*/
function postOrder(node) {
  if (!(node == null)) {
    postOrder(node.firstElementChild);
    postOrder(node.lastElementChild);
    divList.push(node);
  }
}
/*颜色变化函数*/
function changeColor(){
  var i=0;
  divList[i].style.backgroundColor = 'blue';
  timer=setInterval(function(argument){
    i++;
    if(i<divList.length){
      divList[i-1].style.backgroundColor="#fff";
      divList[i].style.backgroundColor="blue";
    }
    else{
      divList[divList.length-1].style.backgroundColor="#fff";
    }
  },500)
}
function reset(){
  divList=[];
  clearInterval(timer);
  var divs=document.getElementsByTagName("div");
  for(var i=0;i<divs.length;i++){
    divs[i].style.backgroundColor="#fff";
  }
}

由此可见,二叉树的遍历思想是一样的。之前一直把JS看做是写各种特效的语言,现在向来是too naive了。

希望本文所述对大家JavaScript程序设计有所帮助。

Javascript 相关文章推荐
jtable列中自定义button示例代码
Nov 21 Javascript
jquery 新建的元素事件绑定问题解决方案
Jun 12 Javascript
javascript中的this详解
Dec 08 Javascript
Bootstrap 粘页脚效果
Mar 28 Javascript
gulp-htmlmin压缩html的gulp插件实例代码
Jun 06 Javascript
js 实现省市区三级联动菜单效果
Feb 20 Javascript
Angular.Js中过滤器filter与自定义过滤器filter实例详解
May 08 Javascript
关于TypeScript中import JSON的正确姿势详解
Jul 25 Javascript
Vue打包后出现一些map文件的解决方法
Feb 13 Javascript
Vue2.0 给Tab标签页和页面切换过渡添加样式的方法
Mar 13 Javascript
微信小程序用户登录和登录态维护的实现
Dec 10 Javascript
uniapp 微信小程序 自定义tabBar 导航
Apr 22 Javascript
JavaScript实现树的遍历算法示例【广度优先与深度优先】
Oct 26 #Javascript
详述 Sublime Text 打开 GBK 格式中文乱码的解决方法
Oct 26 #Javascript
node.js的exports、module.exports与ES6的export、export default深入详解
Oct 26 #Javascript
详解Webstorm 新建.vue文件支持高亮vue语法和es6语法
Oct 26 #Javascript
Node.js中环境变量process.env的一些事详解
Oct 26 #Javascript
Sublime Text新建.vue模板并高亮(图文教程)
Oct 26 #Javascript
vue+swiper实现组件化开发的实例代码
Oct 26 #Javascript
You might like
德生S2000电路分析
2021/03/02 无线电
制作特殊字的脚本
2006/06/26 Javascript
JavaScript实现x秒后自动跳转到一个页面
2013/01/03 Javascript
js与jQuery实现checkbox复选框全选/全不选的方法
2016/01/05 Javascript
jquery模拟实现鼠标指针停止运动事件
2016/01/12 Javascript
基于javascript显示当前时间以及倒计时功能
2016/03/18 Javascript
jQuery加载及解析XML文件的方法实例分析
2017/01/22 Javascript
js禁止浏览器页面后退功能的实例(推荐)
2017/09/01 Javascript
Angular.js通过自定义指令directive实现滑块滑动效果
2017/10/13 Javascript
vue 简单自动补全的输入框的示例
2018/03/12 Javascript
使用vuex的state状态对象的5种方式
2018/04/19 Javascript
详解Vue一个案例引发「内容分发slot」的最全总结
2018/12/02 Javascript
解决vue的touchStart事件及click事件冲突问题
2020/07/21 Javascript
vue递归获取父元素的元素实例
2020/08/07 Javascript
[45:25]OG vs EG 2019国际邀请赛淘汰赛 胜者组 BO3 第一场 8.22
2019/09/05 DOTA
Python中的类学习笔记
2014/09/23 Python
python的else子句使用指南
2016/02/27 Python
深入了解Python数据类型之列表
2016/06/24 Python
插入排序_Python与PHP的实现版(推荐)
2017/05/11 Python
Python模块搜索路径代码详解
2018/01/29 Python
Python OpenCV处理图像之滤镜和图像运算
2018/07/10 Python
Python解析、提取url关键字的实例详解
2018/12/17 Python
Python 监测文件是否更新的方法
2019/06/10 Python
使用TensorFlow-Slim进行图像分类的实现
2019/12/31 Python
通过Turtle库在Python中绘制一个鼠年福鼠
2020/02/03 Python
Django重设Admin密码过程解析
2020/02/10 Python
利用pandas向一个csv文件追加写入数据的实现示例
2020/04/23 Python
Cpython解释器中的GIL全局解释器锁
2020/11/09 Python
泰国演唱会订票网站:StubHub泰国
2018/02/26 全球购物
英国儿童设计师服装和玩具购物网站:Zac & Lulu
2020/10/19 全球购物
高一自我鉴定
2013/12/17 职场文书
幼儿园教师个人总结
2015/02/05 职场文书
专项资金申请报告
2015/05/15 职场文书
大学生社会服务心得体会
2016/01/22 职场文书
css position fixed 左右双定位的实现代码
2021/04/29 HTML / CSS
java协程框架quasar和kotlin中的协程对比分析
2022/02/24 Java/Android