1 | #二叉堆 Heap\<T>
|
2 |
|
3 | 二叉堆是一种特殊的堆,二叉堆是完全二元树(二叉树)或者是近似完全二元树(二叉树)。二叉堆有两种:最大堆和最小堆。最大堆:父结点的键值总是大于或等于任何一个子节点的键值;最小堆:父结点的键值总是小于或等于任何一个子节点的键值。
|
4 | ![Heap](https://gss2.bdstatic.com/-fo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike92%2C5%2C5%2C92%2C30/sign=74466a96fad3572c72ef948eeb7a0842/77c6a7efce1b9d16836da71efbdeb48f8c546422.jpg)
|
5 |
|
6 |
|
7 | ### api适用于最大堆和最小堆,此api以最大堆为例
|
8 | ## 基本操作的API及示例
|
9 |
|
10 | ### Constructor
|
11 | ##### new MaxHeap(key ?: keyof T)
|
12 | ``` text
|
13 | 实例:
|
14 | const MaxHeap = new MaxHeap();
|
15 |
|
16 | const MaxHeap = new MaxHeap<{key:number}>("key");
|
17 | ```
|
18 |
|
19 | ### 插入 add
|
20 | ##### MaxHeap add(T value)
|
21 | ``` text
|
22 | 实例:
|
23 | const maxHeap = new MaxHeap();
|
24 | maxHeap.add(1);
|
25 | maxHeap.add(2);
|
26 | maxHeap.add(3);
|
27 | ```
|
28 |
|
29 | ### 取堆顶 poll
|
30 | ##### poll(): T
|
31 | ``` text
|
32 | 实例:
|
33 | const maxHeap = new MaxHeap();
|
34 | maxHeap.add(1);
|
35 | maxHeap.add(2);
|
36 | maxHeap.add(3);
|
37 |
|
38 | const value = maxHeap.poll();
|
39 | console.log(value);
|
40 | // 3
|
41 | ```
|
42 |
|
43 | ### 删除 remove
|
44 | ##### remove(item: ((item: T) => boolean)|T): boolean
|
45 | ``` text
|
46 | 实例:
|
47 | const maxHeap = new MaxHeap();
|
48 | maxHeap.add(1);
|
49 | maxHeap.add(2);
|
50 | maxHeap.add(3);
|
51 |
|
52 | maxHeap.remove(2);
|
53 | maxHeap.remove(item => item === 3);
|
54 | ```
|
55 |
|
56 | ### 查看堆顶 peek
|
57 | ##### peek(): T
|
58 | ``` text
|
59 | 实例:
|
60 | const maxHeap = new MaxHeap();
|
61 | maxHeap.add(1);
|
62 | maxHeap.add(2);
|
63 | maxHeap.add(3);
|
64 |
|
65 | const value = maxHeap.peek();
|
66 | console.log(value);
|
67 | // 3
|
68 |
|
69 | 描述:
|
70 | 不影响堆结构
|
71 | ```
|
72 |
|
73 | ### 是否为空 isEmpty
|
74 | ##### isEmpty(): boolean
|
75 | ``` text
|
76 | 实例:
|
77 | const maxHeap = new MaxHeap();
|
78 | maxHeap.add(1);
|
79 | maxHeap.add(2);
|
80 | maxHeap.add(3);
|
81 |
|
82 | const value = maxHeap.isEmpty();
|
83 | console.log(value);
|
84 | // false
|
85 | ```
|
86 |
|
87 | ### 查找 find
|
88 | ##### find(): T
|
89 | ``` text
|
90 | 实例:
|
91 | const maxHeap = new MaxHeap();
|
92 | maxHeap.add(1);
|
93 | maxHeap.add(2);
|
94 | maxHeap.add(3);
|
95 |
|
96 | maxHeap.find(2);
|
97 | const value = maxHeap.find(item => item === 2);
|
98 | console.log(value);
|
99 | // 2
|
100 | ```
|
101 |
|
102 | ### 查找所有 findAll
|
103 | ##### findAll(): T[]
|
104 | ``` text
|
105 | 实例:
|
106 | const maxHeap = new MaxHeap();
|
107 | maxHeap.add(1);
|
108 | maxHeap.add(2);
|
109 | maxHeap.add(3);
|
110 |
|
111 | const value = maxHeap.findAll(item => item > 1);
|
112 | console.log(value);
|
113 | // [1, 2]
|
114 | ```
|
115 |
|
116 |
|
117 | ### 清空 clear
|
118 | ##### clear(): void
|
119 | ``` text
|
120 | 实例:
|
121 | const maxHeap = new MaxHeap();
|
122 | maxHeap.add(1);
|
123 | maxHeap.add(2);
|
124 | maxHeap.add(3);
|
125 |
|
126 | maxHeap.clear();
|
127 | console.log(maxHeap.Size);
|
128 | // 0
|
129 | ```
|
130 |
|
131 | ### 清空 entries
|
132 | ##### entries(): T[]
|
133 | ``` text
|
134 | 实例:
|
135 | const maxHeap = new MaxHeap();
|
136 | maxHeap.add(1);
|
137 | maxHeap.add(2);
|
138 | maxHeap.add(3);
|
139 |
|
140 | const values = maxHeap.entries();
|
141 | console.log(values);
|
142 | // [1, 2, 3]
|
143 | ``` |
\ | No newline at end of file |