PHP实现绘制二叉树图形显示功能详解【包括二叉搜索树、平衡树及红黑树】

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP实现绘制二叉树图形显示功能详解【包括二叉搜索树、平衡树及红黑树】脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了PHP实现绘制二叉树图形显示功能分享给大家供大家参考,具体如下:

前言:

最近老师布置了一个作业:理解并实现平衡二叉树和红黑树,本来老师是说用C#写的,但是我学的C#基本都还给老师了,怎么办?那就用现在最熟悉的语言PHP来写吧!

一个问题来了,书上在讲解树的时候基本上会给出形象的树形图。但是当我们自己试着实现某种树,在调试、输出的时候确只能以字符的形式顺序地输出。这给调试等方面带来了很大的不便。然后在各种百度之后,我发现利用PHP实现二叉树的图形显示的资几乎是零!好吧,那我就自己个儿实现一个

效果显示

如果我是直接在这一步摆代码的话,估计大家会比较烦闷,那我就直接上结果吧,后面在补代码,先激发激发大家的阅读兴趣:

1、搜索二叉树:

PHP实现绘制二叉树图形显示功能详解【包括二叉搜索树、平衡树及红黑树】

2、平衡二叉树:

PHP实现绘制二叉树图形显示功能详解【包括二叉搜索树、平衡树及红黑树】

3、红黑树:

PHP实现绘制二叉树图形显示功能详解【包括二叉搜索树、平衡树及红黑树】

代码

我们给图片创建一个类吧,显得稍微的小高级:

image.PHP 文件

<PRe class="brush:PHp;"> 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,$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); 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); if (!is_null($parent = $root->parent)) { imageline($this->image,$p_y,$root_x,$black); } //画左子节点 if (!is_null($root->left)) { $this->draw($root->left,$i + 1,$root_y); } //画右子节点 if (!is_null($root->right)) { $this->draw($root->right,$root_y); } } } /** * 画搜索二叉树结点 * @param $x,当前节点的x坐标 $y,当前节点的y坐标 $node,当前节点的引用 * @return null */ private function drawBstNode($x,$y,$node) { //节点的线颜色 $black = imagecolorallocate($this->image,0); $nodeColor = imagecolorallocate($this->image,$this->nodeColor[0],$this->nodeColor[1],$this->nodeColor[2]); //画节点圆 imageellipse($this->image,$x,$this->rad * 2,$black); //节点圆颜色填充 imagefill($this->image,$nodeColor); //节点对应的数字 imagestring($this->image,4,$node->key,$black); } /** * 画平衡二叉树结点 * @param $x,当前节点的x坐标 $y,当前节点的y坐标 $node,当前节点的引用 * @return null */ private function drawAvlNode($x,$node) { $black = imagecolorallocate($this->image,$this->nodeColor[2]); imageellipse($this->image,$black); imagefill($this->image,$nodeColor); imagestring($this->image,$node->key . '(' . $node->bf . ')',$black); } /** * 画红黑树结点 * @param $x,当前节点的x坐标 $y,当前节点的y坐标 $node,当前节点的引用 * @return null */ private function drawRBTNode($x,0); $gray = imagecolorallocate($this->image,180,180); $pink = imagecolorallocate($this->image,255,203); imageellipse($this->image,$black); if ($node->Isred == TRUE) { imagefill($this->image,$pink); } else { imagefill($this->image,$gray); } imagestring($this->image,$black); } }

脚本宝典总结

以上是脚本宝典为你收集整理的PHP实现绘制二叉树图形显示功能详解【包括二叉搜索树、平衡树及红黑树】全部内容,希望文章能够帮你解决PHP实现绘制二叉树图形显示功能详解【包括二叉搜索树、平衡树及红黑树】所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。