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