This article describes the PHP implementation of the clue two-fork tree and Binary tree traversal method. Share to everyone for your reference, specific as follows:
<?php
require ' bitree.php ';
$str = ' ko#be8#tr### #acy ##### ';
$tree = new Bitree ($STR);
$tree->createthreadtree ();
echo $tree->threadlist (). "\ n"; two-fork Tree
Echo $tree->threadlistreserv () starting at the first node, and traversing?> from the last node.
bitree.php:
? /** * PHP Implementation two fork tree * * @author Zhaojiangwei * @since 2011/10/25 10:32///node class node{private $dat
A = NULL;
Private $left = NULL;
Private $right = NULL;
Private $lTag = 0;
Private $rTag = 0;
Public Function Node ($data = False) {$this->data = $data;
//I don't like using magic method Public Function GetData () {return $this->data;
The Public Function SetData ($data) {$this->data = $data;
The Public Function GetLeft () {return $this->left;
The Public Function Setleft ($left) {$this->left = $left;
The Public Function GetRight () {return $this->right;
The Public Function Setright ($right) {$this->right = $right;
The Public Function Getltag () {return $this->ltag;
The Public Function Setltag ($tag) {$this->ltag = $tag;
The Public Function Getrtag () {return $this->rtag; The Public Function Setrtag ($tag) {$thIs->rtag = $tag;
}//Clue two fork tree class bitree{Private $datas = null;//string to import; Private $root = NULL; root node Private $leafCount = number of 0;//leaf nodes private $headNode = NULL; Clue two fork tree head node Private $preNode = null;//traversal of a threaded binary tree saves the previous Traversal 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
) 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; }//Returns the number of tree leaf nodes public Function getleafcount () {$this->figurEleafcount ($this->root);
return $this->leafcount;
The Private Function Figureleafcount ($node) {if ($node = = NULL) return false;
if ($this->checkempty ($node)) {$this->leafcount + +;
}else{$this->figureleafcount ($node->getleft ());
$this->figureleafcount ($node->getright ()); }//Judgment node is not a leaf node private function checkempty ($node) {if ($node->getleft () = = NULL && $node-&G
T;getright () = = NULL) {return true;
return false;
//Returns the binary tree depth public function getdepth () {return $this->traversdepth ($this->root);
}//Traversal 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 returned in the form of traversal, the front and back 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 a Clue 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
->prenode->setrtag (1);
$this->prenode->setright ($node);
$this->prenode = & $node//note refers to $this->threadtraverse ($node->getright ());
}//From the first node traverse the middle order 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 middle order thread from tail node two-fork tree public Function threadlistrEserv () {$arr = array ();
for ($node = $this->headnode->getright (); $node!= $this->headnode; $node = $this->getprenode ($node)) {
$arr [] = $node->getdata ();
Return implode ($arr); //Returns the precursor public function Getprenode ($node) {if ($node->getltag () = 1) {return $node->getl of a node.
EFT ();
}else{return $this->getlastthreadnode ($node->getleft ()); }//Returns a node's successor public function Getnextnode ($node) {if ($node->getrtag () = 1) {return $node-&
Gt;getright ();
}else{return $this->getfirstthreadnode ($node->getright ());
}//Returns the first node of the two-fork tree in the order clue Public function Getfirstthreadnode ($node) {while ($node->getltag () = = 0) {
$node = $node->getleft ();
return $node; //Returns the last node of the middle order clue two fork tree public Function Getlastthreadnode ($node) {while ($node->getrtag () = = 0) {$nod
E = $node->getright (); return $node; }//forward traversal private function frontlist ($node, & $nodeList) {if ($node!== NULL) {$nodeList [] = $node
->getdata ();
$this->frontlist ($node->getleft (), $nodeList);
$this->frontlist ($node->getright (), $nodeList); The In//sequence traverses private function middlelist ($node, & $nodeList) {if ($node!= NULL) {$this->mid
Dlelist ($node->getleft (), $nodeList);
$nodeList [] = $node->getdata ();
$this->middlelist ($node->getright (), $nodeList); }//Sequence traversal private function lastlist ($node, & $nodeList) {if ($node!= NULL) {$this->lastl
IST ($node->getleft (), $nodeList);
$this->lastlist ($node->getright (), $nodeList);
$nodeList [] = $node->getdata ();
The Public Function GetData () {return $this->data;
The Public Function Getroot () {return $this->root;
}}?>
For more information about PHP interested readers can view the site topics: "PHP Data structure and algorithm tutorial", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP operation Office Document skills Summary (including Word, Excel,access,ppt), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Tutorial", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"
I hope this article will help you with the PHP program design.