php 实现二叉查找树

class btree{
    
    public $head;
    
    public function __construct(){
        $this->head = null;
    }
    
    public function get($key, $node = null){
    
        if (!$this->head) return false;
    
        $node = $node ? $node : $this->head;
        
        while ($node){
            
            //找到直接返回
            if ($node->key == $key){
                return $node;
            }
            
            //查找的值小于父节点,查找左儿子节点
            if ($key < $node->key){
                $node = $node->left;
            }
            
            //查找的值大于父节点,查找右儿子节点
            if ($key > $node->key){
                $node = $node->right;
            }
            
        }
    
        return false;
    
    }
    
    public function get2($key, $node = null){
        
        if (!$this->head) return false;
        
        $node = $node ? $node : $this->head;
        
        //找到直接返回
        if ($node->key == $key){
            return $node;
        }
        
        //查找的值小于父节点,查找左儿子节点
        if ($key < $node->key){
            return $node->left ? $this->get($key, $node->left) : false;
        }
        
        //查找的值大于父节点,查找右儿子节点
        if ($key > $node->key){
            return $node->right ? $this->get($key, $node->right) : false;
        }
        
    }
    
    public function insert($new_node){

        if (!$this->head){
            $this->head = &$new_node;
            return true;
        }
        
        $current_node = $this->head;
        
        while ($current_node){
            $parent_node = $current_node;
            if ($new_node->key > $current_node->key){
                $current_node = $current_node->right;
            }elseif ($new_node->key < $current_node->key){
                $current_node = $current_node->left;
            }else{
                return false;
            }
        }
        
        if ($new_node->key > $parent_node->key){
            $parent_node->right = &$new_node;
        }else{
            $parent_node->left = &$new_node;
        }
        
        return true;
        
    }

    public function insert2($new_node, &$current_node = null){
        
        if (!$this->head){
            $this->head = &$new_node;
            return true;
        }
        
        $current_node = $current_node ?? $this->head;
        
        if (!$current_node){           
            $current_node = &$new_node;
            return true;
        }
        
        if ($new_node->key > $current_node->key){
            $this->insert($new_node, $current_node->right);
        }elseif ($new_node->key < $current_node->key){
            $this->insert($new_node, $current_node->left);
        }
        
        return false;
        
    }
    
}

class Node{
    
    public $key;
    
    public $data;
    
    public $left;
    
    public $right;
    
    public function __construct($key, $data){
        $this->key = $key;
        $this->data = $data;
    }
    
}


$b = new btree();
$b->insert(new Node(1, 1));
$b->insert(new Node(2, 2));;
$b->insert(new Node(3, 3));
$b->insert(new Node(4, 4));
$b->insert(new Node(5, 5));
$re = $b->get(4)->data;
var_dump($re);

输出4

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值