JavaScript實(shí)現(xiàn)二叉樹的先序、中序及后序遍歷方法詳解
本文實(shí)例講述了JavaScript實(shí)現(xiàn)二叉樹的先序、中序及后序遍歷方法。分享給大家供大家參考,具體如下:
之前學(xué)數(shù)據(jù)結(jié)構(gòu)的時(shí)候,學(xué)了二叉樹的先序、中序、后序遍歷的方法,并用C語言實(shí)現(xiàn)了,下文是用js實(shí)現(xiàn)二叉樹的3種遍歷,并以動(dòng)畫的形式展現(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錯(cuò)誤與調(diào)試技巧總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
詳解TypeScript中type與interface的區(qū)別
在寫 ts 相關(guān)代碼的過程中,總能看到 interface 和 type 的身影。它們的作用好像都一樣的,相同的功能用哪一個(gè)都可以實(shí)現(xiàn),也都很好用,所以也很少去真正的理解它們之間到底有啥區(qū)別,因此本文將詳細(xì)講解二者的區(qū)別,需要的可以參考一下2022-04-04
微信公眾平臺(tái)獲取access_token的方法步驟
這篇文章主要介紹了微信公眾平臺(tái)獲取access_token的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
微信小程序之swiper輪播圖中的圖片自適應(yīng)高度的方法
這篇文章主要介紹了微信小程序之swiper輪播圖中的圖片自適應(yīng)高度的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
自動(dòng)生成文章摘要的代碼[JavaScript 版本]
自動(dòng)生成文章摘要的代碼[JavaScript 版本]...2007-03-03
Javascript 實(shí)現(xiàn)復(fù)制(Copy)動(dòng)作方法大全
現(xiàn)在瀏覽器種類也越來越多,諸如 IE、Firefox、Chrome、Safari等等,因此現(xiàn)在要實(shí)現(xiàn)一個(gè)js復(fù)制內(nèi)容到剪貼板的小功能就不是一件那么容易的事了。2014-06-06
使用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)的方法,結(jié)合實(shí)例形式總結(jié)分析了javascript針對(duì)文本框光標(biāo)的位置、設(shè)置及文本操作的相關(guān)技巧,需要的朋友可以參考下2016-11-11

