Home > Backend Development > PHP Tutorial > 刚刚学习php,那位高手可以给个利用php实现链表和二叉树的代码解决方案

刚刚学习php,那位高手可以给个利用php实现链表和二叉树的代码解决方案

WBOY
Release: 2016-06-13 10:27:19
Original
805 people have browsed it

刚刚学习php,那位高手可以给个利用php实现链表和二叉树的代码
请高手给个链表和二叉树相关可执行代码
链表的数据结构:
class Lnode
{
  private $data;
  private $next; 
}

二叉树的数据结构:
class Tnode
{
  private $data;
  private $lchild;
  private $rchild;
}

谢谢!

------解决方案--------------------

PHP array 可以模拟很多种结构。
------解决方案--------------------

PHP code
<?phpclass Lnode{  public $data;  public $next;  }class Lists {  private $root;  function Lists() {    $this->root = new Lnode;  }  function append($v) {    $sp =& $this->root;    while(! empty($sp)) $sp =& $sp->next;    $sp = new Lnode;    $sp->data = $v;  }   function delete($v) {    $sp =& $this->root;    while(! empty($sp) && $sp->next->data != $v) $sp =& $sp->next;    if($sp) $sp->next = $sp->next->next;  }    }$p = new Lists;$p->append('A');$p->append('B');$p->append('C');print_r($p);$p->delete('B');print_r($p);<div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template