@@ -88,7 +88,7 @@ class Queue {
88
88
this .queue .push (item)
89
89
}
90
90
deQueue () {
91
- this .queue .shift ()
91
+ return this .queue .shift ()
92
92
}
93
93
getHeader () {
94
94
return this .queue [0 ]
@@ -142,6 +142,7 @@ class SqQueue {
142
142
if (this .size === this .getLength () / 4 && this .getLength () / 2 !== 0 ) {
143
143
this .resize (this .getLength () / 2 )
144
144
}
145
+ return r
145
146
}
146
147
getHeader () {
147
148
if (this .isEmpty ()) {
@@ -277,7 +278,6 @@ class Node {
277
278
this .right = null
278
279
}
279
280
}
280
-
281
281
class BST {
282
282
constructor () {
283
283
this .root = null
@@ -290,15 +290,19 @@ class BST {
290
290
return this .size === 0
291
291
}
292
292
addNode (v ) {
293
- this .root = this .addChild (this .root , v)
293
+ this .root = this ._addChild (this .root , v)
294
294
}
295
295
// 添加节点时,需要比较添加的节点值和当前
296
296
// 节点值的大小
297
- addChild (node , v ) {
297
+ _addChild (node , v ) {
298
+ if (! node) {
299
+ this .size ++
300
+ return new Node (v)
301
+ }
298
302
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)
302
306
}
303
307
return node
304
308
}
@@ -313,40 +317,56 @@ class BST {
313
317
// 先序遍历可用于打印树的结构
314
318
// 先序遍历表示先访问根节点,然后访问左节点,最后访问右节点。
315
319
preTraversal () {
316
- pre (this .root )
320
+ this . _pre (this .root )
317
321
}
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 )
323
327
}
324
328
}
325
329
// 中序遍历可用于排序
326
330
// 对于 BST 来说,中序遍历可以实现一次遍历就
327
331
// 得到有序的值
328
332
// 中序遍历表示先访问左节点,然后访问根节点,最后访问右节点。
329
333
midTraversal () {
330
- pre (this .root )
334
+ this . _mid (this .root )
331
335
}
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 )
337
341
}
338
342
}
339
343
// 后序遍历可用于先操作子节点
340
344
// 再操作父节点的场景
341
345
// 后序遍历表示先访问左节点,然后访问右节点,最后访问根节点。
342
346
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
+ }
344
355
}
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 )
350
370
}
351
371
}
352
372
```
0 commit comments