PHP-based non-recursive algorithm for first order, middle order and sequential traversal of binary tree operation example

Source: Internet
Author: User
This article mainly introduces PHP based on non-recursive algorithm to achieve the first order, middle order and sequential traversal binary tree operation, combined with an example of PHP using a non-recursive algorithm for the two-fork tree First order, middle sequence and sequential traversal operation principle and specific implementation skills, the need for friends can refer to the next

This paper introduces the implementation of the first Order, middle order and sequential traversal binary tree operation based on non-recursive algorithm in PHP. Share to everyone for your reference, as follows:

Overview:

The binary tree traversal principle is as follows:

For the binary tree traversal shown:

1. Pre-sequence traversal : The root node is traversed first, then the left subtree is traversed, and the right subtree is traversed at last.

Abdhecfg

2. Middle sequence traversal : The left subtree is traversed first, then the root node is traversed, and the right subtree is traversed at last.

Hdbeafcg

3. post-traversal: traverse the left subtree first, then traverse the right subtree, and finally traverse the root node.

Hdebfgca

Implementation method:

first-order traversal: the use of advanced features of the stack, the first access to the root node, then the right subtree pressed into, and then pressed into the left subtree. This takes out the left dial hand tree first, and then the right subtree.

function Preorder ($root) {$stack = array (); Array_push ($stack, $root); while (!empty ($stack)) {  $center _node = Array_ Pop ($stack);  Echo $center _node->value; The root node  if ($center _node->right! = null)   Array_push ($stack, $center _node->right);//press into right subtree  if ($ Center_node->left = null)   Array_push ($stack, $center _node->left);//press into left subtree}}

Middle order: need to traverse from the bottom up, so the left subtree is pressed into the stack, and then access the root node and the right subtree one by one.

function Inorder ($root) {$stack = array (); $center _node = $root; while (!empty ($stack) | | $center _node! = null) {while  ( $center _node = null) {   Array_push ($stack, $center _node);   $center _node = $center _node->left;  }  $center _node = Array_pop ($stack);  echo $center _node->value;  $center _node = $center _node->right; }}

Post-order : first save the root node, and then store the Saozi right subtree in turn. and then output.

function Tailorder ($root) {$stack = array (); $outstack = Array (); Array_push ($ $stack, $root); while ($empty ($stack)) {  $center _node = Array_pop ($stack);  Array_push ($outstack, $center _node);  if ($center _node->right! = null)   Array_push ($stack, $center _node->right);  if ($center _node->left! = null)   Array_push ($stack, $center _node->left);} while ($empty ($outstack)) {  $ Center_node = Array_pop ($outstack);  Echo $center _node->value; }}

Articles you may be interested in:

Explanation of how PHP uses two stacks to implement the queue function

A detailed explanation of the principles of PHP serialization and deserialization

The process of code implementation based on Swoole-Scan code login function

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.