任務(wù)七:JavaScript和樹(shù)(一)

學(xué)習(xí)二叉樹(shù)遞歸

屬于最簡(jiǎn)單的遞歸


<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 是后續(xù)排列
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>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 樹(shù)的概述 樹(shù)是一種非常常用的數(shù)據(jù)結(jié)構(gòu),樹(shù)與前面介紹的線(xiàn)性表,棧,隊(duì)列等線(xiàn)性結(jié)構(gòu)不同,樹(shù)是一種非線(xiàn)性結(jié)構(gòu) 1.樹(shù)的定...
    Jack921閱讀 4,756評(píng)論 1 31
  • 1 序 2016年6月25日夜,帝都,天下著大雨,拖著行李箱和同學(xué)在校門(mén)口照了最后一張合照,搬離寢室打車(chē)去了提前租...
    RichardJieChen閱讀 5,372評(píng)論 0 12
  • 前言 總括: 本文講解了數(shù)據(jù)結(jié)構(gòu)中的[樹(shù)]的概念,盡可能通俗易懂的解釋樹(shù)這種數(shù)據(jù)結(jié)構(gòu)的概念,使用javascrip...
    秦至閱讀 878評(píng)論 0 6
  • 今天聽(tīng)課前翻出大學(xué)外國(guó)文學(xué)史的筆記,所有的記憶撲面而來(lái)。不同的是,比之前有了更深的理解。在變是唯一不變的今天,總有...
    云動(dòng)夢(mèng)飛閱讀 334評(píng)論 0 0
  • 畢業(yè)已經(jīng)87天 天真的以為世界就是自己想象的那個(gè)樣子 而其實(shí)人心難測(cè),前后相悖變成了往往是常態(tài) 半年的時(shí)間其實(shí)并不...
    聽(tīng)風(fēng)在說(shuō)話(huà)閱讀 275評(píng)論 1 1

友情鏈接更多精彩內(nèi)容