File tree 9 files changed +133
-0
lines changed
9 files changed +133
-0
lines changed Original file line number Diff line number Diff line change
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version" : " 0.2.0" ,
6
+ "configurations" : [
7
+ {
8
+ "type" : " pwa-chrome" ,
9
+ "request" : " launch" ,
10
+ "name" : " Launch Chrome against localhost" ,
11
+ "url" : " http://localhost:8080" ,
12
+ "webRoot" : " ${workspaceFolder}"
13
+ }
14
+ ]
15
+ }
Original file line number Diff line number Diff line change
1
+ import './树/Bst'
2
+
File renamed without changes.
Original file line number Diff line number Diff line change
1
+ import { Bst } from "../树/Bst"
2
+
3
+
4
+ describe ( "二叉搜索树" , ( ) => {
5
+ const bts = new Bst ( )
6
+ bts . add ( 7 )
7
+ bts . add ( 4 )
8
+ bts . add ( 9 )
9
+ bts . add ( 2 )
10
+ bts . add ( 5 )
11
+ bts . add ( 8 )
12
+ bts . add ( 11 )
13
+ bts . add ( 3 )
14
+ bts . add ( 12 )
15
+ test ( "是否包含某个元素" , ( ) => {
16
+ expect ( bts . contains ( 1 ) ) . toBe ( false )
17
+ expect ( bts . contains ( 1 ) ) . toBe ( false )
18
+ expect ( bts . contains ( 5 ) ) . toBe ( true )
19
+ expect ( bts . contains ( 3 ) ) . toBe ( true )
20
+ } )
21
+ test ( "清空搜索树" , ( ) => {
22
+ bts . clear ( )
23
+ expect ( bts . contains ( 7 ) ) . toBe ( false )
24
+ expect ( bts . contains ( 5 ) ) . toBe ( false )
25
+ expect ( bts . contains ( 3 ) ) . toBe ( false )
26
+ } )
27
+ } )
Original file line number Diff line number Diff line change
1
+ class Node {
2
+ target = null
3
+ leftNode = null
4
+ rightNode = null
5
+ parent = null
6
+ constructor ( target , parent ) {
7
+ this . target = target
8
+ this . parent = parent
9
+ }
10
+ }
11
+
12
+ export class Bst {
13
+ #root = null
14
+ size = 0
15
+
16
+ isEmpty ( ) {
17
+ return this . size === 0 ;
18
+ }
19
+ clear ( ) {
20
+ this . #root = null
21
+ }
22
+ add ( target ) {
23
+ // 添加根节点
24
+ if ( this . #root == null ) {
25
+ this . #root = new Node ( target , null )
26
+ this . size ++
27
+ return
28
+ }
29
+ let node = this . #root;
30
+ let parent = node
31
+ let ins = 0 ;
32
+ while ( node != null ) {
33
+ ins = this . compare ( target , node . target )
34
+ parent = node
35
+ if ( ins > 0 ) {
36
+ node = node . rightNode
37
+ } else if ( ins < 0 ) {
38
+ node = node . leftNode
39
+ } else {
40
+ return
41
+ }
42
+ }
43
+ const newNode = new Node ( target , parent )
44
+ if ( ins > 0 ) {
45
+ parent . rightNode = newNode
46
+ } else {
47
+ parent . leftNode = newNode
48
+ }
49
+ this . size ++
50
+ }
51
+ compare ( e1 , e2 ) {
52
+ return e1 - e2
53
+ }
54
+ contains ( target ) {
55
+ let root = this . #root
56
+ while ( root != null ) {
57
+ if ( root . target === target ) return true ;
58
+ if ( root . target > target ) {
59
+ root = root . leftNode
60
+ } else {
61
+ root = root . rightNode
62
+ }
63
+ }
64
+ return false
65
+ }
66
+ }
67
+
68
+ const bts = new Bst ( )
69
+ bts . add ( 7 )
70
+ bts . add ( 4 )
71
+ bts . add ( 9 )
72
+ bts . add ( 2 )
73
+ bts . add ( 5 )
74
+ bts . add ( 8 )
75
+ bts . add ( 11 )
76
+ bts . add ( 3 )
77
+ bts . add ( 12 )
78
+
79
+ // console.log( bts.contains(29) )
80
+ // console.log(bts)
Original file line number Diff line number Diff line change
1
+ # 树
2
+ > 应用: 文件夹结构,组织架构,搜索引擎,Dom节点 等等
3
+
4
+ ![ ] ( images/2021-06-02-09-15-43.png )
5
+ ![ ] ( images/2021-06-02-09-18-49.png )
6
+ ![ ] ( images/2021-06-02-09-20-16.png )
7
+
8
+ ## 二叉树(Binary tree)
9
+ > 每个节点的度最多为2 即最多有两个子树
You can’t perform that action at this time.
0 commit comments