Skip to content

Commit 2e80912

Browse files
committed
fix bug on merge node logic in delete key function
1 parent a2f514f commit 2e80912

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

B-Tree/BTree/Delete.class.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,27 @@ private function _canMergeImmediately (BTree_Node $parent, BTree_Node $nextParen
265265
* @param bool $isSaveParent 上级节点是否已保存
266266
* @return BTree_Node 左侧节点
267267
*/
268-
private function _mergeStore (BTree_Node $left, BTree_Node $right, BTree_Node $parent, BTree_Node $nextParent, $midKey, $midValue, $key, $isSaveParent = false) {
268+
private function _mergeStore (
269+
BTree_Node $left,
270+
BTree_Node $right,
271+
BTree_Node $parent,
272+
BTree_Node $nextParent,
273+
$midKey,
274+
$midValue,
275+
$key,
276+
$isSaveParent = false
277+
) {
278+
279+
if (BTree_Validate::value($left->match($key))) {
280+
281+
$left->delete($key, BTree_Node::DELETE_FLAG_RIGHT);
282+
} else {
283+
284+
$right->delete($key, BTree_Node::DELETE_FLAG_RIGHT);
285+
}
269286

270287
$left->insert($midKey, $midValue, $left->rightBorderChild(), $right->leftBorderChild());
271288
$left->merge($right);
272-
$left->delete($key, BTree_Node::DELETE_FLAG_RIGHT);
273289
$parent->delete($midKey, BTree_Node::DELETE_FLAG_RIGHT);
274290

275291
if (!$isSaveParent) {

B-Tree/BTree/Node.class.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,14 @@ public function delete ($key, $deleteFlag = self::DELETE_FLAG_LEFT) {
143143

144144
public function leftBorderChild () {
145145

146-
return $this->_children[0];
146+
reset($this->_children);
147+
148+
return count($this->_children) > 0 ? current($this->_children) : 0;
147149
}
148150

149151
public function rightBorderChild () {
150152

151-
return end($this->_children);
153+
return count($this->_children) > 0 ? end($this->_children) : 0;
152154
}
153155

154156
public function pointerLeftChild ($pointer) {
@@ -233,7 +235,9 @@ public function replaceKey ($keyOld, $keyNew, $valueNew) {
233235

234236
public function isLeaf () {
235237

236-
return 0 == count($this->_children) || $this->_children[0] <= 0;
238+
reset($this->_children);
239+
240+
return 0 == count($this->_children) || current($this->_children) <= 0;
237241
}
238242

239243
/**
@@ -250,7 +254,9 @@ private function _dichotomySearch ($key, $list, $offset = 0) {
250254

251255
if (1 == $count) {
252256

253-
return strval($key) > strval($list[0]) ? $offset + 1 : $offset;
257+
reset($list);
258+
259+
return strval($key) > strval(current($list)) ? $offset + 1 : $offset;
254260
}
255261

256262
$offsetMiddle = floor($count / 2);
@@ -266,7 +272,7 @@ public function getLeftBorderKey () {
266272

267273
$keyList = array_keys($this->_data);
268274

269-
return $keyList[0];
275+
return current($keyList);
270276
}
271277

272278
public function getRightBorderKey () {

0 commit comments

Comments
 (0)