最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript實(shí)現(xiàn)二叉樹的先序、中序及后序遍歷方法詳解

 更新時(shí)間:2017年10月26日 11:59:54   作者:juzipchy  
這篇文章主要介紹了JavaScript實(shí)現(xiàn)二叉樹的先序、中序及后序遍歷方法,結(jié)合實(shí)例形式總結(jié)分析了javascript二叉樹的先序、中序及后序遍歷實(shí)現(xiàn)方法與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了JavaScript實(shí)現(xiàn)二叉樹的先序、中序及后序遍歷方法。分享給大家供大家參考,具體如下:

之前學(xué)數(shù)據(jù)結(jié)構(gòu)的時(shí)候,學(xué)了二叉樹的先序、中序、后序遍歷的方法,并用C語言實(shí)現(xiàn)了,下文是用js實(shí)現(xiàn)二叉樹的3種遍歷,并以動畫的形式展現(xiàn)出遍歷的過程。

整個(gè)遍歷過程還是采用遞歸的思想,原理很粗暴也很簡單

先序遍歷的函數(shù):

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

中序遍歷的函數(shù):

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

后序遍歷的函數(shù):

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

顏色變化函數(shù):

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

核心代碼如上,本來想寫深度優(yōu)先遍歷和廣度優(yōu)先遍歷。后來發(fā)現(xiàn)二叉樹深度優(yōu)先遍歷和先序遍歷相同。改日總結(jié)一下樹的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);
  }
}
/*顏色變化函數(shù)*/
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看做是寫各種特效的語言,現(xiàn)在向來是too naive了。

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》、《JavaScript排序算法總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》及《JavaScript錯誤與調(diào)試技巧總結(jié)

希望本文所述對大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 詳解TypeScript中type與interface的區(qū)別

    詳解TypeScript中type與interface的區(qū)別

    在寫 ts 相關(guān)代碼的過程中,總能看到 interface 和 type 的身影。它們的作用好像都一樣的,相同的功能用哪一個(gè)都可以實(shí)現(xiàn),也都很好用,所以也很少去真正的理解它們之間到底有啥區(qū)別,因此本文將詳細(xì)講解二者的區(qū)別,需要的可以參考一下
    2022-04-04
  • 微信公眾平臺獲取access_token的方法步驟

    微信公眾平臺獲取access_token的方法步驟

    這篇文章主要介紹了微信公眾平臺獲取access_token的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 微信小程序之swiper輪播圖中的圖片自適應(yīng)高度的方法

    微信小程序之swiper輪播圖中的圖片自適應(yīng)高度的方法

    這篇文章主要介紹了微信小程序之swiper輪播圖中的圖片自適應(yīng)高度的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04
  • 自動生成文章摘要的代碼[JavaScript 版本]

    自動生成文章摘要的代碼[JavaScript 版本]

    自動生成文章摘要的代碼[JavaScript 版本]...
    2007-03-03
  • JSON字符串操作移除空串更改key/value的介紹

    JSON字符串操作移除空串更改key/value的介紹

    今天小編就為大家分享一篇關(guān)于JSON字符串操作移除空串更改key/value的介紹,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Javascript 實(shí)現(xiàn)復(fù)制(Copy)動作方法大全

    Javascript 實(shí)現(xiàn)復(fù)制(Copy)動作方法大全

    現(xiàn)在瀏覽器種類也越來越多,諸如 IE、Firefox、Chrome、Safari等等,因此現(xiàn)在要實(shí)現(xiàn)一個(gè)js復(fù)制內(nèi)容到剪貼板的小功能就不是一件那么容易的事了。
    2014-06-06
  • js獲取網(wǎng)頁高度(詳細(xì)整理)

    js獲取網(wǎng)頁高度(詳細(xì)整理)

    js獲取網(wǎng)頁高度包括(網(wǎng)頁可見區(qū)域高,屏幕分辨率的高,屏幕可用工作區(qū)高度,js 獲取瀏覽器高度 js 獲取屏幕高度)等等,各種高度,本文詳細(xì)整理了一些,需要了解的朋友可以參考下
    2012-12-12
  • 使用JavaScript構(gòu)建JSON格式字符串實(shí)現(xiàn)步驟

    使用JavaScript構(gòu)建JSON格式字符串實(shí)現(xiàn)步驟

    這篇文章將幫助你使用javascript來創(chuàng)建json格式字符串如果你需要通過web項(xiàng)目來構(gòu)建json格式字符串的響應(yīng),感興趣的各位可以參考下哈,希望可以幫助到你
    2013-03-03
  • javascript設(shè)置文本框光標(biāo)的方法實(shí)例小結(jié)

    javascript設(shè)置文本框光標(biāo)的方法實(shí)例小結(jié)

    這篇文章主要介紹了javascript設(shè)置文本框光標(biāo)的方法,結(jié)合實(shí)例形式總結(jié)分析了javascript針對文本框光標(biāo)的位置、設(shè)置及文本操作的相關(guān)技巧,需要的朋友可以參考下
    2016-11-11
  • es6函數(shù)之尾遞歸用法實(shí)例分析

    es6函數(shù)之尾遞歸用法實(shí)例分析

    這篇文章主要介紹了es6函數(shù)之尾遞歸用法,結(jié)合實(shí)例形式分析了es6函數(shù)尾遞歸原理、用法及操作注意事項(xiàng),需要的朋友可以參考下
    2020-04-04

最新評論

齐河县| 长治县| 横山县| 竹山县| 庐江县| 满洲里市| 西乡县| 南投县| 琼中| 达州市| 荔浦县| 怀远县| 博乐市| 平度市| 南郑县| 元谋县| 昭平县| 若羌县| 周至县| 西藏| 岳阳市| 德令哈市| 汕尾市| 威宁| 南京市| 桦川县| 永丰县| 潞西市| 探索| 千阳县| 龙游县| 丹凤县| 武穴市| 武陟县| 吉林省| 南川市| 虞城县| 白玉县| 新龙县| 喀喇| 九江市|