侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

php  /  管理员 发布于 7年前   132

本文实例讲述了PHP实现的线索二叉树及二叉树遍历方法。分享给大家供大家参考,具体如下:

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

biTree.php:

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(emptyempty($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;    }  }?>

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数据结构与算法教程》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

  • PHP Class&Object -- PHP 自排序二叉树的深入解析
  • PHP实现二叉树的深度优先与广度优先遍历方法
  • php实现的二叉树遍历算法示例
  • PHP构造二叉树算法示例
  • PHP实现绘制二叉树图形显示功能详解【包括二叉搜索树、平衡树及红黑树】
  • PHP实现从上往下打印二叉树的方法
  • PHP基于非递归算法实现先序、中序及后序遍历二叉树操作示例
  • PHP获取二叉树镜像的方法
  • PHP实现判断二叉树是否对称的方法
  • PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)实例详解
  • PHP排序二叉树基本功能实现方法示例


  • 上一条:
    使用ltrace工具跟踪PHP库函数调用的方法
    下一条:
    PHP简单实现生成txt文件到指定目录的方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(95个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客