• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PHP 二叉树 二叉排序树实现

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
<?php
/**
 * PHP 二叉树
 * @author : xiaojiang 2014-01-01
 * */
class Tree {
    
    protected $k = null;
    protected $left = null;
    protected $right = null;
    
    public function __construct( $k= null , $left = null, $right = null){
    
        $this->k = $k;
        $this->left = $left;
        $this->right = $right;
    }
    
    public function isEmpty(){
        return !isset($this->k);
    }
    
    public function getKey(){
        return isset($this->k) ? $this->k : false;
    }
    
    public function setKey($k){
        
        if(!$this->isEmpty())
            return ;
        $this->k = $k;
        $this->left = new Tree();
        $this->right = new Tree();
        return true;
    }
    
    public function deleteKey(){
        if(!$this->isLeaf())
            return false;
        $ret  = $this->k;
        $this->k = null;
        $this->left = null;
        $this->right = null;
        return $ret;
    }
    
    public function setLeftKey( Tree $obj){
        if( $this->left || !$this->left->isEmpty())
            return false;
        $this->left = $obj;
    }
    
    public function delLeftKey(){
    
        if($this->isEmpty())
            return;
        $ret = $this->left ;
        $this->left = new Tree();
        return $ret;
    }
    
    
    public function setRightKey( Tree $obj){
        if( $this->left || !$this->left->isEmpty())
            return false;
        $this->left = $obj;
    }
    
    public function delRightKey(){
    
        if($this->isEmpty())
            return;
        $ret = $this->left ;
        $this->left = new Tree();
        return $ret;
    }
    
    
}

/**
 * 二叉树排序
 * @author: xiaojiang 
 * */

class bTree extends Tree{
    
    static public function initbTree($array){
        
        $root = new bTree();
        foreach($array as $val){
            $root->insert($val);
        }
        return $root;
    }
    
    public function compare($obj){
        return $this->k - $obj;    
    }
    
    public function contain($obj){
    
        if ($this->isEmpty())
            return false;
        $diff = $this->compare($obj);
        if($diff == 0){
            return true;
        }else if($diff > 0){
            return $this->right->contain($obj);
        }else {
            return $this->left->contain($obj);
        }
    }
    
    public function insert($obj){
        
        if($this->isEmpty()){
            $this->setKey($obj);
        }else{
            $diff = $this->compare($obj);
            if($diff > 0){
                $this->left->insert($obj);
            }else{
                $this->right->insert($obj);
            }
        }
        $this->_afterInsert();
    }
    
    public function findMax(){
        if($this->isEmpty())
            return;
        if(!$this->right->isEmpty())
            return $this->right->findMax();
        else     
            return $this->k;
    }
    
    public function findMin(){
    
        if($this->isEmpty())
            return ;
        if(!$this->left->isEmpty())
            return $this->left->findMin();
        else 
            return $this->k;
    }
    
    public function setKey($k){
        
        if(!$this->isEmpty())
            return ;
        $this->k = $k;
        $this->left = new bTree();
        $this->right = new bTree();
        return true;
    }
    
    protected function _afterInsert(){}
}

$arr = array(1,5,4,5,10);

$btree = bTree::initbTree($arr);

echo $btree->findMax();    // 10 

echo "<br>";

echo $btree->findMin();   // 1

 

非常适用于多数字排序。。避免了批量循环的重复判断··· 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[转]小心PHP的类定义顺序与继承的问题发布时间:2022-07-10
下一篇:
phpsocket的一些问题发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap