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;
}
}