👨‍💼
Hamm Blog
个人主页GithubGitee
  • 🇨🇳Hi Welcome!
  • 📚分享文章
    • CentOS7
      • CentOS7安装Python3的方法
      • CentOS7搭建L2TP服务端一键脚本
      • CentOS7使用CertBot工具获取LetSencrypt泛域名HTTPS证书
    • Wechat
      • 微信公众号网页跳转避免出现导航栏的小技巧
      • 微信公众号php环境无法获取POST值
    • Git
      • GIT进行代码量统计和贡献统计
      • GIT删除仓库的历史提交记录脚本示例
    • HTTP
      • HTTP关于HEADER的详细解释
      • HTTP关于Code状态码的详细解释
      • HTTP协议对URI长度POST数据长度及COOKIE长度限制说明
    • JavaScript
      • 使用ElementUI配合API数据快速渲染合并表格
      • JavaScript调用语音合成Speech Synthesis API
      • JavaScript解析并遍历Excel示例代码
      • JavaScript下的RSA对称加密DEMO
    • MySQL
      • MySQL腾讯云数据库导出备份到本地服务器导入
      • MySQL设计与使用规范
      • MySQL在5.7及以上版本中的ONLY_FULL_GROUP_BY问题处理方案
      • MySQL字符串截取
    • PHP
      • PHP大文件视频上传WebUploader
      • PHP的坑XML转JSON
      • PHP多进程同时处理任务示例DEMO代码
      • PHP高精度计算常用代码
      • PHP获取上周、本周、上月、本月、本季度、上季度时间方法
      • PHP获取真实IP之IP代理 IP伪造 真实IP
      • PHP获取URL HTTP_HOST和SERVER_NAME
      • PHP简单实现QQ登录代码
      • PHP解密微信小程序手机号简单DEMO
      • PHP实现二叉查找树的简单代码实现
      • PHP实现微博登录简单代码
      • PHP实现Web方式发起一个超时任务的处理请求
      • PHP使用独占文件指针实现阻塞少量并发
      • PHP使用root权限执行系统命令和切换到www用户
      • PHP微信JSSDK开发DEMO
      • PHP下的RSA对称加解密 根证书自签与签发子证书
      • PHP下phpMyAdmin数据字典美化代码
      • PHP下RSA对称加密超长字符串分段加密DEMO
    • Nginx
      • Nginx反向代理中的Host参数传递遇到的坑
      • 利用Nginx实现Java后端在开发中的高可用
      • Nginx反向代理NodeJS实现WSS协议
      • Nginx负载均衡的lnmp环境初始化脚本
      • Nginx配置HTTPS与HTTP2.0
      • Nginx图片递归代理服务器DEMO
    • NodeJS
      • NodeJS控制台简单表格打印
      • NodeJS实现超级方便的Git自动发布Webhook
      • NodeJS实现串口通讯简单例程
      • NodeJS实现微信协议登录
      • NodeJS实现UDP数据报套接字通讯
      • NodeJS实现Websocket聊天室
      • NodeJS使用express模块无法收到phpCurl的值
      • NodeJS使用jimp和qrcode-reader识别解析二维码
      • NodeJS使用Request模块实现CURL
      • NodeJS一些冷门但实用的npm包
      • NodeJS转码amr到mp3
      • NWJS配置文件
    • Python
      • Python3实现Webhook
      • Python3使用jieba分词并生成WordCloud词云图
      • Python3使用Pool进程池实现多进程并发
      • Python3使用request进行CURL操作
      • Python3使用Selenium进行自动化测试手册
    • Linux
      • Linux 信号说明列表说明
      • Linux利用CronTab定时执行ThinkPHP命令行模式
  • 😍分享生活
    • 我有个锤子的生活
由 GitBook 提供支持
在本页
  1. 分享文章
  2. PHP

PHP实现二叉查找树的简单代码实现

<?php
class Tree{
	public $top=null;
	public $leftChild=null;
	public $rightChild=null;
	public function Tree($top){
		$this -> top = $top;
	}
	/**
	 * 添加一个子节点
	 **/
	public function addChild($child){
		if ($this -> top > $child){
		    //如果添加的节点比当前节点小 放到左边
			if (empty($this->leftChild )){
			    //如果左边节点为空 直接给左边节点
				$this->leftChild =new Tree($child);
			}else {
			    //递归 左边节点添加子节点
				$this->leftChild ->addChild ($child);
			}
		}else if ($this->top <$child){
		    //如果添加的节点比当前节点大 放到右边
			if (empty($this->rightChild )){
			    //如果右边节点为空 直接给右边节点
				$this->rightChild =new Tree($child);
			}else {
			    //递归 右边节点添加子节点
				$this->rightChild ->addChild ($child);
			}
			return true;
		}else {
			return false;
		}
	}
	/**
	 * 获取一个子节点
	 **/
	public function getChild ($child){
		if ($this->top >$child){
			return $this->leftChild ->getChild ($child);
		}else if ($this->top <$child){
			return $this->rightChild ->getChild ($child);
		}else {
			return $this;
		}
	}
	/**
	 * 获取指定节点下的最大节点
	 **/
	public function  getMaxChild ($node){
		if (empty($node->rightChild )){
			return $node;
		}else {
			return $this->getMaxChild ($node->rightChild );
		}
	}
	/**
	 * 获取指定节点下的最小节点
	 **/
	public function  getMinChild ($node){
		if (empty($node->leftChild )){
			return $node;
		}else {
			return $this->getMinChild ($node->leftChild );
		}
	}
	/**
	 * 删除指定的节点
	 **/
	public function  removeChild ($child){
		if ($this->top >$child){
			if (empty($this->leftChild )){
				return false;
			}else {
				if ($this->leftChild ->top ==$child){
					if (isset($this->leftChild ->leftChild ) && isset($this->leftChild ->rightChild )){
						$maxNode=$this->getMaxChild ($this->leftChild ->leftChild );
						$this->removeChild ($maxNode->top );
						$this->leftChild ->top =$maxNode->top ;
					}else if (empty($this->leftChild ->leftChild )){
						$this->leftChild =$this->getChild ($child)->rightChild ;
						return true;
					}else if (empty($this->leftChild ->rightChild )){
						$this->leftChild =$this->getChild ($child)->leftChild ;
						return true;
					}else {
						$this->leftChild =null;
						return true;
					}
				}else {
					return $this->leftChild ->removeChild ($child);
				}
			}
		}else if ($this->top <$child){
			if (empty($this->rightChild )){
				return false;
			}else {
				if ($this->rightChild ->top ==$child){
					if (isset($this->rightChild ->leftChild ) && isset($this->rightChild ->rightChild )){
						$maxNode=$this->getMaxChild ($this->rightChild ->leftChild );
						$this->removeChild ($maxNode->top );
						$this->rightChild ->top =$maxNode->top ;
					}else if (empty($this->rightChild ->leftChild )){
						$this->rightChild =$this->getChild ($child)->rightChild ;
						return true;
					}else if (empty($this->rightChild ->rightChild )){
						$this->rightChild =$this->getChild ($child)->leftChild ;
						return true;
					}else {
						$this->rightChild =null;
						return true;
					}
				}else {
					return $this->rightChild ->removeChild ($child);
				}
			}
		}else {
		    //根节点就是要删除的
			if (isset($this ->leftChild ) && isset($this ->rightChild )){
				$maxNode=$this->getMaxChild ($this -> leftChild );
				$this-> removeChild ($maxNode -> top);
				$this -> top = $maxNode->top ;
			}else if (empty($this ->leftChild )){
				$this->leftChild =$this->getChild ($this)->rightChild ;
				return true;
			}else if (empty($this ->rightChild )){
				$this->leftChild =$this->getChild ($this)->leftChild ;
				return true;
			}else {
				$this->leftChild =null;
				return true;
			}
	    }
	}
}
$tree=new Tree(15);
$tree->addChild (9);
$tree->addChild (23);
$tree->addChild (3);
$tree->addChild (12);
$tree->addChild (17);
$tree->addChild (28);
$tree->addChild (8);
$tree->addChild (1);
$tree->addChild (4);
$tree->addChild (11);
$tree->removeChild (4);
$tree->removeChild (9);
$tree->removeChild (15);
print_r($tree);

Tree Object
(
    [top] => 12
    [leftChild] => Tree Object
        (
            [top] => 8
            [leftChild] => Tree Object
                (
                    [top] => 3
                    [leftChild] => Tree Object
                        (
                            [top] => 1
                            [leftChild] => 
                            [rightChild] => 
                        )

                    [rightChild] => 
                )

            [rightChild] => Tree Object
                (
                    [top] => 11
                    [leftChild] => 
                    [rightChild] => 
                )

        )

    [rightChild] => Tree Object
        (
            [top] => 23
            [leftChild] => Tree Object
                (
                    [top] => 17
                    [leftChild] => 
                    [rightChild] => 
                )

            [rightChild] => Tree Object
                (
                    [top] => 28
                    [leftChild] => 
                    [rightChild] => 
                )

        )

)

上一页PHP解密微信小程序手机号简单DEMO下一页PHP实现微博登录简单代码
📚