Skip to content

Commit d28036e

Browse files
author
hans
committed
添加bfs dfs等算法
1 parent 443b0be commit d28036e

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
*.ppt
33
test.*
44
.vscode/
5-
node_modules/
5+
node_modules/
6+
note.*

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
### 1、知识点
1010

1111
- [数组常用的api](https://github.com/haima16/JavaScript/tree/master/%E7%9F%A5%E8%AF%86%E7%82%B9)
12-
- 排序算法
12+
- 常用算法
1313

1414
### 2、OOE
1515

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,72 @@
9999
return result.concat(left, right)
100100
}
101101
}
102+
103+
/**
104+
* A
105+
* B C
106+
* D E F G
107+
*/
108+
let tree = {
109+
'A': ['B', 'C'],
110+
'B': ['D', 'E'],
111+
'C': ['F', 'G']
112+
}
113+
// 深度优先搜索
114+
function dfs(tree, node, list=[]) {
115+
list.push(node)
116+
let child = tree[node]
117+
child && child.forEach(e => dfs(tree, e, list))
118+
return list
119+
}
120+
// console.log(dfs(tree, 'A'));
121+
122+
// 广度优先搜索
123+
function bfs(tree, node, list=[]) {
124+
let stack = [node]
125+
while (stack.length) {
126+
let child = stack.shift()
127+
list.push(child)
128+
tree[child] && stack.push(...tree[child])
129+
}
130+
return list
131+
}
132+
// console.log(bfs(tree, 'A'));
102133

103-
console.log('start: ', arr);
104-
console.log('end: ', insertSort(arr));
105-
134+
// 节流
135+
// 如果一个函数持续地触发,那么在固定的事件执行一次
136+
function throttle(fn, ms=1000) {
137+
let cd = true
138+
return function() {
139+
if (!cd) return
140+
cd = false
141+
setTimeout(_=> {
142+
fn.apply(this, arguments)
143+
cd = true
144+
}, ms)
145+
}
146+
}
147+
// 防抖
148+
// 如果一个函数持续地触发,那么只在它结束后过一段时间只执行一次
149+
function debounce(fn, ms=1000) {
150+
let timer = null
151+
return function() {
152+
clearTimeout(timer)
153+
timer = setTimeout(_=> {
154+
fn.apply(this, arguments)
155+
}, ms)
156+
}
157+
}
158+
function sayHi(name) {
159+
console.log(name);
160+
}
161+
let fn = debounce(sayHi)
162+
window.onmousemove = function() {
163+
fn('debounce: hans')
164+
}
165+
let fn2 = throttle(sayHi)
166+
setInterval(_=> {
167+
fn2('throttle: hans')
168+
}, 10)
106169
</script>
107170
</html>

知识点/readme.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
## list
22

3-
- [数组常用api](https://github.com/haima16/JavaScript/issues/1)
3+
- [数组常用api](https://github.com/haima16/JavaScript/issues/1)
4+
- [BFS & DFS]()
5+
- [函数防抖与节流]()
6+
- [排序算法]

0 commit comments

Comments
 (0)