8000 feat(min heap): Add method of getting the first element · HuboArch/algorithms-in-java@d0a9d74 · GitHub
[go: up one dir, main page]

Skip to content

Commit d0a9d74

Browse files
author
agribeau
committed
feat(min heap): Add method of getting the first element
1 parent 23f1610 commit d0a9d74

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/data_structure/heap/MinHeap.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,24 @@ public boolean remove(Object o) {
8888
}
8989
}
9090

91+
/**
92+
* 移除最小堆的首节点
93+
*
94+
* @return 首节点元素
95+
*/
96+
public E poll() {
97+
int s = data.size();
98+
if (s == 0) {
99+
return null;
100+
}
101+
102+
E result = data.get(0);
103+
E x = data.remove(s - 1);
104+
siftDown(0, x);
105+
106+
return result;
107+
}
108+
91109
/*=============================================================*/
92110
/*========================== 私有方法 ==========================*/
93111
/*=============================================================*/

0 commit comments

Comments
 (0)
0