require 'bstOrder.php';

$test = range(1, 10);

//$test = array(3,9,1,4,8,5,7,6,2,10);

$tree = new Bst($test, true);

//$tree->deleteNode('30');(非平衡树可删除,平衡树的没写删除操作)

print_r($tree->getTree());

?>

bstOrder.php

/**

* PHP实现二叉排序树

* @author zhaojiangwei

* @since 2011/11/16 16:29

*

*/

class Node{

private $data;

private $left;

private $right;

private $bf;//平衡度

public function Node($data = NULL, $bf = 0, $left = NULL, $right = NULL){

$this->data = $data;

$this->left = $left;

$this->right = $right;

$this->bf = $bf;

}

public function getBf(){

return $this->bf;

}

public function setBf($bf){

$this->bf = $bf;

}

public function getData(){

return $this->data;

}

public function setData($data){

$this->data = $data;

}

public function &getLeft(){

return $this->left;

}

public function &getRight(){

return $this->right;

}

public function setLeft($left){

$this->left = $left;

}

public function setRight($right){

$this->right = $right;

}

}

class Bst{

private $head;//头结点

private $data;//初始输入的数据,为数组形式,如array('a','b');

private $tag;//查找时设置的前一结点(已无效,没用)

//$bst:是否创建AVL树

public function Bst($data, $bst = FALSE){

$this->data = $data;

$this->head = NULL;

if(!$bst){

$this->createBst();

}else{

$this->createBfBst();

}

}

public function createBfBst(){

foreach($this->data as $value){

$this->insertBfNode($this->head, $value);

}

}

private function insertBfNode(&$node, $value){

if($node == NULL){

$node = new Node($value, 0);

return TRUE;

}else{

if($node->getData() > $value){

if(!$this->insertBfNode($node->getLeft(), $value)){

return FALSE;

}

switch($node->getBf()){

case 0:

$node->setBf(1);

return TRUE;

case 1:

$this->rightRotate($node);

return FALSE;

case -1:

$node->setBf(0);

return FALSE;

}

}elseif($node->getData() < $value){

if(!$this->insertBfNode($node->getRight(), $value)){

return FALSE;

}

switch($node->getBf()){

case 0:

$node->setBf(-1);

return TRUE;

case 1:

$node->setBf(0);

return FALSE;

case -1:

$this->leftRotate($node);

return FALSE;

}

}else{

return FAlse;

}

}

}

private function excuteLeft(&$node){

$temp = $node;

$node = $node->getRight();

$temp->setRight($node->getLeft());

$node->setLeft($temp);

}

private function excuteRight(&$node){

$temp = $node;

$node = $node->getLeft();

$temp->setLeft($node->getRight());

$node->setRight($temp);

}

private function leftRotate(&$node){

$right = &$node->getRight();

switch($right->getBf()){

case 1:

$left = &$right->getLeft();

switch($left->getBf()){

case -1:

$right->setBf(0);

$node->setBf(1);

break;

case 0:

$right->setBf(0);

$node->setBf(0);

break;

case 1:

$right->setBf(-1);

$node->setBf(0);

break;

}

$left->setBf(0);

$this->excuteRight($right);

$this->excuteLeft($node);

break;

case -1:

$node->setBf(0);

$right->setBf(0);

$this->excuteLeft($node);

break;

}

}

private function rightRotate(&$node){

$left = &$node->getLeft();

switch($left->getBf()){

case -1:

$right = &$left->getRight();

switch($right->getBf()){

case -1:

$left->setBf(1);

$node->setBf(0);

break;

case 0:

$left->setBf(0);

$node->setBf(0);

break;

case 1:

$left->setBf(0);

$node->setBf(-1);

break;

}

$right->setBf(0);

$this->excuteLeft($left);

$this->excuteRight($node);

break;

case 1:

$node->setBf(0);

$left->setBf(0);

$this->excuteRight($node);

break;

}

}

public function createBst(){

foreach($this->data as $value){

$this->insertNode($value);

}

}

//$pre:如果找不到该结点,是否返回前值

public function &searchBst(& $node, $key, $pre = FALSE){

if($node == NULL){

if($pre){

return $this->tag;

}else{

return FALSE;

}

}

if($key > $node->getData()){

$this->tag = $node;

return $this->searchBst($node->getRight(), $key, $pre);

}elseif($key < $node->getData()){

$this->tag = $node;

return $this->searchBst($node->getLeft(), $key, $pre);

}else{

return $node;

}

}

public function insertNode($key){

if(!$this->head){

$this->head = new Node($key);

}else{

$pre = $this->searchBst($this->head, $key, TRUE);

$node = new Node($key);

if($pre->getData() > $key){

$pre->setLeft($node);

}else{

$pre->setRight($node);

}

}

}

public function deleteNode($key){

$node = &$this->searchBst($this->head, $key);

if(!$node){

return FALSE;

}

if($node->getLeft() == NULL){

$node = $node->getRight();

}elseif($node->getRight() == NULL){

$node = $node->getLeft();

}else{

$leftBig = &$this->searchLeftBig($node);

$node->setData($leftBig->getData());

if($leftBig->getLeft()){

$leftBig = $leftBig->getLeft();

}

}

}

private function &searchLeftBig(& $key){

$result = &$key->getLeft();

while($result){

if($result->getRight() == NULL){

return $result;

}

$result = &$result->getRight();

}

}

public function getTree(){

return $this->head;

}

}

?>

avl树 php,PHP实现平衡二叉树(AVL树)相关推荐

  1. Python数据结构11:树的实现,树的应用,前中后序遍历,二叉查找树BST,平衡二叉树AVL树,哈夫曼树和哈夫曼编码

    1.概念 树一种基本的"非线性"数据结构. 相关术语: 节点Node:组成树的基本部分.每个节点具有名称,或"键值",节点还可以保存额外数据项,数据项根据不同的 ...

  2. [转]C#与数据结构--树论--平衡二叉树(AVL Tree)

    C#与数据结构--树论--平衡二叉树(AVL Tree) http://www.cnblogs.com/abatei/archive/2008/11/17/1335031.html 介绍 我们知道在二 ...

  3. 平衡二叉树,AVL树之图解篇

    学习过了二叉查找树,想必大家有遇到一个问题.例如,将一个数组{1,2,3,4}依次插入树的时候,形成了图1的情况.有建立树与没建立树对于数据的增删查改已经没有了任何帮助,反而增添了维护的成本.而只有建 ...

  4. (王道408考研数据结构)第五章树-第四节2:平衡二叉树(AVL)及其旋转

    文章目录 一:AVL树基本概念 二:AVL树实现原理 (1)构建AVL树 (2)构建演示 (3)旋转方法 A:右单旋转调整(插入到较高左子树左侧) B:左单旋转调整(插入到较高右子树右侧) C:先左后 ...

  5. 平衡二叉树---AVL树的实现

    AVL树是最先发明的自平衡二叉查找算法,是平衡二叉树的一种.在AVL中任何节点的两个儿子子树的高度最大差别为1,所以它又被成为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增加和 ...

  6. 平衡二叉树(AVL树)和红黑树区别

    1.二叉搜索树,平衡二叉树,红黑树的算法效率 操作 二叉查找树 平衡二叉树 红黑树 查找 O(n) O(logn) O(log2 n) 插入 O(n) O(logn) O(log2 n) 删除 O(n ...

  7. 数据结构-平衡二叉树(AVL树)

    目录 1,平衡二叉树的介绍 1.1,二叉排序树存在的问题 1.2,平衡二叉树 1.3,平衡二叉树的创建 1.4,平衡二叉树的查找 2,代码实现 2.1,平衡二叉树的节点类型 2.2,LL旋转(单右旋转 ...

  8. 算法小讲堂之平衡二叉树|AVL树(超详细~)

    文章目录 一.前言 二.定义 三.原理 3.1 查找操作 3.2 最小(大)值结点 3.3 旋转操作 3.3.1 LL旋转 3.3.2 RR旋转 3.3.3 LR旋转 3.3.4 RL旋转 3.4 插 ...

  9. 实现平衡二叉树(AVL树)的旋转

    给你一个数列{1,2,3,4,5,6},要求创建一颗二叉排序树(BST), 并分析问题所在. 左边 BST 存在的问题分析:               1) 左子树全部为空,从形式上看,更像一个单链 ...

最新文章

  1. Spring Security 5.5发布,正式实装OAuth2.0的第五种授权模式
  2. 从强提醒说起——社交场景下的万有“隐力”
  3. hive sql循环_hive存储过程
  4. 循环神经网络基础介绍
  5. Lync 小技巧-42-动态-IP-统一沟通-环境-IP-变更后-操作
  6. 数据结构与算法--解决问题的方法- 二叉树的的镜像
  7. 创建css的时候选择器有哪几类,CSS3-CSS的选择器共有几类?
  8. 第一次尝试修复Hbase2出现Region不一致,HBCK2
  9. 2020 年 Flink 学习资料整合,建议收藏
  10. Python类和对象的入门级讲解(简单粗暴)
  11. Node.js介绍及安装
  12. 深夜不眠,爬起来写博客
  13. lintcode-111-爬楼梯
  14. linux可执行文件bad interpreter解决方法
  15. 【数据结构】哈夫曼树及哈夫曼编码实现(C语言)
  16. ios、iphone越狱获取系统文件权限
  17. 怎么看rx580是不是470刷的_rx580显卡看是不是刷的教程
  18. 《童趣》——《所见》《小儿垂钓》《村居》《浮生六记·童趣》 ——诗文诵读教学设计
  19. ubuntu18.04声音dummy output的问题
  20. thingJS模模搭(campusbuilder/momoda)及3dsmax插件遇到的坑

热门文章

  1. Git:本地生成SSH KEY,并关联到git仓库
  2. java链式存储_Java实现链式存储的二叉树
  3. Car-like robot运动参数校准(图片版)
  4. 问答| 为何会采用倒车入库(侧方位停车)方式?
  5. z-index 绝对定位的盒子居中
  6. Open3d之坐标变换
  7. ubuntu 查询cpu个数
  8. 如何正确上传一张图片?
  9. oracle仅部分记录建立索引的方法
  10. 玩转Linux进程控制命令