-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtask022.js
69 lines (62 loc) · 1.3 KB
/
task022.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
$(document).ready(function(){
var treeRoot = $('body>div')[0];
$('#root').click(function(){
reset();
preOrder(treeRoot);
render(nodeArr);
});
$('#left').click(function(){
reset();
inOrder(treeRoot);
render(nodeArr);
});
$('#right').click(function(){
reset();
postOrder(treeRoot);
render(nodeArr);
});
})
var timer=null, nodeArr =[];
var preOrder = function(node){
if(node !== null){
nodeArr.push(node);
preOrder(node.firstElementChild);
preOrder(node.lastElementChild);
}
console.log(nodeArr);
};
var inOrder = function(node){
if(node !== null){
inOrder(node.firstElementChild);
nodeArr.push(node);
inOrder(node.lastElementChild);
}
console.log(nodeArr);
};
var postOrder = function(node){
if(node !== null){
postOrder(node.firstElementChild);
postOrder(node.lastElementChild);
nodeArr.push(node);
}
console.log(nodeArr);
};
var render = function(arr){
var i=0;
arr[i].style.backgroundColor='pink';
timer = setInterval(function(){
if(i<arr.length-1){
i++;
arr[i-1].style.backgroundColor = 'white';
arr[i].style.backgroundColor = 'pink';
}else{
arr[arr.length-1].style.backgroundColor ='white';
window.clearInterval(timer);
}
},500);
};
var reset = function(){
nodeArr=[];
window.clearInterval(timer);
$('div').css('background-color','white');
}