<?php
class tree{
public $key;
public $left;
public $right;
public function __construct($key){
$this->key = $key;
}
/**
* 先序遍历
*/
public function travers_pre_order(){
$l = array();
$r = array();
if($this->left){
$l = $this->left->travers_pre_order();
}
if($this->right){
$r = $this->right->travers_pre_order();
}
return array_merge(array($this->key),$l,$r);
}
/**
* 中序遍历
*/
public function travers_in_order(){
$l = array();
$r = array();
if($this->left){
$l = $this->left->travers_in_order();
}
if($this->right){
$r = $this->right->travers_in_order();
}
return array_merge($l,array($this->key),$r);
}
/**
* 后序遍历
*/
public function travers_post_order(){
$l = array();
$r = array();
if($this->left){
$l = $this->left->travers_post_order();
}
if($this->right){
$r = $this->right->travers_post_order();
}
return array_merge($l,$r,array($this->key));
}
}
$root = new tree('a');
$root->left = new tree('b');
$root->left->left = new tree('c');
$root->left->left->left = new tree('d');
$root->left->right = new tree('e');
$root->right = new tree('f');
var_dump($root->travers_in_order());exit;