php实现二分搜索树

class Node{

public $val;

public $left;

public $right;

public function __construct($val){

$this->val = $val;

$this->left = null;

$this->right = null;

}

}

 

class BST{

public $root;

public $size;

public function __construct(){

$this->root = null;

$this->size = 0;

}

 

//在bst中添加一个节点

public function add($val){

$this->root = $this->addNode($this->root, $val);

}

 

//递归调用本函数 返回值为该节点

private function addNode($node, $val){

if($node == null){

$this->size++;

return new Node($val);

}

if($val < $node->val){

$node->left = $this->addNode($node->left, $val);

}else if($val > $node->val){

$node->right = $this->addNode($node->right, $val);

}

return $node;

}

 

//bst中是否包含一个元素

public function contains($val){

return $this->isContains($this->root, $val);

}

 

//从根节点出发,进行递归查看

private function isContains($node, $val){

if($node == null)

return false;

if($node->val == $val)

return true;

elseif($val < $node->val)

return $this->isContains($node->left, $val);

else

return $this->isContains($node->right, $val);

}


 

//查找bst中值最小的节点的值

public function getMini(){

if($this->size == 0)

return;

return $this->getMiniR($this->root)->val;

}

 

//递归调用 查找bst中值最小的节点

private function getMiniR($node){

if($node->left == null)

return $node;

return $this->getMiniR($node->left);

}

 

//查找bst中值最大的节点

public function getMax(){

if($this->size == 0)

return;

return $this->getMaxR($this->root)->val;

}

 

//递归调用 查找bst中值最大的节点

private function getMaxR($node){

if($node->right == null)

return $node;

return $this->getMaxR($node->right);

}

 

//删除bst中最小的元素

public function removeMin(){

 

$this->root = $this->removeMinR($this->root);

}

 

//递归调用 从根节点开始删除最小元素

private function removeMinR($node){

if($node->left == null){

$this->size--;

return $node->right;

}

$node->left = $this->removeMinR($node->left);

return $node;

}

//删除bst中最大的元素

public function removeMax(){

 

$this->root = $this->removeMaxR($this->root);

}

 

//递归调用 从根节点开始删除最大元素

private function removeMaxR($node){

if($node->right == null){

$this->size--;

return $node->left;

}

$node->right = $this->removeMaxR($node->right);

return $node;

}

 

//bst删除任意元素节点

public function remove($val){

 

$this->root = $this->removeR($this->root, $val);

}

 

//递归调用从根节点删除节点

private function removeR($node, $val){

if($node == null)

return null;

if($val < $node->val){

$node->left = $this->removeR($node->left, $val);

return $node;

}elseif($val > $node->val){

$node->right = $this->removeR($node->right, $val);

return $node;

}else{//找到要删除的节点

if($node->left == null){

$this->size--;

return $node->right;

}

if($node->right == null){

$this->size--;

return $node->left;

}

$newNode = $this->getMiniR($node->right);

$newNode->right = $this->removeMinR($node->right);

$newNode->left = $node->left;

$node->left = null;

$node->right = null;

return $newNode;

}

}

 

//bst的前序遍历

public function preOrder(){

$this->preOrderP($this->root);

}

 

private function preOrderP($node){

if($node == null)

return;

echo $node->val . "<br/>";

$this->preOrderP($node->left);

$this->preOrderP($node->right);

}


 

//bst的中序遍历

public function inOrder(){

$this->inOrderP($this->root);

}

 

private function inOrderP($node){

if($node == null)

return;

$this->inOrderP($node->left);

echo $node->val . "<br/>";

$this->inOrderP($node->right);

}


 

//bst的后序遍历

public function postOrder(){

$this->postOrderP($this->root);

}

 

private function postOrderP($node){

if($node == null)

return;

$this->postOrderP($node->left);

$this->postOrderP($node->right);

echo $node->val . "<br/>";

}


 

public function getSize(){

return $this->size;

}

 

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值