31 32 while(!empty($stack)) { 33 $cnode = array_pop($stack); 34 $traverse_data[]=$cnode->data; 35 if ($cnode->right != null) array_push($stack, $cnode->right); 36 if ($cnode->left != null) array_push($stack, $cnode->left); 37 } 38 return $traverse_data; 39 } 40 41 $root = new Node(); 42 $node1 = new Node(); 43 $node2 = new Node(); 44 $node3 = new Node(); 45 $node4 = new Node(); 46 $node5 = new Node(); 47 $node6 = new Node(); 48 $node7 = new Node(); 49 $node8 = new Node(); 50 51 $root->data = 1; 52 $node1->data = 2; 53 $node2->data = 3; 54 $node3->data = 4; 55 $node4->data = 5; 56 $node5->data = 6; 57 $node6->data = 7; 58 $node7->data = 8; 59 $node8->data = 9; 60
61 $root->left = $node1; 62 $root->right = $node2; 63 $node1->left = $node3; 64 $node1->right = $node4; 65 $node2->left = $node5; 66 $node2->right = $node6; 67 $node5->left = $node7; 68 $node5->right = $node8; 69 70 $res = b_first($root); 71 var_dump($res); 72 $res1 = depth_first($root); 73 var_dump($res1); 74 echo "<br>";