先製做一個二叉樹的結構和按鈕組html
經過flex佈局,node
justify-content: center;//水平居中
align-items: center;//垂直居中
而後設計遍歷,http://www.javashuo.com/article/p-vuxboynr-p.html有關遍歷介紹git
須要將樹結構進行遍歷,顯示出遍歷的顏色github
首先進行樹的遍歷,把遍歷順序記錄在數組中,而後根據數組順序,進行顏色變化算法
//先序遍歷 function preOrder(node){ if(!node == null){ putstr(node.show()+ " "); preOrder(node.left); preOrder(node.right); } } //前序遍歷 function preOrder(node){ if(node !=null){ data.push(node); preOrder(node.firstElementChild); preOrder(node.lastElementChild); } }
head取出數組中的節點數據;並將對應的節點的顏色進行修改segmentfault
head = data.shift();//刪除並返回數組的第一個元素 if(head){ head.style.backgroundColor = "#ff0000"; timer = setTimeout(function(){ head.style.backgroundColor = "#fff"; show(); },500); }