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.树 ①遍历 ②二叉树 ③二叉排序树(查找.生成.删除) ④堆(二叉堆.左偏树.堆排序) ...
随机推荐
- 【Centos】【Python3】yum install 报错
运行yum install 安装时报错 File "/usr/libexec/urlgrabber-ext-down", line 28 except OSError, e: Sy ...
- e609. Listening to All Focus Changes Between Components in an Application
To listen to focus change events between components, install a listener with the keyboard focus mana ...
- CI框架 -- 开发环境、生产环境
开发者常常希望当系统运行在开发环境或生产环境中时能有不同的行为, 例如,在开发环境如果程序能输出详细的错误信息将非常有用,但是在 生产环境这将造成一些安全问题. ENVIRONMENT 常量 Code ...
- ubuntu -- 安装memcached
Memcached的安装依赖libevent.它是memcached所依赖的异步事件通知库,因此在安装memcached之前先要安装libevent. ubuntu安装软件的方法通常有两种 第一种:使 ...
- iOS: block参数
先看一下 NSArray 是怎么传递 block 参数的 ... @interface NSArray (NSExtendedArray) … #if NS_BLOCKS_AVAILABLE - (v ...
- 关于Json-lib类库的使用
前言介绍: json-lib包是一个Java类库,它可以将Java对象(包括:beans,maps,collections,java arrays ,xml等)和JSON互相转换. 与此相同的是谷歌也 ...
- ClangFormat代码格式化
下载地址:https://github.com/travisjeffery/ClangFormat-Xcode 配合Xcode自带的格式化操作,就很不错了 选中内容组合操作: 第一步:ClangFor ...
- json字符串使用注意问题
json本身是字符串,即 json字符串 js使用 要把 json字符串 转为 javascript对象 json字符串转为js对象的方法:jquery的parseJSON var str='[{& ...
- 在 ubuntu 【6.06、6.10】 上安装 oracle 10.2.0.1,并打补丁 10.2.0.5
特点: ubuntu 6.06.6.10 算是很古老的ubuntu了,其应该是基于 debian 4 的 tesing/unstable 分支.所以,毛病较多. 如何安装oracle10g? 几个技术 ...
- Unity3D学习笔记——Android远程真机调试(Unity Remote)
前言:当使用Unity开发移动端的游戏,特别是使用到手机的传感器,如重力感应等,调试的时候,很麻烦, 因为每次都需要编译成APK后安装到手机中测试,而Unity Remote便能很好的解决这个问题,U ...