<?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. SSH-简单登陆业务详解,从环境部署到代码编写

    一直都是半解半知的情况下动手去实现某些功能,但是竟然也可以成功, 而我发现你花2小时听视频看所谓的理论,不如花2小时直接做个功能出来, 更加深了对此框架的理解,中间遇到问题后,可以上网再查详细的知识点 ...

  2. spatial transformer networks 这篇论文

    大致看了看这个paper, 很novel. 我的观点: 在traditional convolutional neural netwoks 中,我们通常会depend 于 extracting fea ...

  3. VIM下的可视模式的相关知识

    三种可视模式: v 激活面向字符的可视模式: V 激活面向行的可视模式: ctrl+v 激活面向列块的可视模式: 选择高亮区: 上面的 v 是可以与跳转指令 以及表示范围的指令组合使用的. 如:vl, ...

  4. ZBar与ZXing使用后感觉

    [原]ZBar与ZXing使用后感觉(上) 2014-3-18阅读2011 评论1 最近对二维码比较感兴趣,还是那句老话,那么我就对比了一下zxing和zbar 如果对于这两个的背景不了解的话,可以看 ...

  5. TPshop标签

    很多cms 中有很多 标签, 商品标签 文章标签  列表标签  几十个标签, 让开发者头疼, 难记,  TPshop开发者考虑到这点, 用了一个万能标签, 开发者非常方便实用 TPshop万能标签只要 ...

  6. (转)DSound开发常用的几个结构

    WAVEFORMATEX WAVEFORMATEX { WORD wFormatTag; WORD nChannels; DWORD nSamplesPerSec; DWORD nAvgBytesPe ...

  7. spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的区别联系

    InitializingBean Spirng的InitializingBean为bean提供了定义初始化方法的方式.InitializingBean是一个接口,它仅仅包含一个方法:afterProp ...

  8. UIStatusBarStyle PreferredStatusBarStyle does not work on iOS 7

    Q: In my iPhone application built with Xcode 5 for iOS 7 I set UIViewControllerBasedStatusBarAppeara ...

  9. ESB架构之企业实施案例

    ESB架构之企业实施案例 ESB解决令企业最头痛的信息系统整合问题 SOA 架构中的ESB是更好的应用于异构系统集成整合还是用于统一服务调用/基础服务实施

  10. centos7 Minimal安装没有ifconfig

    centos7 Minimal  安装后 ip addr 系统的网卡没有分配IP地址 网卡为ens33 cd /etc/sysconfig/network-scripts vi ifcfg-ens33 ...