PHP Cashing binary tree, clue two fork Tree

Source: Internet
Author: User
PHP implementation two fork tree, clue two fork 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
 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;        }}//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's head node private $preNode = null;//traverse a threaded binary tree when saving the previous traversed node public function Bitree ($datas) {Is_arra Y ($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 (empt                        Y ($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 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 () = = N            ULL && $node->getright () = = 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) {retur               n 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//er is returned in the form of traversal, before the public function getList (er = ' front ') {i            F ($this->root = = null) return null;            $nodeList = Array ();                    Switch (er) {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 ($nod                    E->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 the reference $this->threadtraverse ($node-&gt            ; 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->getdata ();        } return implode ($arr); }//Returns the predecessor of a node public function Getprenode ($node) {if($node->getltag () = = 1)            {return $node->getleft ();            }else{return $this->getlastthreadnode ($node->getleft ());                }}//Returns the successor Public function Getnextnode ($node) {if ($node->getrtag () = = 1) of a node {            return $node->getright ();            }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) {$node = $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->getdata ();                $this->frontlist ($node->getleft (), $nodeList);            $this->frontlist ($node->getright (), $nodeList);                }}//middle order traversal Private Function Middlelist ($node, & $nodeList) {if ($node! = NULL) {                $this->middlelist ($node->getleft (), $nodeList);                $nodeList [] = $node->getdata ();            $this->middlelist ($node->getright (), $nodeList);                }}//post-traverse 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; }}?>
  • 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.