• PHP 二叉树 二叉排序树实现


    <?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

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

  • 相关阅读:
    BZOJ1093 [SCOI2003]字符串折叠
    BZOJ1078 [SCOI2008]斜堆
    BZOJ1089 [SCOI2003]严格n元树
    BZOJ1031 [JSOI2007]字符加密
    BZOJ1038 [ZJOI2008]瞭望塔
    BZOJ1037 [ZJOI2008]生日聚会Party
    BZOJ1041 [HAOI2008]圆上的整点
    BZOJ1026 [SCOI2009]windy数
    linux命令行计算器 <转>
    正则的[]与()
  • 原文地址:https://www.cnblogs.com/glory-jzx/p/3501178.html
Copyright © 2020-2023  润新知