PHP implementation of the two-fork Tree traversal algorithm sample code

Source: Internet
Author: User
This article mainly introduces PHP implementation of the two-fork tree traversal algorithm, combined with specific examples of PHP for binary tree commonly used in the pre-order, middle sequence and post-order traversal algorithm implementation skills, the need for friends can refer to the next

In this paper, we describe the two-fork tree traversal algorithm implemented by PHP. Share to everyone for your reference, as follows:

Using PHP today to traverse a two-fork tree

Create a two-fork tree as shown

The PHP code looks like this:

<?phpclass Node {public $value;  Public $child _left; Public $child _right;} Final class Ergodic {//Pre-sequence traversal: first accesses the root node, then traverses the left subtree, and finally traverses the right subtree, and while traversing the left and left subtrees, it is necessary to traverse the root node first, then access the subtree, and finally traverse the right subtree public static function preorder ($root)    {$stack = array ();    Array_push ($stack, $root);      while (!empty ($stack)) {$center _node = Array_pop ($stack); Echo $center _node->value.      ' ';      First put the right subtree node into the stack to ensure that the left subtree node first out of the stack if ($center _node->child_right! = null) Array_push ($stack, $center _node->child_right);    if ($center _node->child_left! = null) Array_push ($stack, $center _node->child_left); }}//middle sequence traversal: first traverse the Zuozi, then access the root node, and finally traverse the right subtree;    is still to traverse the left subtree first, then access the root node, and finally traverse the right subtree public static function Midorder ($root) {$stack = array ();    $center _node = $root; while (!empty ($stack) | | $center _node! = null) {while ($center _node! = null) {Array_push ($stack, $center _nod        e);      $center _node = $center _node->child_left;      } $center _node = Array_pop ($stack); Echo $center _NODE-&GT;value.      ' ';    $center _node = $center _node->child_right; }}//post-order traversal: traverse the left subtree first, then traverse the right subtree, and finally access the root node, similarly, when traversing the left and right subtree, the same time, the same must be traversed by the tree, and then traverse the tree, and finally access the root node public static function Endorder ($root) {$pu    Sh_stack = Array ();    $visit _stack = Array ();    Array_push ($push _stack, $root);      while (!empty ($push _stack)) {$center _node = Array_pop ($push _stack);      Array_push ($visit _stack, $center _node); Left dial hand the tree node first into the $pushstack stack, making sure that the stack if ($center _node->child_left! = null) Array_push ($push _stack, $center _no in $visitstack      De->child_left);    if ($center _node->child_right! = null) Array_push ($push _stack, $center _node->child_right);      } while (!empty ($visit _stack)) {$center _node = Array_pop ($visit _stack); Echo $center _node->value.    ' '; }}}//creates a two fork tree $ A = new node (), $b = new node (), $c = new node (), $d = new node (), $e = new node (), $f = new node (), $g = new node ( $h = new Node (), $i = new node (), $a->value = ' a '; $b->value = ' B '; $c->value = ' C '; $d->value = ' d '; $e->value = ' e '; $f->value = ' f '; $g->value = ' g '; $h->value = ' h '; $i->value = ' i '; $a->child_left = $b; $a- >child_right = $c; $b->child_left = $d; $b->child_right = $g; $c->child_left = $e; $c->child_right = $f; $d-& Gt;child_left = $h; $d->child_right = $i;//Pre-sequence traversal ergodic::p reorder ($a); The result is: A B D H I G C E fecho ' <br/> ';//Sequence Traversal ergodic::midorder ($a); The result is: H D I B G A E C fecho ' <br/> ';//post-traversal ergodic::endorder ($a); The result is: H I D G B E F C A

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.