<?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. Unity 基础-------------------------关于Anchor锚点的理解

    Unity进阶技巧 - RectTransform详解 Zui 关注 2016.02.17 01:27 字数 1704 阅读 22157评论 13喜欢 57赞赏 2 RectTransform属性一览 ...

  2. bioperl 格式化genebank的输出

    代码如下: use Bio::SeqIO; use Bio::DB::GenBank; use Bio::DB::Query::GenBank; my $db_obj = Bio::DB::GenBa ...

  3. 每天一个linux命令:traceroute命令

    通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一 ...

  4. Maven最佳实践-distributionManagement

    分发构件至远程仓库 mvn install 会将项目生成的构件安装到本地Maven仓库,mvn deploy 用来将项目生成的构件分发到远程Maven仓库.本地Maven仓库的构件只能供当前用户使用, ...

  5. 关于jq ajax封装以及ajax上传Webapi

    jq的ajax完整版本 $(function () {     fileChange(); }); function fileChange() {     $('#fileinput').change ...

  6. dropwizard metrics - 基本使用介绍

    之前在healthcheck中介绍了怎样通过metrics lib往系统中增加一些简单的健康侦測.如今讲讲dropwizard metrics更重要的部分.记录系统的度量信息. dropwizard提 ...

  7. [Learn AF3]第二章 App Framework 3.0的组件View——AF3的驱动引擎

    View:af3中的驱动引擎   组件名称:View     使用说明:按照官方的说法,view组件是af3的“驱动引擎(driving force)”,view是af3应用的基础,一个app中可以包 ...

  8. EA修改生成代码的表头注释

    我们在做项目的过程中,每个代码文件都应有此文件的注释,比如说作者,文件说明等.但是如果用EA生成的代码文件的注释是纯英文的,而且有些不是我们需要显示的注释,有些我们需要显示的它又不具备.那么我们就可以 ...

  9. windows xp\2003 之上的操作系统多启动(多系统)引导

    概要技术: 微软自windows vista以来的操作系统引导bootmgr是真的很强大,只是因为其全底层的命令操作,且不友好的命令帮助让人望而却步! 基本技术概要提点: boot.ini 支持:xp ...

  10. Ubuntu14.04 安装git

    通过ubuntu的APT安装 sudo apt-get update sudo apt-get install git 配置自己的Git账号信息 git config --global user.na ...