二叉排序樹

代碼實現

<?php
//二叉排序樹
class BinarySortTree{
    public $left;
    public $right;
    public $parent;
    public $value;

    public function insert($value){
        if(!$this->value){
            $this->value = $value;
            $this->parent = null;
            $this->left = null;
            $this->right = null;
            return true;
        }

        if($this->value == $value){
            return false;
        }

        if($value > $this->value){
            if($this->right){
                return $this->right->insert($value);
            }else{
                $tree = new BinarySortTree();
                $tree->parent = $this;
                $tree->value = $value;
                $tree->left = null;
                $tree->right = null;
                $this->right = $tree;
                return true;
            }
        }

        if($value < $this->value){
            if($this->left){
                return $this->left->insert($value);
            }else{
                $tree = new BinarySortTree();
                $tree->parent = $this;
                $tree->value = $value;
                $tree->left = null;
                $tree->right = null;
                $this->left = $tree;
                return true;
            }
        }
    }

    public function search($value){
        if($this->value == $value){
            return true;
        }

        if($this->value > $value){
            if($this->left){
                return $this->left->search($value);
            }else{
                return false;
            }
        }

        if($this->value < $value){
            if($this->right){
                return $this->right->search($value);
            }else{
                return false;
            }
        }
    }

    public function delete($value){
        if($this->value == $value){
            //待刪除節點沒有左右子節點
            if(!$this->left && !$this->right){
                if($this->parent){
                    if($this->value > $this->parent->value){
                        $this->parent->right = null;
                    }
                    if($this->value < $this->parent->value){
                        $this->parent->left = null;
                    }
                }else{
                    $this->value = null;
                }
                $this->destroy();
                return true;
            }

            //待刪除節點只有左節點
            if($this->left && !$this->right){
                if($this->parent){
                    if($this->left->value > $this->parent->value){
                        $this->parent->right = $this->left;
                    }
                    if($this->left->value < $this->parent->value){
                        $this->parent->left = $this->left;
                    }
                    $this->destroy();
                }else{
                    $t = $this->left;
                    $this->value = $t->value;

                    if($t->left){
                        $t->left->parent = $this;
                    }

                    if($t->right){
                        $t->right->parent = $this;
                    }

                    $this->left = $t->left;
                    $this->right = $t->right;
                    $t->destroy();
                }
                return true;
            }

            //待刪除節點只有右節點
            if($this->right && !$this->left){
                if($this->parent){
                    if($this->right->value > $this->parent->value){
                        $this->parent->right = $this->right;
                    }
                    if($this->right->value < $this->parent->value){
                        $this->parent->left = $this->right;
                    }
                    $this->destroy();
                }else{
                    $t = $this->right;
                    $this->value = $t->value;
                    if($t->left){
                        $t->left->parent = $this;
                    }

                    if($t->right){
                        $t->right->parent = $this;
                    }
                    $this->left = $t->left;
                    $this->right = $t->right;
                    $t->destroy();
                }
                return true;
            }

            //待刪除節點左右節點都有
            if($this->left && $this->right){
                $t = $this->right;
                //待刪除節點的右節點沒有左節點時
                if(!$t->left){
                    $this->value = $t->value;
                    $this->right = $t->right;
                }else{
                    while($t->left){
                        $t = $t->left;
                    }
                    $this->value = $t->value;
                    $t->parent->right = $t->right;
                }
                $t->destroy();
                return true;
            }
        }

        if($this->value > $value && $this->left){
            return $this->left->delete($value);
        }

        if($this->value < $value && $this->right){
            return $this->right->delete($value);
        }
    }

    public function destroy(){
        $this->parent = null;
        $this->left = null;
        $this->right = null;
        $this->value = null;
    }

    //廣度優先遍歷
    public static function levelDisplay(BinarySortTree $tree){
        $queue = new SplQueue();
        $queue->enqueue($tree);
        while (!$queue->isEmpty()){
            $node = $queue->dequeue();
            echo $node->value."<br/>";
            if($node->left){
                $queue->enqueue($node->left);
            }
            if($node->right){
                $queue->enqueue($node->right);
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章