PHP implementation Clues two fork tree and Binary tree traversal method detailed clues binary tree pre-sequence clue binary tree First order clue binary

Source: Internet
Author: User
Tags getting started with php
In this paper, we describe the clue of PHP implementation two fork tree and binary tree traversal method. Share to everyone for your reference, as follows:

<?php  require ' bitree.php ';  $str = ' ko#be8#tr### #acy ##### ';  $tree = new Bitree ($STR);  $tree->createthreadtree ();  echo $tree->threadlist (). "\ n"; traverse the lead from the first node two tree  echo $tree->threadlistreserv (); reverse traverse?> from the last node.

bitree.php:

<? /** * PHP Implementation two fork tree * * @author Zhaojiangwei * @since 2011/10/25 10:32 *//Node 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;    }//I do not like to use the Magic method public Function GetData () {return $this->data;    The Public Function SetData ($data) {$this->data = $data;    } public Function GetLeft () {return $this->left;    The Public Function Setleft ($left) {$this->left = $left;    } public Function GetRight () {return $this->right;    The Public Function Setright ($right) {$this->right = $right;    } public Function Getltag () {return $this->ltag;    The Public Function Setltag ($tag) {$this->ltag = $tag;    } public Function Getrtag () {return $this->rtag;    The Public Function Setrtag ($tag) {$this->rtag = $tag; }}//Lead two fork tree classes class Bitree{Private $datas = null;//the string to import; Private $root = NULL; root node private $leafCount = 0;//leaf node number private $headNode = NULL; Clue two fork Tree's head node private $preNode = null;//traversal of a threaded binary tree when saving the previous traversed node public function Bitree ($datas) {Is_array ($datas) | |      Datas = Str_split ($datas);      $this->datas = $datas;      $this->backupdata = $this->datas;    $this->createtree (TRUE); }//Pre-sequence traversal create tree//$root determine if you want to create a root node public function createtree ($root = FALSE) {if (Emptyempty ($this->datas)) r      Eturn 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;      }}//Returns the number of leaf nodes of the binary tree 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 ()); }}//Determine if the node is a leaf node private function checkempty ($node) {if ($node->getleft () = = NULL && $node->ge      Tright () = = NULL) {return true;    } return false;    }//Returns the binary tree depth public function getdepth () {return $this->traversdepth ($this->root);      }//Traversal to find the binary tree depth 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; }//Returns the traversal result, in the form of a string//$order is returned in the form of a traversal, before the 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);      }//Create thread two fork tree 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); }//threaded binary tree 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-&          Gt;prenode->setrtag (1);        $this->prenode->setright ($node);      } $this->prenode = & $node;//Note the reference $this->threadtraverse ($node->getright ());      }}//From the first node traversing the sequence clue two fork tree public Function threadlist () {$arr = array (); for ($node = $this->getfirstthreadnode ($this->root); $node! = $this->headnode; $node = $this->getnextnode ($      node) {$arr [] = $node->getdata ();    } return implode ($arr);      }//reverse-traverse the sequence thread from the tail node two fork tree public Function Threadlistreserv () {$arr = array (); for ($node = $this->headnode->getright (); $node! = $this->headnode; $node = $this->getprenode ($node)) {$ arr[] = $node-&Gt;getdata ();    } return implode ($arr); }//Returns a node's predecessor public Function Getprenode ($node) {if ($node->getltag () = = 1) {return $node->getleft (      );      }else{return $this->getlastthreadnode ($node->getleft ()); }}//Returns the subsequent public function Getnextnode ($node) {if ($node->getrtag () = = 1) of a node {return $node->ge      Tright ();      }else{return $this->getfirstthreadnode ($node->getright ()); }}//Returns the first node of the sequence clue two fork in public function Getfirstthreadnode ($node) {while ($node->getltag () = = 0) {$no      de = $node->getleft ();    } return $node; }//Returns the last node of the sequence clue two fork tree public Function Getlastthreadnode ($node) {while ($node->getrtag () = = 0) {$node =      $node->getright ();    } return $node; }//Pre-order traversal private Function Frontlist ($node, & $nodeList) {if ($node!== NULL) {$nodeList [] = $node-&gt        ; GetData (); $this->frontlist ($node->getleft(), $nodeList);      $this->frontlist ($node->getright (), $nodeList); }}//Sequence traversal private function middlelist ($node, & $nodeList) {if ($node! = NULL) {$this->middleli        St ($node->getleft (), $nodeList);        $nodeList [] = $node->getdata ();      $this->middlelist ($node->getright (), $nodeList); }}//post-traversal 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; }}?>

More readers interested in PHP related content can view the topic: "PHP Data structure and algorithm tutorial", "PHP operation and operator Usage Summary", "PHP Network Programming Tips", "PHP Basic Grammar Introductory Tutorial", "PHP operation Office Document tips summary (including word, excel,access,ppt), "PHP Date and Time usage summary", "PHP primer for Object-oriented programming", "PHP String Usage Summary", "Getting Started with Php+mysql database operations" and "PHP Common Database Operations Skills Summary"

I hope this article is helpful to you in PHP programming.

The above describes the PHP implementation of the Clue two fork tree and Binary tree traversal method, including the Clue two fork tree, binary tree traversal aspects of the content, I hope that the PHP tutorial interested friends have helped.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.