首页 后端开发 php教程 PHP兑现二叉树,线索二叉树

PHP兑现二叉树,线索二叉树

Jun 13, 2016 pm 01:11 PM
function gt node return this

PHP实现二叉树,线索二叉树

<?php
    require 'biTree.php';

    $str = 'ko#be8#tr####acy#####';
   
    $tree = new BiTree($str);
    $tree->createThreadTree();

    echo $tree->threadList() . "\n";从第一个结点开始遍历线索二叉树
    echo $tree->threadListReserv();从最后一个结点开始反向遍历
?>
登录后复制


biTree.php
<?
    /**
     * PHP实现二叉树
     *
     * @author zhaojiangwei
     * @since 2011/10/25 10:32
     */

    //结点类
    class Node{
        private $data = NULL;
        private $left = NULL;
        private $right = NULL;
        private $lTag = 0;
        private $rTag = 0;

        public function Node($data = false){
            $this->data = $data;
        }
       
        //我不喜欢使用魔术方法 
        public function getData(){
            return $this->data;
        }

        public function setData($data){
            $this->data = $data;
        }

        public function getLeft(){
            return $this->left;
        }

        public function setLeft($left){
            $this->left = $left;
        }

        public function getRight(){
            return $this->right;
        }

        public function setRight($right){
            $this->right = $right;
        }

        public function getLTag(){
            return $this->lTag;
        }

        public function setLTag($tag){
            $this->lTag = $tag;
        }

        public function getRTag(){
            return $this->rTag;
        }

        public function setRTag($tag){
            $this->rTag = $tag;
        }
    }

    //线索二叉树类
    class BiTree{
        private $datas = NULL;//要导入的字符串;
        private $root = NULL; //根结点
        private $leafCount = 0;//叶子结点个数
        private $headNode = NULL; //线索二叉树的头结点
        private $preNode = NULL;//遍历线索化二叉树时保存前一个遍历的结点

        public function BiTree($datas){
            is_array($datas) || $datas = str_split($datas);
            $this->datas = $datas; 
            $this->backupData = $this->datas; 
            $this->createTree(TRUE);          
        }

        
        //前序遍历创建树
        //$root 判断是不是要创建根结点
        public function createTree($root = FALSE){
            if(empty($this->datas)) return NULL;
            
            $first = array_shift($this->datas);
            if($first == '#'){
                return NULL;
            }else{
                $node = new Node($first);
                $root && $this->root = $node;                

                $node->setLeft($this->createTree());
                $node->setRight($this->createTree());

                return $node;
            }
        }
    
        //返回二叉树叶子结点的个数
        public function getLeafCount(){
            $this->figureLeafCount($this->root);
            return $this->leafCount; 
        }
    
        private function figureLeafCount($node){
            if($node == NULL)
                return false;

            if($this->checkEmpty($node)){
                $this->leafCount ++;
            }else{
                $this->figureLeafCount($node->getLeft());
                $this->figureLeafCount($node->getRight());
            }
        }
         
        //判断结点是不是叶子结点
        private function checkEmpty($node){
            if($node->getLeft() == NULL && $node->getRight() == NULL){
                return true;
            }

            return false;
        }

        //返回二叉树深度
        public function getDepth(){
            return $this->traversDepth($this->root);
        }
        
        //遍历求二叉树深度
        public function traversDepth($node){
            if($node == NULL){
                return 0;   
            }

            $u = $this->traversDepth($node->getLeft()) + 1;
            $v = $this->traversDepth($node->getRight()) + 1;

            return $u > $v ? $u : $v;
        }
     
        //返回遍历结果,以字符串的形式
        //$order 按遍历形式返回,前中后 
        public function getList($order = 'front'){
            if($this->root == NULL) return NULL;

            $nodeList = array();

            switch ($order){
                case "front":
                    $this->frontList($this->root, $nodeList);
                    break;
                    
                case "middle":
                    $this->middleList($this->root, $nodeList);
                    break;
                
                case "last":
                    $this->lastList($this->root, $nodeList);
                    break;
                     
                default:
                    $this->frontList($this->root, $nodeList);
                    break; 
            }
            
            return implode($nodeList);
        }

        //创建线索二叉树
        public function createThreadTree(){
            $this->headNode = new Node();
            $this->preNode = & $this->headNode;
            $this->headNode->setLTag(0);
            $this->headNode->setLeft($this->root);
            $this->headNode->setRTag(1);
            
            $this->threadTraverse($this->root);

            $this->preNode->setRight($this->headNode);
            $this->preNode->setRTag(1);
            $this->headNode->setRight($this->preNode);
        }
     
        //线索化二叉树
        private function threadTraverse($node){
            if($node != NULL){
                if($node->getLeft() == NULL){
                    $node->setLTag(1);
                    $node->setLeft($this->preNode);
                }else{
                    $this->threadTraverse($node->getLeft());
                }
                
                if($this->preNode != $this->headNode && $this->preNode->getRight() == NULL){
                    $this->preNode->setRTag(1);
                    $this->preNode->setRight($node);
                }
                
                $this->preNode = & $node;//注意传引用
                $this->threadTraverse($node->getRight());
            }
        }

        //从第一个结点遍历中序线索二叉树
        public function threadList(){
            $arr = array();

            for($node = $this->getFirstThreadNode($this->root); $node != $this->headNode; $node = $this->getNextNode($node)){
                $arr[] = $node->getData();
            }

            return implode($arr);
        }

        //从尾结点反向遍历中序线索二叉树
        public function threadListReserv(){
            $arr = array();
            
            for($node = $this->headNode->getRight(); $node != $this->headNode; $node = $this->getPreNode($node)){
                $arr[] = $node->getData(); 
            }

            return implode($arr);
        }

        //返回某个结点的前驱
        public function getPreNode($node){
            if($node->getLTag() == 1){
                return $node->getLeft();
            }else{
                return $this->getLastThreadNode($node->getLeft());
            }
        }

        //返回某个结点的后继
        public function getNextNode($node){
            if($node->getRTag() == 1){
                return $node->getRight();
            }else{
                return $this->getFirstThreadNode($node->getRight());
            }
        }

        //返回中序线索二叉树的第一个结点
        public function getFirstThreadNode($node){
            while($node->getLTag() == 0){
                $node = $node->getLeft();
            }

            return $node;
        }
       
        //返回中序线索二叉树的最后一个结点
        public function getLastThreadNode($node){
            while($node->getRTag() == 0){
                $node = $node->getRight(); 
            }

            return $node;
        }
       

        //前序遍历
        private function frontList($node, & $nodeList){
            if($node !== NULL){
                $nodeList[] = $node->getData();
                $this->frontList($node->getLeft(), $nodeList);
                $this->frontList($node->getRight(), $nodeList);
            }
        }

        //中序遍历
        private function middleList($node, & $nodeList){
            if($node != NULL){
                $this->middleList($node->getLeft(), $nodeList);
                $nodeList[] = $node->getData();
                $this->middleList($node->getRight(), $nodeList);
            }
        }
        
        //后序遍历
        private function lastList($node, & $nodeList){
            if($node != NULL){
                $this->lastList($node->getLeft(), $nodeList);
                $this->lastList($node->getRight(), $nodeList);
                $nodeList[] = $node->getData();
            }
        }

        public function getData(){
            return $this->data;
        }

        public function getRoot(){
            return $this->root;
        }

    }

?>
登录后复制

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

华为GT3 Pro和GT4的差异是什么? 华为GT3 Pro和GT4的差异是什么? Dec 29, 2023 pm 02:27 PM

许多用户在选择智能手表的时候都会选择的华为的品牌,其中华为GT3pro和GT4都是非常热门的选择,不少用户都很好奇华为GT3pro和GT4有什么区别,下面就就给大家介绍一下二者。华为GT3pro和GT4有什么区别一、外观GT4:46mm和41mm,材质是玻璃表镜+不锈钢机身+高分纤维后壳。GT3pro:46.6mm和42.9mm,材质是蓝宝石玻璃表镜+钛金属机身/陶瓷机身+陶瓷后壳二、健康GT4:采用最新的华为Truseen5.5+算法,结果会更加的精准。GT3pro:多了ECG心电图和血管及安

C语言return的用法详解 C语言return的用法详解 Oct 07, 2023 am 10:58 AM

C语言return的用法有:1、对于返回值类型为void的函数,可以使用return语句来提前结束函数的执行;2、对于返回值类型不为void的函数,return语句的作用是将函数的执行结果返回给调用者;3、提前结束函数的执行,在函数内部,我们可以使用return语句来提前结束函数的执行,即使函数并没有返回值。

nvm 怎么删除node nvm 怎么删除node Dec 29, 2022 am 10:07 AM

nvm删除node的方法:1、下载“nvm-setup.zip”并将其安装在C盘;2、配置环境变量,并通过“nvm -v”命令查看版本号;3、使用“nvm install”命令安装node;4、通过“nvm uninstall”命令删除已安装的node即可。

node项目中如何使用express来处理文件的上传 node项目中如何使用express来处理文件的上传 Mar 28, 2023 pm 07:28 PM

怎么处理文件上传?下面本篇文章给大家介绍一下node项目中如何使用express来处理文件的上传,希望对大家有所帮助!

function是什么意思 function是什么意思 Aug 04, 2023 am 10:33 AM

function是函数的意思,是一段具有特定功能的可重复使用的代码块,是程序的基本组成单元之一,可以接受输入参数,执行特定的操作,并返回结果,其目的是封装一段可重复使用的代码,提高代码的可重用性和可维护性。

修复:截图工具在 Windows 11 中不起作用 修复:截图工具在 Windows 11 中不起作用 Aug 24, 2023 am 09:48 AM

为什么截图工具在Windows11上不起作用了解问题的根本原因有助于找到正确的解决方案。以下是截图工具可能无法正常工作的主要原因:对焦助手已打开:这可以防止截图工具打开。应用程序损坏:如果截图工具在启动时崩溃,则可能已损坏。过时的图形驱动程序:不兼容的驱动程序可能会干扰截图工具。来自其他应用程序的干扰:其他正在运行的应用程序可能与截图工具冲突。证书已过期:升级过程中的错误可能会导致此issu简单的解决方案这些适合大多数用户,不需要任何特殊的技术知识。1.更新窗口和Microsoft应用商店应用程

深入浅析Node的进程管理工具“pm2” 深入浅析Node的进程管理工具“pm2” Apr 03, 2023 pm 06:02 PM

本篇文章给大家分享Node的进程管理工具“pm2”,聊聊为什么需要pm2、安装和使用pm2的方法,希望对大家有所帮助!

Java中return和finally语句的执行顺序是怎样的? Java中return和finally语句的执行顺序是怎样的? Apr 25, 2023 pm 07:55 PM

源码:publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#输出上述代码的输出可以简单地得出结论:return在finally之前执行,我们来看下字节码层面上发生了什么事情。下面截取case1方法的部分字节码,并且对照源码,将每个指令的含义注释在

See all articles