Binary tree traversal algorithm example implemented by php and php Binary Tree Algorithm example
This example describes the binary tree traversal algorithm implemented by php. We will share this with you for your reference. The details are as follows:
Today, we use php to implement binary tree traversal.
Shows the created binary tree.
The php code is as follows:
<? Phpclass Node {public $ value; public $ child_left; public $ child_right;} final class Ergodic {// sequential traversal: first access the root Node and then traverse the left subtree, finally, traverse the right subtree. When traversing the left and right subtree, you still need to traverse the root node first, then access the left 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 add the right subtree node to the stack to ensure that the Left subtree node first outputs 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) ;}// in-order traversal: First traverse the left subtree, then access the root node, and finally traverse the right subtree; and when traversing the left and right subtree. Still, 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_node); $ center_node = $ center_node-> child_left;} $ center_node = array_pop ($ stack); echo $ center_node-> value. ''; $ center_node = $ center_node-> child_right;} // post-sequential traversal: First traverse the left subtree, then traverse the right subtree, and finally access the root node; similarly, when Traversing left and right subtree, you also need to traverse the left subtree first, then traverse the right subtree, and finally access the root node public static function endOrder ($ root) {$ push_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); // The left subtree node first enters the $ pushstack stack, make sure that the first stack in $ visitstack if ($ center_node-> child_left! = Null) array_push ($ push_stack, $ center_node-> 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. '';}}// create a binary 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-> child_left = $ h; $ d-> child_right = $ I; // pre-order traversal Ergodic: preOrder ($ a); // The result is: a B d h I G C E Fecho '<br/>'; // In the middle order, traverse Ergodic: midOrder ($ a); // The result is: h d I B G A E C Fecho '<br/>'; // traverse Ergodic in descending order: endOrder ($ a); // The result is: H I D G B E F C