html5中文学习网

您的位置: 首页 > 网络编程 > PHP编程 » 正文

PHP实现二叉树/线索二叉树_PHP教程_编程技术

[ ] 已经帮助:人解决问题

PHP实现二叉树、线索二叉树,如下代码:fPLHTML5中文学习网 - HTML5先行者学习网

  1. <?php 
  2.     require 'biTree.php'
  3.  
  4.     $str = 'ko#be8#tr####acy#####'
  5.     
  6.     $tree = new BiTree($str); 
  7.     $tree->createThreadTree(); 
  8.  
  9.     echo $tree->threadList() . "/n";从第一个结点开始遍历线索二叉树 
  10.     echo $tree->threadListReserv();从最后一个结点开始反向遍历 
  11. ?> 
  12. //biTree.php 
  13. <? 
  14.     //结点类 
  15.     class Node{ 
  16.         private $data = NULL; 
  17.         private $left = NULL; 
  18.         private $right = NULL; 
  19.         private $lTag = 0; 
  20.         private $rTag = 0; 
  21.  
  22.         public function Node($data = false){ 
  23.             $this->data = $data
  24.         } 
  25.         
  26.         //我不喜欢使用魔术方法  
  27.         public function getData(){ 
  28.             return $this->data; 
  29.         } 
  30.  
  31.         public function setData($data){ 
  32.             $this->data = $data
  33.         } 
  34.  
  35.         public function getLeft(){ 
  36.             return $this->left; 
  37.         } 
  38.  
  39.         public function setLeft($left){ 
  40.             $this->left = $left
  41.         } 
  42.  
  43.         public function getRight(){ 
  44.             return $this->right; 
  45.         } 
  46.  
  47.         public function setRight($right){ 
  48.             $this->right = $right
  49.         } 
  50.  
  51.         public function getLTag(){ 
  52.             return $this->lTag; 
  53.         } 
  54.  
  55.         public function setLTag($tag){ 
  56.             $this->lTag = $tag
  57.         } 
  58.  
  59.         public function getRTag(){ 
  60.             return $this->rTag; 
  61.         } 
  62.  
  63.         public function setRTag($tag){ 
  64.             $this->rTag = $tag
  65.         } 
  66.     } 
  67.  
  68.     //线索二叉树类 
  69.     class BiTree{ 
  70.         private $datas = NULL;//要导入的字符串; 
  71.         private $root = NULL; //根结点 
  72.         private $leafCount = 0;//叶子结点个数 
  73.         private $headNode = NULL; //线索二叉树的头结点 
  74.         private $preNode = NULL;//遍历线索化二叉树时保存前一个遍历的结点 
  75.  
  76.         public function BiTree($datas){ 
  77.             is_array($datas)  $datas = str_split($datas); 
  78.             $this->datas = $datas;  
  79.             $this->backupData = $this->datas;  
  80.             $this->createTree(TRUE);           
  81.         } 
  82.  
  83.          
  84.         //前序遍历创建树 
  85.         //$root 判断是不是要创建根结点 
  86.         public function createTree($root = FALSE){ 
  87.             if(emptyempty($this->datas)) return NULL; 
  88.              
  89.             $first = array_shift($this->datas); 
  90.             if($first == '#'){ 
  91.                 return NULL; 
  92.             }else
  93.                 $node = new Node($first); 
  94.                 $root && $this->root = $node;                 
  95.  
  96.                 $node->setLeft($this->createTree()); 
  97.                 $node->setRight($this->createTree()); 
  98.  
  99.                 return $node
  100.             } 
  101.         } 
  102.      
  103.         //返回二叉树叶子结点的个数 
  104.         public function getLeafCount(){ 
  105.             $this->figureLeafCount($this->root); 
  106.             return $this->leafCount;  
  107.         } 
  108.      
  109.         private function figureLeafCount($node){ 
  110.             if($node == NULL) 
  111.                 return false; 
  112.  
  113.             if($this->checkEmpty($node)){ 
  114.                 $this->leafCount ++; 
  115.             }else
  116.                 $this->figureLeafCount($node->getLeft()); 
  117.                 $this->figureLeafCount($node->getRight()); 
  118.             } 
  119.         } 
  120.           
  121.         //判断结点是不是叶子结点 
  122.         private function checkEmpty($node){ 
  123.             if($node->getLeft() == NULL && $node->getRight() == NULL){ 
  124.                 return true; 
  125.             } 
  126.  
  127.             return false; 
  128.         } 
  129.  
  130.         //返回二叉树深度 
  131.         public function getDepth(){ 
  132.             return $this->traversDepth($this->root); 
  133.         } 
  134.          
  135.         //遍历求二叉树深度 
  136.         public function traversDepth($node){ 
  137.             if($node == NULL){ 
  138.                 return 0;    
  139.             } 
  140.  
  141.             $u = $this->traversDepth($node->getLeft()) + 1; 
  142.             $v = $this->traversDepth($node->getRight()) + 1; 
  143.  
  144.             return $u > $v ? $u : $v
  145.         } 
  146.       
  147.         //返回遍历结果,以字符串的形式 
  148.         //$order 按遍历形式返回,前中后  
  149.         public function getList($order = 'front'){ 
  150.             if($this->root == NULL) return NULL; 
  151.  
  152.             $nodeList = array(); 
  153.  
  154.             switch ($order){ 
  155.                 case "front"
  156.                     $this->frontList($this->root, $nodeList); 
  157.                     break
  158.                      
  159.                 case "middle"
  160.                     $this->middleList($this->root, $nodeList); 
  161.                     break
  162.                  
  163.                 case "last"
  164.                     $this->lastList($this->root, $nodeList); 
  165.                     break
  166.                       
  167.                 default
  168.                     $this->frontList($this->root, $nodeList); 
  169.                     break;  
  170.             } 
  171.              
  172.             return implode($nodeList); 
  173.         } 
  174.  
  175.         //创建线索二叉树 
  176.         public function createThreadTree(){ 
  177.             $this->headNode = new Node(); 
  178.             $this->preNode = & $this->headNode; 
  179.             $this->headNode->setLTag(0); 
  180.             $this->headNode->setLeft($this->root); 
  181.             $this->headNode->setRTag(1); 
  182.              
  183.             $this->threadTraverse($this->root); 
  184.  
  185.             $this->preNode->setRight($this->headNode); 
  186.             $this->preNode->setRTag(1); 
  187.             $this->headNode->setRight($this->preNode); 
  188.         } 
  189.       
  190.         //线索化二叉树 
  191.         private function threadTraverse($node){ 
  192.             if($node != NULL){ 
  193.                 if($node->getLeft() == NULL){ 
  194.                     $node->setLTag(1); 
  195.                     $node->setLeft($this->preNode); 
  196.                 }else
  197.                     $this->threadTraverse($node->getLeft()); 
  198.                 } 
  199.                  
  200.                 if($this->preNode != $this->headNode && $this->preNode->getRight() == NULL){ 
  201.                     $this->preNode->setRTag(1); 
  202.                     $this->preNode->setRight($node); 
  203.                 } 
  204.                  
  205.                 $this->preNode = & $node;//注意传引用 
  206.                 $this->threadTraverse($node->getRight()); 
  207.             } 
  208.         } 
  209.  
  210.         //从第一个结点遍历中序线索二叉树 
  211.         public function threadList(){ 
  212.             $arr = array(); 
  213.  
  214.             for($node = $this->getFirstThreadNode($this->root); $node != $this->headNode; $node = $this->getNextNode($node)){ 
  215.                 $arr[] = $node->getData(); 
  216.             } 
  217.  
  218.             return implode($arr); 
  219.         } 
  220.  
  221.         //从尾结点反向遍历中序线索二叉树 
  222.         public function threadListReserv(){ 
  223.             $arr = array(); 
  224.              
  225.             for($node = $this->headNode->getRight(); $node != $this->headNode; $node = $this->getPreNode($node)){ 
  226.                 $arr[] = $node->getData();  
  227.             } 
  228.  
  229.             return implode($arr); 
  230.         } 
  231.  
  232.         //返回某个结点的前驱 
  233.         public function getPreNode($node){ 
  234.             if($node->getLTag() == 1){ 
  235.                 return $node->getLeft(); 
  236.             }else
  237.                 return $this->getLastThreadNode($node->getLeft()); 
  238.             } 
  239.         } 
  240.  
  241.         //返回某个结点的后继 
  242.         public function getNextNode($node){ 
  243.             if($node->getRTag() == 1){ 
  244.                 return $node->getRight(); 
  245.             }else
  246.                 return $this->getFirstThreadNode($node->getRight()); 
  247.             } 
  248.         } 
  249.  
  250.         //返回中序线索二叉树的第一个结点 
  251.         public function getFirstThreadNode($node){ 
  252.             while($node->getLTag() == 0){ 
  253.                 $node = $node->getLeft(); 
  254.             } 
  255.  
  256.             return $node
  257.         } 
  258.         
  259.         //返回中序线索二叉树的最后一个结点 
  260.         public function getLastThreadNode($node){ 
  261.             while($node->getRTag() == 0){ 
  262.                 $node = $node->getRight();  
  263.             } 
  264.  
  265.             return $node
  266.         } 
  267.         
  268.  
  269.         //前序遍历 
  270.         private function frontList($node, & $nodeList){ 
  271.             if($node !== NULL){ 
  272.                 $nodeList[] = $node->getData(); 
  273.                 $this->frontList($node->getLeft(), $nodeList); 
  274.                 $this->frontList($node->getRight(), $nodeList); 
  275.             } 
  276.         } 
  277.  
  278.         //中序遍历 
  279.         private function middleList($node, & $nodeList){ 
  280.             if($node != NULL){ 
  281.                 $this->middleList($node->getLeft(), $nodeList); 
  282.                 $nodeList[] = $node->getData(); 
  283.                 $this->middleList($node->getRight(), $nodeList); 
  284.             } 
  285.         } 
  286.          
  287.         //后序遍历 
  288.         private function lastList($node, & $nodeList){ 
  289.             if($node != NULL){ 
  290.                 $this->lastList($node->getLeft(), $nodeList); 
  291.                 $this->lastList($node->getRight(), $nodeList); 
  292.                 $nodeList[] = $node->getData(); 
  293.             } 
  294.         } 
  295.  
  296.         public function getData(){ 
  297.             return $this->data; 
  298.         } 
  299.  
  300.         public function getRoot(){ 
  301.             return $this->root; 
  302.         } 
  303.     } 
  304. ?> 
fPLHTML5中文学习网 - HTML5先行者学习网
fPLHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助