国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JavaScript實現二叉樹的先序、中序及后序遍歷方法詳解

2024-05-06 15:46:18
字體:
來源:轉載
供稿:網友

我們在學習數據結構之前,已經學習了前序、中序和后序的非遞歸遍歷中,而且可以使用C語言實現,下面是錯新技術頻道小編介紹的JavaScript實現二叉樹的先序、中序及后序遍歷方法詳解。

先序遍歷的函數:

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實現二叉樹的先序、中序及后序遍歷方法詳解,大家可以自行學習,希望本文能夠幫助到大家。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 厦门市| 福鼎市| 拜泉县| 怀安县| 乐都县| 灵川县| 普定县| 奉节县| 自贡市| 内丘县| 漳州市| 昌邑市| 罗田县| 临清市| 平果县| 连南| 尼勒克县| 沿河| 长顺县| 绥宁县| 包头市| 改则县| 沐川县| 南丰县| 稷山县| 屏东县| 青阳县| 津南区| 方正县| 沭阳县| 通河县| 满洲里市| 南召县| 莱阳市| 拜泉县| 紫阳县| 虹口区| 达尔| 昆山市| 伊川县| 台山市|