当前位置: 首页 > 编程文章 >

PHP实现二叉树图形可视化:BST、AVL与红黑树绘制方案

PHP实现二叉树图形可视化:BST、AVL与红黑树绘制方案

作者:陆景时间:2026-10-01 17:28:02浏览:435

针对PHP调试树结构时缺乏直观图形显示的痛点,本文介绍了一种通过PHP生成图像来可视化二叉树的方法。内容涵盖搜索二叉树、平衡二叉树及红黑树的图形呈现效果,并提供了包含图像生成类Client调用逻辑及具体树节点类(Bst、Avl、Rbt)的完整代码实现,帮助开发者更便捷地理解与调试各类树算法。

本文目录展开

PHP实现二叉树图形可视化方案

本示例详细解析了使用PHP语言实现二叉树图形化展示的具体方法,旨在为相关开发者提供实用的参考代码与思路。

背景说明:

近期课程作业要求深入理解并编码实现平衡二叉树(AVL)和红黑树。虽然原计划采用C#进行开发,但由于对C#掌握程度有限,故转而使用更为熟悉的PHP语言来完成这一任务。

在实际编码调试过程中发现,教材中通常配有直观的树形结构图,但程序运行时的输出往往仅为字符序列,这给调试和结构验证带来了不便。经检索发现,网络上基于PHP实现二叉树图形可视化的资源极为稀缺,因此决定自行开发一套解决方案。

效果预览:

为避免冗长的代码直接冲击阅读体验,此处先展示最终生成的图形结果,以激发读者的兴趣,随后再逐步展开代码细节。

1、二叉搜索树(BST)效果:

2、平衡二叉树(AVL)效果:

3、红黑树(RB Tree)效果:

核心代码实现:

为了提升代码的结构化程度,我们将图像生成功能封装为一个独立的类。

文件 image.php 内容如下:


<?php
/**
* author:LSGOZJ
* description: 绘制二叉树图像
*/
class image
{
    //树相关设置
    //每层之间的间隔高度
    private $level_high = 100;
    //最底层叶子结点之间的宽度
    private $leaf_width = 50;
    //结点圆的半径
    private $rad = 20;
    //根节点离边框顶端距离
    private $leave = 20;
    //树(保存树对象的引用)
    private $tree;
    //树的层数
    private $level;
    //完全二叉树中最底层叶子结点数量(计算图像宽度时用到,论如何实现图片大小自适应)
    private $maxCount;
    //图像相关设置
    //画布宽度
    private $width;
    //画布高度
    private $height;
    //画布背景颜色(RGB)
    private $bg = array(
    220, 220, 220
    );
    //节点颜色(搜索二叉树和平衡二叉树时用)
    private $nodeColor = array(
    255, 192, 203
    );
    //图像句柄
    private $image;
    /**
    * 构造函数,类属性初始化
    * @param $tree 传递一个树的对象
    * @return null
    */
    public function __construct($tree)
    {
        $this->tree = $tree;
        $this->level = $this->getLevel();
        $this->maxCount = $this->GetMaxCount($this->level);
        $this->width = ($this->rad * 2 * $this->maxCount) + $this->maxCount * $this->leaf_width;
        $this->height = $this->level * ($this->rad * 2) + $this->level_high * ($this->level - 1) + $this->leave;
        //1.创建画布
        $this->image = imagecreatetruecolor($this->width, $this->height); //新建一个真彩色图像,默认背景是黑色
        //填充背景色
        $bgcolor = imagecolorallocate($this->image, $this->bg[0], $this->bg[1], $this->bg[2]);
        imagefill($this->image, 0, 0, $bgcolor);
    }
    /**
    * 返回传进来的树对象对应的完全二叉树中最底层叶子结点数量
    * @param $level 树的层数
    * @return 结点数量
    */
    function GetMaxCount($level)
    {
        return pow(2, $level - 1);
    }
    /**
    * 获取树对象的层数
    * @param null
    * @return 树的层数
    */
    function getLevel()
    {
        return $this->tree->Depth();
    }
    /**
    * 显示二叉树图像
    * @param null
    * @return null
    */
    public function show()
    {
        $this->draw($this->tree->root, 1, 0, 0);
        header("Content-type:image/png");
        imagepng($this->image);
        imagedestroy($this->image);
    }
    /**
    * (递归)画出二叉树的树状结构
    * @param $root,根节点(树或子树) $i,该根节点所处的层 $p_x,父节点的x坐标 $p_y,父节点的y坐标
    * @return null
    */
    private function draw($root, $i, $p_x, $p_y)
    {
        if ($i <= $this->level) {
            //当前节点的y坐标
            $root_y = $i * $this->rad + ($i - 1) * $this->level_high;
            //当前节点的x坐标
            if (!is_null($parent = $root->parent)) {
                if ($root == $parent->left) {
                    $root_x = $p_x - $this->width / (pow(2, $i));
                } else {
                    $root_x = $p_x + $this->width / (pow(2, $i));
                }
            } else {
                //根节点
                $root_x = (1 / 2) * $this->width;
                $root_y += $this->leave;
            }
            //画结点(确定所画节点的类型(平衡、红黑、排序)和方法)
            $method = 'draw' . get_class($this->tree) . 'Node';
            $this->$method($root_x, $root_y, $root);
            //将当前节点和父节点连线(黑色线)
            $black = imagecolorallocate($this->image, 0, 0, 0);
            if (!is_null($parent = $root->parent)) {
                imageline($this->image, $p_x, $p_y, $root_x, $root_y, $black);
            }
            //画左子节点
            if (!is_null($root->left)) {
                $this->draw($root->left, $i + 1, $root_x, $root_y);
            }
            //画右子节点
            if (!is_null($root->right)) {
                $this->draw($root->right, $i + 1, $root_x, $root_y);
            }
        }
    }
    /**
    * 画搜索二叉树结点
    * @param $x,当前节点的x坐标 $y,当前节点的y坐标 $node,当前节点的引用
    * @return null
    */
    private function drawBstNode($x, $y, $node)
    {
        //节点圆的线颜色
        $black = imagecolorallocate($this->image, 0, 0, 0);
        $nodeColor = imagecolorallocate($this->image, $this->nodeColor[0], $this->nodeColor[1], $this->nodeColor[2]);
        //画节点圆
        imageellipse($this->image, $x, $y, $this->rad * 2, $this->rad * 2, $black);
        //节点圆颜色填充
        imagefill($this->image, $x, $y, $nodeColor);
        //节点对应的数字
        imagestring($this->image, 4, $x, $y, $node->key, $black);
    }
    /**
    * 画平衡二叉树结点
    * @param $x,当前节点的x坐标 $y,当前节点的y坐标 $node,当前节点的引用
    * @return null
    */
    private function drawAvlNode($x, $y, $node)
    {
        $black = imagecolorallocate($this->image, 0, 0, 0);
        $nodeColor = imagecolorallocate($this->image, $this->nodeColor[0], $this->nodeColor[1], $this->nodeColor[2]);
        imageellipse($this->image, $x, $y, $this->rad * 2, $this->rad * 2, $black);
        imagefill($this->image, $x, $y, $nodeColor);
        imagestring($this->image, 4, $x, $y, $node->key . '(' . $node->bf . ')', $black);
    }
    /**
    * 画红黑树结点
    * @param $x,当前节点的x坐标 $y,当前节点的y坐标 $node,当前节点的引用
    * @return null
    */
    private function drawRbtNode($x, $y, $node)
    {
        $black = imagecolorallocate($this->image, 0, 0, 0);
        $gray = imagecolorallocate($this->image, 180, 180, 180);
        $pink = imagecolorallocate($this->image, 255, 192, 203);
        imageellipse($this->image, $x, $y, $this->rad * 2, $this->rad * 2, $black);
        if ($node->IsRed == TRUE) {
            imagefill($this->image, $x, $y, $pink);
        } else {
            imagefill($this->image, $x, $y, $gray);
        }
        imagestring($this->image, 4, $x, $y, $node->key, $black);
    }
}
</code></pre>
接下来演示如何在客户端脚本中调用上述类以生成图像:
文件 client.php 内容如下:
<pre class="brush:php;"><code class="language-php">
class Client
{
    public static function Main()
    {
        try {
            //实现文件的自动加载
            function autoload($class)
            {
                include strtolower($class) . '.php';
            }
            spl_autoload_register('autoload');
            $arr = array(62, 88, 58, 47, 35, 73, 51, 99, 37, 93);
            //      $tree = new Bst();  //搜索二叉树
            $tree = new Avl();  //平衡二叉树
            //      $tree = new Rbt();  //红黑树
            $tree->init($arr);   //树的初始化
            //      $tree->Delete(62);
            //      $tree->Insert(100);
            //      $tree->MidOrder();  //树的中序遍历(这也是调试的一个手段,看看数字是否从小到大排序)
            $image = new image($tree);
            $image->show();  //显示图像
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }
}
Client::Main();
</code></pre>
在此过程中,主要涉及以下三个树结构的类定义:
二叉搜索树类 bst.php 内容如下:
<pre class="brush:php;"><code class="language-php">
<?php
/**
* author:zhongjin
* description: 二叉查找树
*/
//结点
class Node
{
    public $key;
    public $parent;
    public $left;
    public $right;
    public function __construct($key)
    {
        $this->key = $key;
        $this->parent = NULL;
        $this->left = NULL;
        $this->right = NULL;
    }
}
//二叉搜索树
class Bst
{
    public $root;
    /**
    * 初始化树结构
    * @param $arr 初始化树结构的数组
    * @return null
    */
    public function init($arr)
    {
        $this->root = new Node($arr[0]);
        for ($i = 1; $i < count($arr); $i++) {
            $this->Insert($arr[$i]);
        }
    }
    /**
    * (对内)中序遍历
    * @param $root (树或子树的)根节点
    * @return null
    */
    private function mid_order($root)
    {
        if ($root != NULL) {
            $this->mid_order($root->left);
            echo $root->key . " ";
            $this->mid_order($root->right);
        }
    }
    /**
    * (对外)中序遍历
    * @param null
    * @return null
    */
    public function MidOrder()
    {
        $this->mid_order($this->root);
    }
    /**
    * 查找树中是否存在$key对应的节点
    * @param $key 待搜索数字
    * @return $key对应的节点
    */
    function search($key)
    {
        $current = $this->root;
        while ($current != NULL) {
            if ($current->key == $key) {
                return $current;
            } elseif ($current->key > $key) {
                $current = $current->left;
            } else {
                $current = $current->right;
            }
        }
        return $current;
    }
    /**
    * 查找树中的最小关键字
    * @param $root 根节点
    * @return 最小关键字对应的节点
    */
    function search_min($root)
    {
        $current = $root;
        while ($current->left != NULL) {
            $current = $current->left;
        }
        return $current;
    }
    /**
    * 查找树中的最大关键字
    * @param $root 根节点
    * @return 最大关键字对应的节点
    */
    function search_max($root)
    {
        $current = $root;
        while ($current->right != NULL) {
            $current = $current->right;
        }
        return $current;
    }
    /**
    * 查找某个$key在中序遍历时的直接前驱节点
    * @param $x 待查找前驱节点的节点引用
    * @return 前驱节点引用
    */
    function predecessor($x)
    {
        //左子节点存在,直接返回左子节点的最右子节点
        if ($x->left != NULL) {
            return $this->search_max($x->left);
        }
        //否则查找其父节点,直到当前结点位于父节点的右边
        $p = $x->parent;
        //如果x是p的左孩子,说明p是x的后继,我们需要找的是p是x的前驱
        while ($p != NULL && $x == $p->left) {
            $x = $p;
            $p = $p->parent;
        }
        return $p;
    }
    /**
    * 查找某个$key在中序遍历时的直接后继节点
    * @param $x 待查找后继节点的节点引用
    * @return 后继节点引用
    */
    function successor($x)
    {
        if ($x->right != NULL) {
            return $this->search_min($x->right);
        }
        $p = $x->parent;
        while ($p != NULL && $x == $p->right) {
            $x = $p;
            $p = $p->parent;
        }
        return $p;
    }
    /**
    * 将$key插入树中
    * @param $key 待插入树的数字
    * @return null
    */
    function Insert($key)
    {
        if (!is_null($this->search($key))) {
            throw new Exception('结点' . $key . '已存在,不可插入!');
        }
        $root = $this->root;
        $inode = new Node($key);
        $current = $root;
        $prenode = NULL;
        //为$inode找到合适的插入位置
        while ($current != NULL) {
            $prenode = $current;
            if ($current->key > $inode->key) {
                $current = $current->left;
            } else {
                $current = $current->right;
            }
        }
        $inode->parent = $prenode;
        //如果$prenode == NULL, 则证明树是空树
        if ($prenode == NULL) {
            $this->root = $inode;
        } else {
            if ($inode->key < $prenode->key) {
                $prenode->left = $inode;
            } else {
                $prenode->right = $inode;
            }
        }
        //return $root;
    }
    /**
    * 在树中删除$key对应的节点
    * @param $key 待删除节点的数字
    * @return null
    */
    function Delete($key)
    {
        if (is_null($this->search($key))) {
            throw new Exception('结点' . $key . "不存在,删除失败!");
        }
        $root = $this->root;
        $dnode = $this->search($key);
        if ($dnode->left == NULL || $dnode->right == NULL) { #如果待删除结点无子节点或只有一个子节点,则c = dnode
            $c = $dnode;
        } else { #如果待删除结点有两个子节点,c置为dnode的直接后继,以待最后将待删除结点的值换为其后继的值
            $c = $this->successor($dnode);
        }
        //无论前面情况如何,到最后c只剩下一边子结点
        if ($c->left != NULL) {
            $s = $c->left;
        } else {
            $s = $c->right;
        }
        if ($s != NULL) { #将c的子节点的父母结点置为c的父母结点,此处c只可能有1个子节点,因为如果c有两个子节点,则c不可能是dnode的直接后继
            $s->parent = $c->parent;
        }
        if ($c->parent == NULL) { #如果c的父母为空,说明c=dnode是根节点,删除根节点后直接将根节点置为根节点的子节点,此处dnode是根节点,且拥有两个子节点,则c是dnode的后继结点,c的父母就不会为空,就不会进入这个if
            $this->root = $s;
        } else if ($c == $c->parent->left) { #如果c是其父节点的左右子节点,则将c父母的左右子节点置为c的左右子节点
            $c->parent->left = $s;
        } else {
            $c->parent->right = $s;
        }
        #如果c!=dnode,说明c是dnode的后继结点,交换c和dnode的key值
        if ($c != $dnode) {
            $dnode->key = $c->key;
        }
        #返回根节点
        //    return $root;
    }
    /**
    * (对内)获取树的深度
    * @param $root 根节点
    * @return 树的深度
    */
    private function getdepth($root)
    {
        if ($root == NULL) {
            return 0;
        }
        $dl = $this->getdepth($root->left);
        $dr = $this->getdepth($root->right);
        return ($dl > $dr ? $dl : $dr) + 1;
    }
    /**
    * (对外)获取树的深度
    * @param null
    * @return null
    */
    public function Depth()
    {
        return $this->getdepth($this->root);
    }
}
?>

平衡二叉树类 avl.php 内容如下:


<?php
 /**
 * author:zhongjin
 * description: 平衡二叉树
 */
//结点
class Node
{
  public $key;
  public $parent;
  public $left;
  public $right;
  public $bf; //平衡因子
  public function __construct($key)
  {
    $this->key = $key;
    $this->parent = NULL;
    $this->left = NULL;
    $this->right = NULL;
    $this->bf = 0;
  }
}
//平衡二叉树
class Avl
{
  public $root;
  const LH = +1; //左高
  const EH = 0;  //等高
  const RH = -1; //右高
  /**
   * 初始化树结构
   * @param $arr 初始化树结构的数组
   * @return null
   */
  public function init($arr)
  {
    $this->root = new Node($arr[0]);
    for ($i = 1; $i < count($arr); $i++) {
      $this->Insert($arr[$i]);
    }
  }
  /**
   * (对内)中序遍历
   * @param $root (树或子树的)根节点
   * @return null
   */
  private function mid_order($root)
  {
    if ($root != NULL) {
      $this->mid_order($root->left);
      echo $root->key . "-" . $root->bf . " ";
      $this->mid_order($root->right);
    }
  }
  /**
   * (对外)中序遍历
   * @param null
   * @return null
   */
  public function MidOrder()
  {
    $this->mid_order($this->root);
  }
  /**
   * 将以$root为根节点的最小不平衡二叉树做右旋处理
   * @param $root(树或子树)根节点
   * @return null
   */
  private function R_Rotate($root)
  {
    $L = $root->left;
    if (!is_NULL($root->parent)) {
      $P = $root->parent;
      if ($root == $P->left) {
        $P->left = $L;
      } else {
        $P->right = $L;
      }
      $L->parent = $P;
    } else {
      $L->parent = NULL;
    }
    $root->parent = $L;
    $root->left = $L->right;
    $L->right = $root;
    //这句必须啊!
    if ($L->parent == NULL) {
      $this->root = $L;
    }
  }
  /**
   * 将以$root为根节点的最小不平衡二叉树做左旋处理
   * @param $root(树或子树)根节点
   * @return null
   */
  private function L_Rotate($root)
  {
    $R = $root->right;
    if (!is_NULL($root->parent)) {
      $P = $root->parent;
      if ($root == $P->left) {
        $P->left = $R;
      } else {
        $P->right = $R;
      }
      $R->parent = $P;
    } else {
      $R->parent = NULL;
    }
    $root->parent = $R;
    $root->right = $R->left;
    $R->left = $root;
    //这句必须啊!
    if ($R->parent == NULL) {
      $this->root = $R;
    }
  }
  /**
   * 对以$root所指结点为根节点的二叉树作左平衡处理
   * @param $root(树或子树)根节点
   * @return null
   */
  public function LeftBalance($root)
  {
    $L = $root->left;
    $L_bf = $L->bf;
    switch ($L_bf) {
      //检查root的左子树的平衡度,并作相应的平衡处理
      case self::LH:  //新结点插入在root的左孩子的左子树上,要做单右旋处理
        $root->bf = $L->bf = self::EH;
        $this->R_Rotate($root);
        break;
      case self::RH:  //新节点插入在root的左孩子的右子树上,要做双旋处理
        $L_r = $L->right;  //root左孩子的右子树根
        $L_r_bf = $L_r->bf;
        //修改root及其左孩子的平衡因子
        switch ($L_r_bf) {
          case self::LH:
            $root->bf = self::RH;
            $L->bf = self::EH;
            break;
          case self::EH:
            $root->bf = $L->bf = self::EH;
            break;
          case self::RH:
            $root->bf = self::EH;
            $L->bf = self::LH;
            break;
        }
        $L_r->bf = self::EH;
        //对root的左子树作左平衡处理
        $this->L_Rotate($L);
        //对root作右平衡处理
        $this->R_Rotate($root);
    }
  }
  /**
   * 对以$root所指结点为根节点的二叉树作右平衡处理
   * @param $root(树或子树)根节点
   * @return null
   */
  public function RightBalance($root)
  {
    $R = $root->right;
    $R_bf = $R->bf;
    switch ($R_bf) {
      //检查root的右子树的平衡度,并作相应的平衡处理
      case self::RH:  //新结点插入在root的右孩子的右子树上,要做单左旋处理
        $root->bf = $R->bf = self::EH;
        $this->L_Rotate($root);
        break;
      case self::LH:  //新节点插入在root的右孩子的左子树上,要做双旋处理
        $R_l = $R->left;  //root右孩子的左子树根
        $R_l_bf = $R_l->bf;
        //修改root及其右孩子的平衡因子
        switch ($R_l_bf) {
          case self::RH:
            $root->bf = self::LH;
            $R->bf = self::EH;
            break;
          case self::EH:
            $root->bf = $R->bf = self::EH;
            break;
          case self::LH:
            $root->bf = self::EH;
            $R->bf = self::RH;
            break;
        }
        $R_l->bf = self::EH;
        //对root的右子树作右平衡处理
        $this->R_Rotate($R);
        //对root作左平衡处理
        $this->L_Rotate($root);
    }
  }
  /**
   * 查找树中是否存在$key对应的节点
   * @param $key 待搜索数字
   * @return $key对应的节点
   */
  public function search($key)
  {
    $current = $this->root;
    while ($current != NULL) {
      if ($current->key == $key) {
        return $current;
      } elseif ($current->key > $key) {
        $current = $current->left;
      } else {
        $current = $current->right;
      }
    }
    return $current;
  }
  /**
   * 查找树中的最小关键字
   * @param $root 根节点
   * @return 最小关键字对应的节点
   */
  function search_min($root)
  {
    $current = $root;
    while ($current->left != NULL) {
      $current = $current->left;
    }
    return $current;
  }
  /**
   * 查找树中的最大关键字
   * @param $root 根节点
   * @return 最大关键字对应的节点
   */
  function search_max($root)
  {
    $current = $root;
    while ($current->right != NULL) {
      $current = $current->right;
    }
    return $current;
  }
  /**
   * 查找某个$key在中序遍历时的直接前驱节点
   * @param $x 待查找前驱节点的节点引用
   * @return 前驱节点引用
   */
  private function predecessor($x)
  {
    //左子节点存在,直接返回左子节点的最右子节点
    if ($x->left != NULL) {
      return $this->search_max($x->left);
    }
    //否则查找其父节点,直到当前结点位于父节点的右边
    $p = $x->parent;
    //如果x是p的左孩子,说明p是x的后继,我们需要找的是p是x的前驱
    while ($p != NULL && $x == $p->left) {
      $x = $p;
      $p = $p->parent;
    }
    return $p;
  }
  /**
   * 查找某个$key在中序遍历时的直接后继节点
   * @param $x 待查找后继节点的节点引用
   * @return 后继节点引用
   */
  private function successor($x)
  {
    if ($x->left != NULL) {
      return $this->search_min($x->right);
    }
    $p = $x->parent;
    while ($p != NULL && $x == $p->right) {
      $x = $p;
      $p = $p->parent;
    }
    return $p;
  }
  /**
   * (对内)插入结点,如果结点不存在则插入,失去平衡要做平衡处理
   * @param $root 根节点 $key 待插入树的数字
   * @return null
   */
  private function insert_node(&$root, $key)
  {
    //找到了插入的位置,插入新节点
    if (is_null($root)) {
      $root = new Node($key);
      //插入结点成功
      return TRUE;
    } else {
      //在树中已经存在和$key相等的结点
      if ($key == $root->key) {
        //插入节点失败
        return FALSE;
      } //在root的左子树中继续搜索
      elseif ($key < $root->key) {
        //插入左子树失败
        if (!($this->insert_node($root->left, $key))) {
          //树未长高
          return FALSE;
        }
        //成功插入,修改平衡因子
        if (is_null($root->left->parent)) {
          $root->left->parent = $root;
        }
        switch ($root->bf) {
          //原来左右子树等高,现在左子树增高而树增高
          case self::EH:
            $root->bf = self::LH;
            //树长高
            return TRUE;
            break;
          //原来左子树比右子树高,需要做左平衡处理
          case self::LH:
            $this->LeftBalance($root);
            //平衡后,树并未长高
            return FALSE;
            break;
          //原来右子树比左子树高,现在左右子树等高
          case self::RH:
            $root->bf = self::EH;
            //树并未长高
            return FALSE;
            break;
        }
      } //在root的右子树中继续搜索
      else {
        //插入右子树失败
        if (!$this->insert_node($root->right, $key)) {
          //树未长高
          return FALSE;
        }
        //成功插入,修改平衡因子
        if (is_null($root->right->parent)) {
          $root->right->parent = $root;
        }
        switch ($root->bf) {
          //原来左右子树等高,现在右子树增高而树增高
          case self::EH:
            $root->bf = self::RH;
            //树长高
            return TRUE;
            break;
          //原来左子树比右子树高,现在左右子树等高
          case self::LH:
            $root->bf = self::EH;
            return FALSE;
            break;
          //原来右子树比左子树高,要做右平衡处理
          case self::RH:
            $this->RightBalance($root);
            //树并未长高
            return FALSE;
            break;
        }
      }
    }
  }
  /**
   * (对外)将$key插入树中
   * @param $key 待插入树的数字
   * @return null
   */
  public function Insert($key)
  {
    $this->insert_node($this->root, $key);
  }
  /**
   * 获取待删除的节点(删除的最终节点)
   * @param $key 待删除的数字
   * @return 最终被删除的节点
   */
  private function get_del_node($key)
  {
    $dnode = $this->search($key);
    if ($dnode == NULL) {
      throw new Exception("结点不存在!");
      return;
    }
    if ($dnode->left == NULL || $dnode->right == NULL) { #如果待删除结点无子节点或只有一个子节点,则c = dnode
      $c = $dnode;
    } else { #如果待删除结点有两个子节点,c置为dnode的直接后继,以待最后将待删除结点的值换为其后继的值
      $c = $this->successor($dnode);
    }
    $dnode->key = $c->key;
    return $c;
  }
  /**
   * (对内)删除指定节点,处理该结点往上结点的平衡因子
   * @param $node 最终该被删除的节点
   * @return null
   */
  private function del_node($node)
  {
    if ($node == $this->root) {
      $this->root = NULL;
      return;
    }
    $current = $node;
    //现在的node只有两种情况,要么只有一个子节点,要么没有子节点
    $P = $current->parent;
    //删除一个结点,第一个父节点的平衡都肯定会发生变化
    $lower = TRUE;
    while ($lower == TRUE && !is_null($P)) {
      //待删除结点是左节点
      if ($current == $P->left) {
        if($current == $node){
          if (!is_null($current->left)) {
            $P->left = $current->left;
          } else {
            $P->left = $current->left;
          }
        }
        $P_bf = $P->bf;
        switch ($P_bf) {
          case self::LH:
            $P->bf = self::EH;
            $lower = TRUE;
            $current = $P;
            $P = $current->parent;
            break;
          case self::EH:
            $P->bf = self::RH;
            $lower = FALSE;
            break;
          case self::RH:
            $this->RightBalance($P);
            $lower = TRUE;
            $current = $P->parent;
            $P = $current->parent;
            break;
        }
      } //右结点
      else {
        if($current == $node){
          if (!is_null($current->left)) {
            $P->right = $current->left;
          } else {
            $P->right = $current->left;
          }
        }
        $P_bf = $P->bf;
        switch ($P_bf) {
          case self::LH:
            $this->LeftBalance($P);
            $lower = TRUE;
            $current = $P->parent;
            $P = $current->parent;
            break;
          case self::EH:
            $P->bf = self::LH;
            $lower = FALSE;
            break;
          case self::RH:
            $P->bf = self::LH;
            $lower = TRUE;
            $current = $P;
            $P = $current->parent;
            break;
        }
      }
    }
  }
  /**
   * (对外)删除指定节点
   * @param $key 删除节点的key值
   * @return null
   */
  public function Delete($key)
  {
    $del_node = $this->get_del_node($key);
    $this->del_node($del_node);
  }
  /**
   * (对内)获取树的深度
   * @param $root 根节点
   * @return 树的深度
   */
  private function getdepth($root)
  {
    if ($root == NULL) {
      return 0;
    }
    $dl = $this->getdepth($root->left);
    $dr = $this->getdepth($root->right);
    return ($dl > $dr ? $dl : $dr) + 1;
  }
  /**
   * (对外)获取树的深度
   * @param null
   * @return null
   */
  public function Depth()
  {
    return $this->getdepth($this->root);
  }
}
?>

红黑树类 rbt.php 内容如下:


<?php
 /**
 * author:zhongjin
 * description: 红黑树
 */
//结点
class Node
{
  public $key;
  public $parent;
  public $left;
  public $right;
  public $IsRed; //分辨红节点或黑节点
  public function __construct($key, $IsRed = TRUE)
  {
    $this->key = $key;
    $this->parent = NULL;
    $this->left = NULL;
    $this->right = NULL;
    //插入结点默认是红色
    $this->IsRed = $IsRed;
  }
}
//红黑树
class Rbt
{
  public $root;
  /**
   * 初始化树结构
   * @param $arr 初始化树结构的数组
   * @return null
   */
  public function init($arr)
  {
    //根节点必须是黑色
    $this->root = new Node($arr[0], FALSE);
    for ($i = 1; $i < count($arr); $i++) {
      $this->Insert($arr[$i]);
    }
  }
  /**
   * (对内)中序遍历
   * @param $root (树或子树的)根节点
   * @return null
   */
  private function mid_order($root)
  {
    if ($root != NULL) {
      $this->mid_order($root->left);
      echo $root->key . "-" . ($root->IsRed ? 'r' : 'b') . ' ';
      $this->mid_order($root->right);
    }
  }
  /**
   * (对外)中序遍历
   * @param null
   * @return null
   */
  public function MidOrder()
  {
    $this->mid_order($this->root);
  }
  /**
   * 查找树中是否存在$key对应的节点
   * @param $key 待搜索数字
   * @return $key对应的节点
   */
  function search($key)
  {
    $current = $this->root;
    while ($current != NULL) {
      if ($current->key == $key) {
        return $current;
      } elseif ($current->key > $key) {
        $current = $current->left;
      } else {
        $current = $current->right;
      }
    }
    //结点不存在
    return $current;
  }
  /**
   * 将以$root为根节点的最小不平衡二叉树做右旋处理
   * @param $root(树或子树)根节点
   * @return null
   */
  private function R_Rotate($root)
  {
    $L = $root->left;
    if (!is_null($root->parent)) {
      $P = $root->parent;
      if($root == $P->left){
        $P->left = $L;
      }else{
        $P->right = $L;
      }
      $L->parent = $P;
    } else {
      $L->parent = NULL;
    }
    $root->parent = $L;
    $root->left = $L->right;
    $L->right = $root;
    //这句必须啊!
    if ($L->parent == NULL) {
      $this->root = $L;
    }
  }
  /**
   * 将以$root为根节点的最小不平衡二叉树做左旋处理
   * @param $root(树或子树)根节点
   * @return null
   */
  private function L_Rotate($root)
  {
    $R = $root->right;
    if (!is_null($root->parent)) {
      $P = $root->parent;
      if($root == $P->right){
        $P->right = $R;
      }else{
        $P->left = $R;
      }
      $R->parent = $P;
    } else {
      $R->parent = NULL;
    }
    $root->parent = $R;
    $root->right = $R->left;
    $R->left = $root;
    //这句必须啊!
    if ($R->parent == NULL) {
      $this->root = $R;
    }
  }
  /**
   * 查找树中的最小关键字
   * @param $root 根节点
   * @return 最小关键字对应的节点
   */
  function search_min($root)
  {
    $current = $root;
    while ($current->left != NULL) {
      $current = $current->left;
    }
    return $current;
  }
  /**
   * 查找树中的最大关键字
   * @param $root 根节点
   * @return 最大关键字对应的节点
   */
  function search_max($root)
  {
    $current = $root;
    while ($current->right != NULL) {
      $current = $current->right;
    }
    return $current;
  }
  /**
   * 查找某个$key在中序遍历时的直接前驱节点
   * @param $x 待查找前驱节点的节点引用
   * @return 前驱节点引用
   */
  function predecessor($x)
  {
    //左子节点存在,直接返回左子节点的最右子节点
    if ($x->left != NULL) {
      return $this->search_max($x->left);
    }
    //否则查找其父节点,直到当前结点位于父节点的右边
    $p = $x->parent;
    //如果x是p的左孩子,说明p是x的后继,我们需要找的是p是x的前驱
    while ($p != NULL && $x == $p->left) {
      $x = $p;
      $p = $p->parent;
    }
    return $p;
  }
  /**
   * 查找某个$key在中序遍历时的直接后继节点
   * @param $x 待查找后继节点的节点引用
   * @return 后继节点引用
   */
  function successor($x)
  {
    if ($x->left != NULL) {
      return $this->search_min($x->right);
    }
    $p = $x->parent;
    while ($p != NULL && $x == $p->right) {
      $x = $p;
      $p = $p->parent;
    }
    return $p;
  }
  /**
   * 将$key插入树中
   * @param $key 待插入树的数字
   * @return null
   */
  public function Insert($key)
  {
    if (!is_null($this->search($key))) {
      throw new Exception('结点' . $key . '已存在,不可插入!');
    }
    $root = $this->root;
    $inode = new Node($key);
    $current = $root;
    $prenode = NULL;
    //为$inode找到合适的插入位置
    while ($current != NULL) {
      $prenode = $current;
      if ($current->key > $inode->key) {
        $current = $current->left;
      } else {
        $current = $current->right;
      }
    }
    $inode->parent = $prenode;
    //如果$prenode == NULL, 则证明树是空树
    if ($prenode == NULL) {
      $this->root = $inode;
    } else {
      if ($inode->key < $prenode->key) {
        $prenode->left = $inode;
      } else {
        $prenode->right = $inode;
      }
    }
    //将它重新修正为一颗红黑树
    $this->InsertFixUp($inode);
  }
  /**
   * 对插入节点的位置及往上的位置进行颜色调整
   * @param $inode 插入的节点
   * @return null
   */
  private function InsertFixUp($inode)
  {
    //情况一:需要调整条件,父节点存在且父节点的颜色是红色
    while (($parent = $inode->parent) != NULL && $parent->IsRed == TRUE) {
      //祖父结点:
      $gparent = $parent->parent;
      //如果父节点是祖父结点的左子结点,下面的else与此相反
      if ($parent == $gparent->left) {
        //叔叔结点
        $uncle = $gparent->right;
        //case1:叔叔结点也是红色
        if ($uncle != NULL && $uncle->IsRed == TRUE) {
          //将父节点和叔叔结点都涂黑,将祖父结点涂红
          $parent->IsRed = FALSE;
          $uncle->IsRed = FALSE;
          $gparent->IsRed = TRUE;
          //将新节点指向祖父节点(现在祖父结点变红,可以看作新节点存在)
          $inode = $gparent;
          //继续while循环,重新判断
          continue;  //经过这一步之后,组父节点作为新节点存在(跳到case2)
        }
        //case2:叔叔结点是黑色,且当前结点是右子节点
        if ($inode == $parent->right) {
          //以父节点作为旋转结点做左旋转处理
          $this->L_Rotate($parent);
          //在树中实际上已经转换,但是这里的变量的指向还没交换,
          //将父节点和字节调换一下,为下面右旋做准备
          $temp = $parent;
          $parent = $inode;
          $inode = $temp;
        }
        //case3:叔叔结点是黑色,而且当前结点是父节点的左子节点
        $parent->IsRed = FALSE;
        $gparent->IsRed = TRUE;
        $this->R_Rotate($gparent);
      } //如果父节点是祖父结点的右子结点,与上面完全相反
      else {
        //叔叔结点
        $uncle = $gparent->left;
        //case1:叔叔结点也是红色
        if ($uncle != NULL && $uncle->IsRed == TRUE) {
          //将父节点和叔叔结点都涂黑,将祖父结点涂红
          $parent->IsRed = FALSE;
          $uncle->IsRed = FALSE;
          $gparent->IsRed = TRUE;
          //将新节点指向祖父节点(现在祖父结点变红,可以看作新节点存在)
          $inode = $gparent;
          //继续while循环,重新判断
          continue;  //经过这一步之后,组父节点作为新节点存在(跳到case2)
        }
        //case2:叔叔结点是黑色,且当前结点是左子节点
        if ($inode == $parent->left) {
          //以父节点作为旋转结点做右旋转处理
          $this->R_Rotate($parent);
          //在树中实际上已经转换,但是这里的变量的指向还没交换,
          //将父节点和字节调换一下,为下面右旋做准备
          $temp = $parent;
          $parent = $inode;
          $inode = $temp;
        }
        //case3:叔叔结点是黑色,而且当前结点是父节点的右子节点
        $parent->IsRed = FALSE;
        $gparent->IsRed = TRUE;
        $this->L_Rotate($gparent);
      }
    }
    //情况二:原树是根节点(父节点为空),则只需将根节点涂黑
    if ($inode == $this->root) {
      $this->root->IsRed = FALSE;
      return;
    }
    //情况三:插入节点的父节点是黑色,则什么也不用做
    if ($inode->parent != NULL && $inode->parent->IsRed == FALSE) {
      return;
    }
  }
  /**
   * (对外)删除指定节点
   * @param $key 删除节点的key值
   * @return null
   */
  function Delete($key)
  {
    if (is_null($this->search($key))) {
      throw new Exception('结点' . $key . "不存在,删除失败!");
    }
    $dnode = $this->search($key);
    if ($dnode->left == NULL || $dnode->right == NULL) { #如果待删除结点无子节点或只有一个子节点,则c = dnode
      $c = $dnode;
    } else { #如果待删除结点有两个子节点,c置为dnode的直接后继,以待最后将待删除结点的值换为其后继的值
      $c = $this->successor($dnode);
    }
    //为了后面颜色处理做准备
    $parent = $c->parent;
    //无论前面情况如何,到最后c只剩下一边子结点
    if ($c->left != NULL) {  //这里不会出现,除非选择的是删除结点的前驱
      $s = $c->left;
    } else {
      $s = $c->right;
    }
    if ($s != NULL) { #将c的子节点的父母结点置为c的父母结点,此处c只可能有1个子节点,因为如果c有两个子节点,则c不可能是dnode的直接后继
      $s->parent = $c->parent;
    }
    if ($c->parent == NULL) { #如果c的父母为空,说明c=dnode是根节点,删除根节点后直接将根节点置为根节点的子节点,此处dnode是根节点,且拥有两个子节点,则c是dnode的后继结点,c的父母就不会为空,就不会进入这个if
      $this->root = $s;
    } else if ($c == $c->parent->left) { #如果c是其父节点的左右子节点,则将c父母的左右子节点置为c的左右子节点
      $c->parent->left = $s;
    } else {
      $c->parent->right = $s;
    }
    $dnode->key = $c->key;
    $node = $s;
    //c的结点颜色是黑色,那么会影响路径上的黑色结点的数量,必须进行调整
    if ($c->IsRed == FALSE) {
      $this->DeleteFixUp($node,$parent);
    }
  }
  /**
   * 删除节点后对接点周围的其他节点进行调整
   * @param $key 删除节点的子节点和父节点
   * @return null
   */
  private function DeleteFixUp($node,$parent)
  {
    //如果待删结点的子节点为红色,直接将子节点涂黑
    if ($node != NULL && $node->IsRed == TRUE) {
      $node->IsRed = FALSE;
      return;
    }
    //如果是根节点,那就直接将根节点置为黑色即可
    while (($node == NULL || $node->IsRed == FALSE) && ($node != $this->root)) {
      //node是父节点的左子节点,下面else与这里相反
      if ($node == $parent->left) {
        $brother = $parent->right;
        //case1:兄弟结点颜色是红色(父节点和兄弟孩子结点都是黑色)
        //将父节点涂红,将兄弟结点涂黑,然后对父节点进行左旋处理(经过这一步,情况转换为兄弟结点颜色为黑色的情况)
        if ($brother->IsRed == TRUE) {
          $brother->IsRed = FALSE;
          $parent->IsRed = TRUE;
          $this->L_Rotate($parent);
          //将情况转化为其他的情况
          $brother = $parent->right; //在左旋处理后,$parent->right指向的是原来兄弟结点的左子节点
        }
        //以下是兄弟结点为黑色的情况
        //case2:兄弟结点是黑色,且兄弟结点的两个子节点都是黑色
        //将兄弟结点涂红,将当前结点指向其父节点,将其父节点指向当前结点的祖父结点。
        if (($brother->left == NULL || $brother->left->IsRed == FALSE) && ($brother->right == NULL || $brother->right->IsRed == FALSE)) {
          $brother->IsRed = TRUE;
          $node = $parent;
          $parent = $node->parent;
        } else {
          //case3:兄弟结点是黑色,兄弟结点的左子节点是红色,右子节点为黑色
          //将兄弟结点涂红,将兄弟节点的左子节点涂黑,然后对兄弟结点做右旋处理(经过这一步,情况转换为兄弟结点颜色为黑色,右子节点为红色的情况)
          if ($brother->right == NULL || $brother->right->IsRed == FALSE) {
            $brother->IsRed = TRUE;
            $brother->left->IsRed = FALSE;
            $this->R_Rotate($brother);
            //将情况转换为其他情况
            $brother = $parent->right;
          }
          //case4:兄弟结点是黑色,且兄弟结点的右子节点为红色,左子节点为任意颜色
          //将兄弟节点涂成父节点的颜色,再把父节点涂黑,将兄弟结点的右子节点涂黑,然后对父节点做左旋处理
          $brother->IsRed = $parent->IsRed;
          $parent->IsRed = FALSE;
          $brother->right->IsRed = FALSE;
          $this->L_Rotate($parent);
          //到了第四种情况,已经是最基本的情况了,可以直接退出了
          $node = $this->root;
          break;
        }
      } //node是父节点的右子节点
      else {
        $brother = $parent->left;
        //case1:兄弟结点颜色是红色(父节点和兄弟孩子结点都是黑色)
        //将父节点涂红,将兄弟结点涂黑,然后对父节点进行右旋处理(经过这一步,情况转换为兄弟结点颜色为黑色的情况)
        if ($brother->IsRed == TRUE) {
          $brother->IsRed = FALSE;
          $parent->IsRed = TRUE;
          $this->R_Rotate($parent);
          //将情况转化为其他的情况
          $brother = $parent->left; //在右旋处理后,$parent->left指向的是原来兄弟结点的右子节点
        }
        //以下是兄弟结点为黑色的情况
        //case2:兄弟结点是黑色,且兄弟结点的两个子节点都是黑色
        //将兄弟结点涂红,将当前结点指向其父节点,将其父节点指向当前结点的祖父结点。
        if (($brother->left == NULL || $brother->left->IsRed == FALSE) && ($brother->right == NULL || $brother->right->IsRed == FALSE)) {
          $brother->IsRed = TRUE;
          $node = $parent;
          $parent = $node->parent;
        } else {
          //case3:兄弟结点是黑色,兄弟结点的右子节点是红色,左子节点为黑色
          //将兄弟结点涂红,将兄弟节点的左子节点涂黑,然后对兄弟结点做左旋处理(经过这一步,情况转换为兄弟结点颜色为黑色,右子节点为红色的情况)
          if ($brother->left == NULL || $brother->left->IsRed == FALSE) {
            $brother->IsRed = TRUE;
            $brother->right = FALSE;
            $this->L_Rotate($brother);
            //将情况转换为其他情况
            $brother = $parent->left;
          }
          //case4:兄弟结点是黑色,且兄弟结点的左子节点为红色,右子节点为任意颜色
          //将兄弟节点涂成父节点的颜色,再把父节点涂黑,将兄弟结点的右子节点涂黑,然后对父节点左左旋处理
          $brother->IsRed = $parent->IsRed;
          $parent->IsRed = FALSE;
          $brother->left->IsRed = FALSE;
          $this->R_Rotate($parent);
          $node = $this->root;
          break;
        }
      }
    }
    if ($node != NULL) {
      $this->root->IsRed = FALSE;
    }
  }
  /**
   * (对内)获取树的深度
   * @param $root 根节点
   * @return 树的深度
   */
  private function getdepth($root)
  {
    if ($root == NULL) {
      return 0;
    }
    $dl = $this->getdepth($root->left);
    $dr = $this->getdepth($root->right);
    return ($dl > $dr ? $dl : $dr) + 1;
  }
  /**
   * (对外)获取树的深度
   * @param null
   * @return null
   */
  public function Depth()
  {
    return $this->getdepth($this->root);
  }
}
?>

希望上述关于PHP绘制二叉树图形的分享,能对从事PHP程序设计及数据结构学习的读者有所帮助。

上一篇
PHP类继承与接口继承深度对比解析

PHP中类继承仅支持单继承但允许多层次结构,而接口支持多继承且方法默认公开。本文详细对比了两者在权限修饰、构造函数、数据成员及实现关键字上的差异,并阐述了抽象类作为中间形态的特性。通过实例分析私有、保护及公有方法的继承行为,帮助开发者根据封装需求选择类继承或接口实现。

下一篇
PHP数组操作与排序函数全解析

本文详细梳理了PHP中常见的数组操作方法,涵盖键值获取、内部指针控制、分段填充、栈与队列模拟等基础功能。重点介绍了多种数组排序机制,包括按值排序(sort, rsort, asort)、按键名排序(ksort, krsort)、自然排序(natsort)以及基于用户自定义函数的灵活排序(usort, uasort)。此外,还讲解了array_multisort在多维数组排序中的应用,并提供了相关代码示例以辅助理解。

相关文章

更多
PHP判断二叉树是否对称的递归实现

本文介绍如何使用PHP语言通过递归方法判断二叉树是否对称。核心思路是比较二叉树与其镜像是否相同,即左右子节点的值及结构需完全一致。文章提供了具体的代码实现逻辑,帮助开发者掌握这一常见数据结构问题的解法。

2026.07.14 876
PHP实现排序二叉树:插入、遍历与查找功能详解

本文通过PHP代码示例,演示了排序二叉树的核心功能实现。内容涵盖节点的插入逻辑、中序遍历以获取有序序列,以及查找最大值、最小值和特定值的算法。文章简要介绍了二叉树及相关术语定义,适合希望了解基础数据结构在PHP中应用的开发者参考。

2026.10.12 305
PHP实现的线索二叉树及遍历方法详解

本文介绍使用PHP语言构建线索二叉树的具体实现方案,涵盖核心类定义与节点链接逻辑。内容详细解析了前序、中序和后序等常见遍历方式在带线索结构下的处理差异,旨在为开发者提供清晰的数据结构应用参考。

2026.10.10 255
PHP实现二叉树遍历算法示例

本文介绍使用PHP语言实现二叉树遍历的具体方法。通过构建特定的二叉树结构,展示了相应的核心代码逻辑,旨在为PHP开发者提供关于数据结构编程的参考示例。

2025.01.14 131
PHP时间戳与日期字符串相互转换方法详解

本文介绍PHP中时间数值(Unix时间戳)与日期字符串之间的相互转换方法。通过strtotime和date函数,实现将当前时间、指定日期转换为时间戳,以及将时间戳格式化为可读的日期时间字符串,并提供相关在线计算工具推荐。

2025.11.30 1,031
PHP操作MySQL数据库:从工具选择到实战案例

本文介绍了使用PHP操作MySQL数据库的完整流程,涵盖命令行、Navicat及phpMyAdmin等常用管理工具。重点讲解HTTP GET与POST方式在表单数据传递中的区别与安全特性。通过mysqli扩展的基础步骤(创建连接、选择库、设置编码、执行SQL、关闭连接),演示了增删改查的具体实现,并对比了fetch_assoc、fetch_row和fetch_array三种结果集获取方法的差异。最后结合注册与登录两个实际案例,展示如何将前端用户输入安全地存入数据库并进行验证。

2026.01.07 1,004

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。