Skip to content

Commit 9191b73

Browse files
author
YuChengKai
committed
广度遍历
1 parent 36d6ae0 commit 9191b73

File tree

1 file changed

+45
-25
lines changed

1 file changed

+45
-25
lines changed

DataStruct/dataStruct-zh.md

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class Queue {
8888
this.queue.push(item)
8989
}
9090
deQueue() {
91-
this.queue.shift()
91+
return this.queue.shift()
9292
}
9393
getHeader() {
9494
return this.queue[0]
@@ -142,6 +142,7 @@ class SqQueue {
142142
if (this.size === this.getLength() / 4 && this.getLength() / 2 !== 0) {
143143
this.resize(this.getLength() / 2)
144144
}
145+
return r
145146
}
146147
getHeader() {
147148
if (this.isEmpty()) {
@@ -277,7 +278,6 @@ class Node {
277278
this.right = null
278279
}
279280
}
280-
281281
class BST {
282282
constructor() {
283283
this.root = null
@@ -290,15 +290,19 @@ class BST {
290290
return this.size === 0
291291
}
292292
addNode(v) {
293-
this.root = this.addChild(this.root, v)
293+
this.root = this._addChild(this.root, v)
294294
}
295295
// 添加节点时,需要比较添加的节点值和当前
296296
// 节点值的大小
297-
addChild(node, v) {
297+
_addChild(node, v) {
298+
if (!node) {
299+
this.size++
300+
return new Node(v)
301+
}
298302
if (node.value > v) {
299-
node.left = this.addChild(node.left, v)
300-
} else if (node.value < 0) {
301-
node.right = this.addChild(node.right, v)
303+
node.left = this._addChild(node.left, v)
304+
} else if (node.value < v) {
305+
node.right = this._addChild(node.right, v)
302306
}
303307
return node
304308
}
@@ -313,40 +317,56 @@ class BST {
313317
// 先序遍历可用于打印树的结构
314318
// 先序遍历表示先访问根节点,然后访问左节点,最后访问右节点。
315319
preTraversal() {
316-
pre(this.root)
320+
this._pre(this.root)
317321
}
318-
pre(node) {
319-
if (this.root) {
320-
console.log(this.root.value)
321-
pre(this.root.left)
322-
pre(this.root.right)
322+
_pre(node) {
323+
if (node) {
324+
console.log(node.value)
325+
this._pre(node.left)
326+
this._pre(node.right)
323327
}
324328
}
325329
// 中序遍历可用于排序
326330
// 对于 BST 来说,中序遍历可以实现一次遍历就
327331
// 得到有序的值
328332
// 中序遍历表示先访问左节点,然后访问根节点,最后访问右节点。
329333
midTraversal() {
330-
pre(this.root)
334+
this._mid(this.root)
331335
}
332-
mid(node) {
333-
if (this.root) {
334-
mid(this.root.left)
335-
console.log(this.root.value)
336-
mid(this.root.right)
336+
_mid(node) {
337+
if (node) {
338+
this._mid(node.left)
339+
console.log(node.value)
340+
this._mid(node.right)
337341
}
338342
}
339343
// 后序遍历可用于先操作子节点
340344
// 再操作父节点的场景
341345
// 后序遍历表示先访问左节点,然后访问右节点,最后访问根节点。
342346
backTraversal() {
343-
pre(this.root)
347+
this._back(this.root)
348+
}
349+
_back(node) {
350+
if (node) {
351+
this._back(node.left)
352+
this._back(node.right)
353+
console.log(node.value)
354+
}
344355
}
345-
back(node) {
346-
if (this.root) {
347-
back(this.root.left)
348-
back(this.root.right)
349-
console.log(this.root.value)
356+
```
357+
358+
以上的这几种遍历都可以称之为深度遍历,对应的还有种遍历叫做广度遍历,也就是一层层地遍历树。对于广度遍历来说,我们需要利用之前讲过的队列结构来完成。
359+
360+
```js
361+
breadthTraversal() {
362+
if (!this.root) return null
363+
let q = new Queue()
364+
q.enQueue(this.root)
365+
while (!q.isEmpty()) {
366+
let n = q.deQueue()
367+
console.log(n.value)
368+
if (n.left) q.enQueue(n.left)
369+
if (n.right) q.enQueue(n.right)
350370
}
351371
}
352372
```

0 commit comments

Comments
 (0)