@@ -44,6 +44,14 @@ class BTree_Node {
4444 */
4545 private $ _pointer ;
4646
47+ /**
48+ * 构造函数
49+ *
50+ * @param arary $data 数据
51+ * @param array $children 子节点指针
52+ * @param BTree_Node|null $parent 上级节点
53+ * @param int $pointer 当前节点指针
54+ */
4755 public function __construct ($ data = array (), $ children = array (), $ parent = NULL , $ pointer = self ::POINTER_NEW ) {
4856
4957 $ keyList = array_map ('strval ' , array_keys ($ data ));
@@ -56,11 +64,22 @@ public function __construct ($data = array(), $children = array(), $parent = NU
5664 $ this ->_pointer = $ pointer ;
5765 }
5866
67+ /**
68+ * 返回指针
69+ *
70+ * @return int 当前节点指针
71+ */
5972 public function pointer () {
6073
6174 return $ this ->_pointer ;
6275 }
6376
77+ /**
78+ * 匹配关键词对应的值
79+ *
80+ * @param string $key 关键词
81+ * @return int|bool 值|如果关键词不存在返回false
82+ */
6483 public function match ($ key ) {
6584
6685 if (isset ($ this ->_data [$ key ])) {
@@ -71,6 +90,12 @@ public function match ($key) {
7190 return false ;
7291 }
7392
93+ /**
94+ * 匹配关键词对应的子节点指针
95+ *
96+ * @param string $key 关键词
97+ * @return int|bool 子节点指针|如果关键词存在返回false
98+ */
7499 public function matchChildren ($ key ) {
75100
76101 $ keyList = array_keys ($ this ->_data );
@@ -87,6 +112,12 @@ public function matchChildren ($key) {
87112
88113 /**
89114 * 插入key
115+ *
116+ * @param string $key 关键词
117+ * @param int $value 值
118+ * @param int $pointerLeft 左侧子节点指针
119+ * @param int $pointerRight 右侧子节点指针
120+ * @return bool 执行结果
90121 */
91122 public function insert ($ key , $ value , $ pointerLeft = 0 , $ pointerRight = 0 ) {
92123
@@ -106,10 +137,16 @@ public function insert ($key, $value, $pointerLeft = 0, $pointerRight = 0) {
106137 array_splice ($ this ->_children , $ offset , 1 , array ($ pointerLeft , $ pointerRight ));
107138 $ this ->_children = array_slice ($ this ->_children , 0 , count ($ keyList ) + 1 );
108139 $ this ->_data = array_combine ($ keyList , $ valueList );
140+
141+ return true ;
109142 }
110143
111144 /**
112145 * 删除key
146+ *
147+ * @param string $key 关键词
148+ * @param string $deleteFlag 从关键词左侧或者右侧删除子节点指针
149+ * @return bool 执行结果
113150 */
114151 public function delete ($ key , $ deleteFlag = self ::DELETE_FLAG_LEFT ) {
115152
@@ -139,20 +176,38 @@ public function delete ($key, $deleteFlag = self::DELETE_FLAG_LEFT) {
139176 }
140177
141178 $ this ->_data = array_combine ($ keyList , $ valueList );
179+
180+ return true ;
142181 }
143182
183+ /**
184+ * 左边缘子节点指针
185+ *
186+ * @return int 子节点指针
187+ */
144188 public function leftBorderChild () {
145189
146190 reset ($ this ->_children );
147191
148192 return count ($ this ->_children ) > 0 ? current ($ this ->_children ) : 0 ;
149193 }
150194
195+ /**
196+ * 右边缘子节点指针
197+ *
198+ * @return int 子节点指针
199+ */
151200 public function rightBorderChild () {
152201
153202 return count ($ this ->_children ) > 0 ? end ($ this ->_children ) : 0 ;
154203 }
155204
205+ /**
206+ * 指针左侧相邻子节点指针
207+ *
208+ * @param int $pointer 指针
209+ * @return int 子节点指针
210+ */
156211 public function pointerLeftChild ($ pointer ) {
157212
158213 $ offset = array_search ($ pointer , $ this ->_children );
@@ -162,6 +217,12 @@ public function pointerLeftChild ($pointer) {
162217 : self ::POINTER_NEW ;
163218 }
164219
220+ /**
221+ * 指针右侧相邻子节点指针
222+ *
223+ * @param int $pointer 指针
224+ * @return int 子节点指针
225+ */
165226 public function pointerRightChild ($ pointer ) {
166227
167228 $ offset = array_search ($ pointer , $ this ->_children );
@@ -171,6 +232,12 @@ public function pointerRightChild ($pointer) {
171232 : self ::POINTER_NEW ;
172233 }
173234
235+ /**
236+ * 指针左侧相邻关键词
237+ *
238+ * @param int $pointer 指针
239+ * @return string 关键词
240+ */
174241 public function pointerLeftKey ($ pointer ) {
175242
176243 $ offset = array_search ($ pointer , $ this ->_children );
@@ -181,6 +248,12 @@ public function pointerLeftKey ($pointer) {
181248 : NULL ;
182249 }
183250
251+ /**
252+ * 指针右侧相邻关键词
253+ *
254+ * @param int $pointer 指针
255+ * @return string 关键词
256+ */
184257 public function pointerRightKey ($ pointer ) {
185258
186259 $ offset = array_search ($ pointer , $ this ->_children );
@@ -191,6 +264,12 @@ public function pointerRightKey ($pointer) {
191264 : NULL ;
192265 }
193266
267+ /**
268+ * 关键词左侧子节点指针
269+ *
270+ * @param string $key 关键词
271+ * @return int|bool 子节点指针|失败返回false
272+ */
194273 public function keyLeftChild ($ key ) {
195274
196275 $ keyList = array_keys ($ this ->_data );
@@ -204,6 +283,12 @@ public function keyLeftChild ($key) {
204283 return $ this ->_children [$ offset ];
205284 }
206285
286+ /**
287+ * 关键词右侧子节点指针
288+ *
289+ * @param string $key 关键词
290+ * @return int|bool 子节点指针|失败返回false
291+ */
207292 public function keyRightChild ($ key ) {
208293
209294 $ keyList = array_keys ($ this ->_data );
@@ -217,6 +302,14 @@ public function keyRightChild ($key) {
217302 return $ this ->_children [$ offset + 1 ];
218303 }
219304
305+ /**
306+ * 替换关键词
307+ *
308+ * @param string $keyOld 目标关键词
309+ * @param string $keyNew 要替换的关键词
310+ * @param int $valueNew 要替换的值
311+ * @return bool 执行结果
312+ */
220313 public function replaceKey ($ keyOld , $ keyNew , $ valueNew ) {
221314
222315 $ keyList = array_keys ($ this ->_data );
@@ -231,8 +324,15 @@ public function replaceKey ($keyOld, $keyNew, $valueNew) {
231324 array_splice ($ keyList , $ offset , 1 , $ keyNew );
232325 array_splice ($ valueList , $ offset , 1 , $ valueNew );
233326 $ this ->_data = array_combine ($ keyList , $ valueList );
327+
328+ return true ;
234329 }
235330
331+ /**
332+ * 返回本节点是否是叶节点
333+ *
334+ * @return bool 判断结果
335+ */
236336 public function isLeaf () {
237337
238338 reset ($ this ->_children );
@@ -241,7 +341,12 @@ public function isLeaf () {
241341 }
242342
243343 /**
244- * 二分法查找位置 (该值不存在)
344+ * 二分法查找位置 (该关键词不存在)
345+ *
346+ * @param string $key 关键词
347+ * @param array $list 关键词列表
348+ * @param int $offset 初始位置
349+ * @return int 目地位置
245350 */
246351 private function _dichotomySearch ($ key , $ list , $ offset = 0 ) {
247352
@@ -268,20 +373,35 @@ private function _dichotomySearch ($key, $list, $offset = 0) {
268373 : $ this ->_dichotomySearch ($ key , $ listRight , $ offset + $ offsetMiddle );
269374 }
270375
271- public function getLeftBorderKey () {
376+ /**
377+ * 获取左端关键词
378+ *
379+ * @return string 关键词
380+ */
381+ public function leftBorderKey () {
272382
273383 $ keyList = array_keys ($ this ->_data );
274384
275385 return current ($ keyList );
276386 }
277387
278- public function getRightBorderKey () {
388+ /**
389+ * 获取右端关键词
390+ *
391+ * @return string 关键词
392+ */
393+ public function rightBorderKey () {
279394
280395 $ keyList = array_keys ($ this ->_data );
281396
282397 return end ($ keyList );
283398 }
284399
400+ /**
401+ * 向右侧分裂节点
402+ *
403+ * @return array|bool 成功返回数组包含 中间关键词 中间值 右侧节点|失败返回false
404+ */
285405 public function separateRight () {
286406
287407 $ count = count ($ this ->_data );
@@ -310,6 +430,11 @@ public function separateRight () {
310430 return array ($ midKey , $ midValue , $ rightNode );
311431 }
312432
433+ /**
434+ * 合并节点
435+ *
436+ * @param BTree_Node $node 待合并节点
437+ */
313438 public function merge (BTree_Node $ node ) {
314439
315440 $ offset = 0 ;
@@ -322,28 +447,53 @@ public function merge (BTree_Node $node) {
322447 }
323448 }
324449
450+ /**
451+ * 返回上级节点
452+ *
453+ * @return BTree_Node 上级节点
454+ */
325455 public function parent () {
326456
327457 return $ this ->_parent instanceof self
328458 ? $ this ->_parent
329459 : new self (array (), array (), NULL , self ::POINTER_NEW_ROOT );
330460 }
331461
462+ /**
463+ * 返回子节点指针列表
464+ *
465+ * @return array 子节点指针列表
466+ */
332467 public function children () {
333468
334469 return $ this ->_children ;
335470 }
336471
472+ /**
473+ * 返回当前节点数据
474+ *
475+ * @return array 当前节点数据
476+ */
337477 public function data () {
338478
339479 return $ this ->_data ;
340480 }
341481
482+ /**
483+ * 判断是否为新节点
484+ *
485+ * @return bool 判断结果
486+ */
342487 public function isNew () {
343488
344489 return self ::POINTER_NEW == $ this ->pointer ();
345490 }
346491
492+ /**
493+ * 判断是否为新的根节点
494+ *
495+ * @return bool 判断结果
496+ */
347497 public function isNewRoot () {
348498
349499 return self ::POINTER_NEW_ROOT == $ this ->pointer ();
0 commit comments