JavaScript实现二叉树先序、中序及后序遍历的可视化详解
本文详细介绍如何使用JavaScript实现二叉树的先序、中序和后序遍历。通过递归思想构建核心算法,并结合HTML5与CSS3实现遍历过程的动画可视化。文章提供了完整的代码示例,帮助开发者深入理解树结构遍历逻辑及其在前端交互中的应用。
JavaScript二叉树遍历概述
本文通过具体实例,详细解析了如何利用JavaScript代码实现二叉树的先序、中序以及后序遍历操作,旨在为读者提供可参考的技术方案。
在数据结构课程中,我们曾学习过二叉树的三种遍历方式,并使用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)
}
上述核心代码展示了遍历逻辑。原本计划编写深度优先遍历(DFS)和广度优先遍历(BFS),但发现二叉树的深度优先遍历与先序遍历逻辑一致。后续将另行总结树的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>
JavaScript部分实现如下:
/**
* 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";
}
}
通过上述代码可以看出,二叉树的遍历核心思想是一致的。过去常有人误认为JavaScript仅用于编写页面特效,这种观点已显过时。
若读者对JavaScript其他技术感兴趣,可进一步查阅本站相关专题,包括《JavaScript数据结构与算法技巧总结》、《JavaScript数学运算用法总结》、《JavaScript排序算法总结》、《JavaScript遍历算法与技巧总结》、《JavaScript查找算法技巧总结》以及《JavaScript错误与调试技巧总结》。
希望本文提供的代码与解析能对大家的JavaScript编程实践有所帮助。
JavaScript入门书籍避坑:这些书不适合初学者
2026-11-05 16:31:15
JS使用Math.round实现保留两位小数的原理与代码
2026-11-05 15:35:55
JavaScript 原生 Date 对象怎么实现日期加减
2026-11-05 14:30:00
JavaScript函数定义的三种方式:声明、表达式与箭头函数
2026-11-05 13:50:55
JavaScript中escape、encodeURI和encodeURIComponent对空格的处理差异
2026-11-05 12:47:23
