JavaScript实现二叉树的先序、中序及后序遍历方…
2020-01-17 08:53:37来源:爱站网 阅读 ()
我们在学习数据结构之前,已经学习了前序、中序和后序的非递归遍历中,而且可以使用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实现二叉树的先序、中序及后序遍历方法详解,大家可以自行学习,希望本文能够帮助到大家。
原文链接:https://js.aizhan.com/develop/JavaScript/11193.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- javascript面向对象入门基础详细介绍 2020-03-29
- js防止表单重复提交实现代码 2020-03-29
- 基于JQuery的多标签实现代码 2020-03-29
- js实现翻页后保持checkbox选中状态的实现方法 2020-03-25
- JavaScript函数表达式详解及实例 2020-03-25
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
