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
非常适用于多数字排序。。避免了批量循环的重复判断···
PHP 二叉树 二叉排序树实现的更多相关文章
- 哈夫曼树;二叉树;二叉排序树(BST)
优先队列:priority_queue<Type, Container, Functional>Type 为数据类型, Container 为保存数据的容器,Functional 为元素比 ...
- Cracking The Coding Interview 4.0_二叉树
#include <iostream> #include <string> using namespace std; class tree { public: tree() { ...
- NOIP 提高组必会!(转)
1.排序算法(快排.选择.冒泡.堆排序.二叉排序树.桶排序)2.DFS/BFS 也就是搜索算法,剪枝务必要学! 学宽搜的时候学一下哈希表!3.树 ①遍历 ②二叉树 ③二叉排序树(查找.生成.删除) ④ ...
- Cracking The Coding Interview 4.6
//原文: // // Design an algorithm and write code to find the first common ancestor of two nodes in a b ...
- Cracking The Coding Interview4.5
//原文: // // Write an algorithm to find the 'next' node (i.e., in-order successor) of a given node in ...
- 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 ...
- Cracking The Coding Interview4.3
//Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal h ...
- Cracking The Coding Interview 4.1
//Implement a function to check if a tree is balanced. For the purposes of this question, a balanced ...
- NOIP需要掌握的内容(大致
1.排序算法(快排.选择.冒泡.堆排序.二叉排序树.桶排序)2.DFS/BFS 剪枝 哈希表3.树 ①遍历 ②二叉树 ③二叉排序树(查找.生成.删除) ④堆(二叉堆.左偏树.堆排序) ...
随机推荐
- C# 符合备忘录
~ 按位求补符:! 非逻辑运算符:% 求余运算符:^ 异或位运算符:& 且位运算符:| 或位运算符:* 既可以用作乘法符号,还可以表示为指针:+ 表示数学运算符相加:= 用来表示赋值操作:\ ...
- Volley的Get、Post方式(JsonObjectRequest、StringRequest)以及Volley获取图片的3种方式
activity_main.xml 里面什么也没有 AndroidManifest.xml(重点是android:name="com.example.volley.MyApplication ...
- [SQL Server] 复制数据库任务
假设你要生产环境下的数据做相应的测试,比如修改及测试存储过程.更改和优化索引等.但是你用户在连接数据库的情况下,你又不能断开数据库的连接.如何取得数据库的副本呢? 一. 利用数据库任务中的复制数据库 ...
- POI写docx文件table中的单元格水平、垂直对齐
核心示例代码 垂直对齐 XWPFTableCell cell = table.getRow(i).getCell(j); cell.setVerticalAlignment(XWPFTableCell ...
- 一篇关于apache commons类库的详解[转]
1.1. 开篇 在Java的世界,有很多(成千上万)开源的框架,有成功的,也有不那么成功的,有声名显赫的,也有默默无闻的.在我看来,成功而默默无闻的那些框架值得我们格外的尊敬和关注,Jakarta C ...
- mysql 找不到或无法加载已注册的 .Net Framework Data Provider和Unable to find the requested .Net Framework Data Provider. It may not be installed解决
需要安装 mysql-connector-net-6.7.4.msi 在C盘安装mysql的位置找到三个DLL,复制到Bin文件夹下 在Web.config文件中添加对应配置: <system. ...
- bat 变量作用域
set answer=one if true equ true ( set answer=two echo %answer% ) echo Argument is %answer% pause
- Android 内存
memory usage of this progress under 15MB for 1GB RAM device Android内存机制分析下篇:分析APP内存使用情况http://mobile ...
- Git项目协同开发学习笔记2:项目库开发协作相关命令
之前介绍了如何用git构建项目库及其后续操作的问题,但主要还是个人的操作问题,不太涉及到项目协作方面的问题,所以来说下这块.传送门在这里(后面的可以不用看了). 1.同步 首先就式同步问题:在项目协作 ...
- nginx配置设置,使部分页面访问跳转到404页面
location ~* /(ask|hospital|wenda|regsearch|user|doctor) { return ; } error_page /.html;