-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinary-search-st.ts
More file actions
69 lines (61 loc) · 1.43 KB
/
binary-search-st.ts
File metadata and controls
69 lines (61 loc) · 1.43 KB
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
import { ISearch } from '@/types'
/**
* 二分查找(基于有序数组)
*/
export default class BinarySearchST<Key, Value> implements ISearch<Key, Value> {
private keys: Key[]
private vals: Value[]
private N: number
constructor() {
this.keys = []
this.vals = []
this.N = 0
}
size(): number {
return this.N
}
isEmpty(): boolean {
return this.N === 0
}
contains(key: Key) {
return this.get(key) != null
}
getKeys() {
return this.keys
}
get(key: Key): Value | null {
if (this.isEmpty()) return null
const i = this.rank(key, 0, this.N - 1)
if (i < this.N && this.keys[i] === key) {
return this.vals[i]
} else {
return null
}
}
// 查找键,找到则更新值,否则创建新的元素:w
put(key: Key, val: Value) {
const i = this.rank(key, 0, this.N - 1)
if (i < this.N && this.keys[i] === key) {
this.vals[i] = val
return
}
for (let j = this.N; j > i; j--) {
this.keys[j] = this.keys[j - 1]
this.vals[j] = this.vals[j - 1]
}
this.keys[i] = key
this.vals[i] = val
this.N++
}
rank(key: Key, lo: number, hi: number): number {
if (hi < lo) return lo
const mid = Math.floor(lo + (hi - lo) / 2)
if (key < this.keys[mid]) {
return this.rank(key, lo, mid - 1)
} else if (key > this.keys[mid]) {
return this.rank(key, mid + 1, hi)
} else {
return mid
}
}
}