### 基本概念
* 深度優先遍歷:
對每一個可能的分支路徑深入到不能再深入為止,而且每個結點只能訪問一次。要特別注意的是,二叉樹的深度優先遍歷比較特殊,可以細分為先序遍歷、中序遍歷、后序遍歷。具體說明如下:
前序遍歷:根節點->左子樹->右子樹
中序遍歷:左子樹->根節點->右子樹
后序遍歷:左子樹->右子樹->根節點
* 廣度優先遍歷:
又叫層次遍歷,從上往下對每一層依次訪問,在每一層中,從左往右(也可以從右往左)訪問結點,訪問完一層就進入下一層,直到沒有結點可以訪問為止。
### 動畫演示
--- **此小結主要講解深度遍歷,例如有序列:A B C D E F G H K,然后各種遍歷后得出結果如下所示:**
* 前序遍歷:A B C D E F G H K
:-: 
* 中序遍歷:B D C A E H G K F
:-: 
* 后序遍歷:D C B H K G F E A
:-: 
### 應用場景
(略)
### 代碼示例
* 前序遍歷
```
/**
* 前序遍歷(遞歸方法)
*/
private function preOrder($root)
{
if (!is_null($root)) {
$function = __FUNCTION__;
echo $root->key . " ";
$this->$function($root->left);
$this->$function($root->right);
}
}
```
* 中序遍歷
```
/**
* 中序遍歷(遞歸方法)
*/
private function midOrder($root)
{
if (!is_null($root)) {
$function = __FUNCTION__;
$this->$function($root->left);
echo $root->key . " ";
$this->$function($root->right);
}
}
```
* 后序遍歷
```
/**
* 后序遍歷(遞歸方法)
*/
private function postOrder($root)
{
if (!is_null($root)) {
$function = __FUNCTION__;
$this->$function($root->left);
$this->$function($root->right);
echo $root->key . " ";
}
}
```
* 客戶端調用
```
class Client
{
public static function Main()
{
$arr = array(10, 8, 12, 7, 9, 11, 13);
$tree = new Bst();
$tree->init($arr);
$traverse = new traverse($tree);
$traverse->preOrder();
$traverse->midOrder();
$traverse->postOrder();
}
}
CLient::Main();
```
### 小結