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

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

PHP 二叉树 二叉排序树实现的更多相关文章

  1. 哈夫曼树;二叉树;二叉排序树(BST)

    优先队列:priority_queue<Type, Container, Functional>Type 为数据类型, Container 为保存数据的容器,Functional 为元素比 ...

  2. Cracking The Coding Interview 4.0_二叉树

    #include <iostream> #include <string> using namespace std; class tree { public: tree() { ...

  3. NOIP 提高组必会!(转)

    1.排序算法(快排.选择.冒泡.堆排序.二叉排序树.桶排序)2.DFS/BFS 也就是搜索算法,剪枝务必要学! 学宽搜的时候学一下哈希表!3.树 ①遍历 ②二叉树 ③二叉排序树(查找.生成.删除) ④ ...

  4. Cracking The Coding Interview 4.6

    //原文: // // Design an algorithm and write code to find the first common ancestor of two nodes in a b ...

  5. Cracking The Coding Interview4.5

    //原文: // // Write an algorithm to find the 'next' node (i.e., in-order successor) of a given node in ...

  6. Cracking The Coding Interview 4.4

    //Given a binary search tree, design an algorithm which creates a linked list of all the nodes at ea ...

  7. Cracking The Coding Interview4.3

    //Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal h ...

  8. Cracking The Coding Interview 4.1

    //Implement a function to check if a tree is balanced. For the purposes of this question, a balanced ...

  9. NOIP需要掌握的内容(大致

    1.排序算法(快排.选择.冒泡.堆排序.二叉排序树.桶排序)2.DFS/BFS 剪枝 哈希表3.树   ①遍历   ②二叉树   ③二叉排序树(查找.生成.删除)   ④堆(二叉堆.左偏树.堆排序)  ...

随机推荐

  1. Linux 安装 MongoDB数据库

    1下载: wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.7.tgz (下载较慢) 2.安装: mv mongodb-li ...

  2. java做web项目比较多

    WEB就是轻量级:如果要炫,FLEX或即将普及的html5.0都能做到像C/S那样. java做web项目比较多:如果是桌面程序,还是走C/S比较成熟. 如果是B/S架构的,后台还是JAVA,前台可以 ...

  3. Python encode() 方法

    描述 encode() 方法以指定的编码格式编码字符串,默认编码为 'utf-8'. 对应的解码方法:bytes decode()  方法. 语法 encode() 方法语法: S.encode([e ...

  4. (原)关于sdl在部分机器上做视频显示,改变显示窗口大小会崩溃

    今天测试人员反应,之前做的视频绘图显示,会在她机器上,会出现崩溃现象,最后我在她机器上对代码进行跟踪,发现在某种情况,确实会崩溃. 最主要的原因是,视频显示窗口变成非活动窗口的时候,sdl内部会循环消 ...

  5. Remote SSH: Using JSCH with Expect4j

    Now-a-days, we can see that whole world is moving around Clouds and virtualization. More and more ap ...

  6. EayRadius 于 2013-7-19 进行体验度更新,增加用户体验度

    EasyRadius于2013-7-19进行更新,此次更新并没有更新通讯接口,通讯接口将统一更新,包括对其他路由的支持 下面我将主要更新的地方向大家描述一下 如果你有疑问或者建议,可以致电137799 ...

  7. Fedora26 tftp-server设置

    安装tftp-server yum install -y  tftp-server 启动软件 systemctl start tftp.socket systemctl enable tftp.soc ...

  8. 系统目录结构/ls命令/文件类型/alias命令

    2.1/2.2 系统目录结构 2.3 ls命令 2.4 文件类型 2.5 alias命令 linux文件目录结构 linux文件结构 / 系统跟目录 root  root用户主目录,存放启动linux ...

  9. kuser_cmpxchg_check 原子操作

    对于ARM体系结构,每一个由用户态到内核态的中断或异常处理路径都经过kuser_cmpxchg_check,kuser_cmpxchg_check中检查被中断的地址是否大于TASK_SIZE:TASK ...

  10. EF6 Code First & Auto Migration on Appharbor

    之前不小心看到EF的code first在appharbor上进行migration的时候比较麻烦,今天碰巧也要更新数据库了,顺便试试. modify model public class SiteI ...