任务七:JavaScript和树(一)

学习二叉树递归

属于最简单的递归


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>task7</title>
</head>

<body>
    <div id="wrapper" style="background-color: rgb(255, 255, 255);">
        <div class="layer_1" style="background-color: rgb(255, 255, 255);">
            <div class="layer_2" style="background-color: rgb(255, 255, 255);">
                <div class="layer_3" style="background-color: rgb(255, 255, 255);"></div>
                <div class="layer_3" style="background-color: rgb(255, 255, 255);"></div>
            </div>
            <div class="layer_2" style="background-color: rgb(255, 255, 255);">
                <div class="layer_3" style="background-color: rgb(255, 255, 255);"></div>
                <div class="layer_3" style="background-color: rgb(255, 255, 255);"></div>
            </div>
        </div>
        <div class="layer_1" style="background-color: rgb(255, 255, 255);">
            <div class="layer_2" style="background-color: rgb(255, 255, 255);">
                <div class="layer_3" style="background-color: rgb(255, 255, 255);"></div>
                <div class="layer_3" style="background-color: rgb(255, 255, 255);"></div>
            </div>
            <div class="layer_2" style="background-color: rgb(255, 255, 255);">
                <div class="layer_3" style="background-color: rgb(255, 255, 255);"></div>
                <div class="layer_3" style="background-color: rgb(255, 255, 255);"></div>
            </div>
        </div>
    </div>
    <div id="control-box">
        <input type="button" id="preorder" value="前序遍历">
        <input type="button" id="inorder" value="中序遍历">
        <input type="button" id="postorder" value="后序遍历">
    </div>
<style>
    #wrapper,#wrapper div{
        display: flex;
      flex-direction: row;
      padding: 15px 10px;
      margin: 5px;
      border: 1px solid #000;
      background-color: #fff;
    }
    #wrapper {
  width: 730px;
  height: 240px; 
}

.layer_1 {
  width: 340px;
  height: 200px; 
}

.layer_2 {
  width: 135px;
  height: 160px; 
}

.layer_3 {
  width: 52.5px;
  height: 120px;
}</style>
<script type="text/javascript">
    var wrapper=document.getElementById("wrapper");
    var arr=[];
function tree(obj,num){
    if(num==0){arr.push(obj);}
    if(obj.children[0]){
        tree(obj.children[0],num);
    }
    if(num==1){arr.push(obj);}
    if(obj.children[1]){
        tree(obj.children[1],num);
    }
    if(num==2){arr.push(obj);}
}
//0是前序  1是中序  2 是后续排列
document.querySelectorAll('#control-box input').forEach(function(ev,index){
    ev.addEventListener('click',function(){
        arr=[];
        tree(wrapper,index);
        var length=arr.length;
        var i=0;
        var timer=setInterval(function(){
            if(i){arr[i-1].style.backgroundColor='';}
            arr[i].style.backgroundColor='blue';
            i++;
            if(i>=length){
                clearInterval(timer);
                setTimeout(function(){
                    arr[i-1].style.backgroundColor='';
                },500)
            }
        },500)
    })
})
</script>
</body></html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 树的概述 树是一种非常常用的数据结构,树与前面介绍的线性表,栈,队列等线性结构不同,树是一种非线性结构 1.树的定...
    Jack921阅读 9,933评论 1 31
  • 1 序 2016年6月25日夜,帝都,天下着大雨,拖着行李箱和同学在校门口照了最后一张合照,搬离寝室打车去了提前租...
    RichardJieChen阅读 10,610评论 0 12
  • 前言 总括: 本文讲解了数据结构中的[树]的概念,尽可能通俗易懂的解释树这种数据结构的概念,使用javascrip...
    秦至阅读 4,196评论 0 6
  • 今天听课前翻出大学外国文学史的笔记,所有的记忆扑面而来。不同的是,比之前有了更深的理解。在变是唯一不变的今天,总有...
    云动梦飞阅读 2,286评论 0 0
  • 毕业已经87天 天真的以为世界就是自己想象的那个样子 而其实人心难测,前后相悖变成了往往是常态 半年的时间其实并不...
    听风在说话阅读 1,683评论 1 1