PHP Class&Object -- 解析PHP實現(xiàn)二叉樹
更新時間:2013年06月25日 09:05:59 作者:
本篇文章是對PHP中二叉樹的實現(xiàn)代碼進行詳細的分析介紹,需要的朋友參考下
二叉樹及其變體是數(shù)據結構家族里的重要組成部分。最為鏈表的一種變體,二叉樹最適合處理需要一特定次序快速組織和檢索的數(shù)據。
<?php
// Define a class to implement a binary tree
class Binary_Tree_Node {
// Define the variable to hold our data:
public $data;
// And a variable to hold the left and right objects:
public $left;
public $right;
// A constructor method that allows for data to be passed in
public function __construct($d = NULL) {
$this->data = $d;
}
// Traverse the tree, left to right, in pre-order, returning an array
// Preorder means that each node's value preceeds its children.
public function traversePreorder() {
// Prep some variables.
$l = array();
$r = array();
// Read in the left and right children appropriately traversed:
if ($this->left) { $l = $this->left->traversePreorder(); }
if ($this->right) { $r = $this->right->traversePreorder(); }
// Return a merged array of the current value, left, and right:
return array_merge(array($this->data), $l, $r);
}
// Traverse the tree, left to right, in postorder, returning an array
// Postorder means that each node's value follows its children.
public function traversePostorder() {
// Prep some variables.
$l = array();
$r = array();
// Read in the left and right children appropriately traversed:
if ($this->left) { $l = $this->left->traversePostorder(); }
if ($this->right) { $r = $this->right->traversePostorder(); }
// Return a merged array of the current value, left, and right:
return array_merge($l, $r, array($this->data));
}
// Traverse the tree, left to right, in-order, returning an array.
// In-order means that values are ordered as left children, then the
// node value, then the right children.
public function traverseInorder() {
// Prep some variables.
$l = array();
$r = array();
// Read in the left and right children appropriately traversed:
if ($this->left) { $l = $this->left->traverseInorder(); }
if ($this->right) { $r = $this->right->traverseInorder(); }
// Return a merged array of the current value, left, and right:
return array_merge($l, array($this->data), $r);
}
}
// Let's create a binary tree that will equal the following: 3
// / /
// h 9
// / /
// Create the tree: 6 a
$tree = new Binary_Tree_Node(3);
$tree->left = new Binary_Tree_Node('h');
$tree->right = new Binary_Tree_Node(9);
$tree->right->left = new Binary_Tree_Node(6);
$tree->right->right = new Binary_Tree_Node('a');
// Now traverse this tree in all possible orders and display the results:
// Pre-order: 3, h, 9, 6, a
echo '<p>', implode(', ', $tree->traversePreorder()), '</p>';
// Post-order: h, 9, 6, a, 3
echo '<p>', implode(', ', $tree->traversePostorder()), '</p>';
// In-order: h, 3, 6, 9, a
echo '<p>', implode(', ', $tree->traverseInorder()), '</p>';
?>
復制代碼 代碼如下:
<?php
// Define a class to implement a binary tree
class Binary_Tree_Node {
// Define the variable to hold our data:
public $data;
// And a variable to hold the left and right objects:
public $left;
public $right;
// A constructor method that allows for data to be passed in
public function __construct($d = NULL) {
$this->data = $d;
}
// Traverse the tree, left to right, in pre-order, returning an array
// Preorder means that each node's value preceeds its children.
public function traversePreorder() {
// Prep some variables.
$l = array();
$r = array();
// Read in the left and right children appropriately traversed:
if ($this->left) { $l = $this->left->traversePreorder(); }
if ($this->right) { $r = $this->right->traversePreorder(); }
// Return a merged array of the current value, left, and right:
return array_merge(array($this->data), $l, $r);
}
// Traverse the tree, left to right, in postorder, returning an array
// Postorder means that each node's value follows its children.
public function traversePostorder() {
// Prep some variables.
$l = array();
$r = array();
// Read in the left and right children appropriately traversed:
if ($this->left) { $l = $this->left->traversePostorder(); }
if ($this->right) { $r = $this->right->traversePostorder(); }
// Return a merged array of the current value, left, and right:
return array_merge($l, $r, array($this->data));
}
// Traverse the tree, left to right, in-order, returning an array.
// In-order means that values are ordered as left children, then the
// node value, then the right children.
public function traverseInorder() {
// Prep some variables.
$l = array();
$r = array();
// Read in the left and right children appropriately traversed:
if ($this->left) { $l = $this->left->traverseInorder(); }
if ($this->right) { $r = $this->right->traverseInorder(); }
// Return a merged array of the current value, left, and right:
return array_merge($l, array($this->data), $r);
}
}
// Let's create a binary tree that will equal the following: 3
// / /
// h 9
// / /
// Create the tree: 6 a
$tree = new Binary_Tree_Node(3);
$tree->left = new Binary_Tree_Node('h');
$tree->right = new Binary_Tree_Node(9);
$tree->right->left = new Binary_Tree_Node(6);
$tree->right->right = new Binary_Tree_Node('a');
// Now traverse this tree in all possible orders and display the results:
// Pre-order: 3, h, 9, 6, a
echo '<p>', implode(', ', $tree->traversePreorder()), '</p>';
// Post-order: h, 9, 6, a, 3
echo '<p>', implode(', ', $tree->traversePostorder()), '</p>';
// In-order: h, 3, 6, 9, a
echo '<p>', implode(', ', $tree->traverseInorder()), '</p>';
?>
您可能感興趣的文章:
- PHP實現(xiàn)二叉樹的深度優(yōu)先與廣度優(yōu)先遍歷方法
- php FLEA中二叉樹數(shù)組的遍歷輸出
- PHP實現(xiàn)的線索二叉樹及二叉樹遍歷方法詳解
- php實現(xiàn)的二叉樹遍歷算法示例
- PHP構造二叉樹算法示例
- PHP Class&Object -- PHP 自排序二叉樹的深入解析
- PHP實現(xiàn)繪制二叉樹圖形顯示功能詳解【包括二叉搜索樹、平衡樹及紅黑樹】
- PHP完全二叉樹定義與實現(xiàn)方法示例
- PHP實現(xiàn)判斷二叉樹是否對稱的方法
- PHP基于非遞歸算法實現(xiàn)先序、中序及后序遍歷二叉樹操作示例
- PHP實現(xiàn)按之字形順序打印二叉樹的方法
相關文章
PHP模擬登陸163郵箱發(fā)郵件及獲取通訊錄列表的方法
這篇文章主要介紹了PHP模擬登陸163郵箱發(fā)郵件及獲取通訊錄列表的方法,實例分析了php實用curl模擬登陸163郵箱的操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03php獲取網卡的MAC地址支持WIN/LINUX系統(tǒng)
這篇文章主要介紹了使用php獲取網卡的MAC地址支持WIN/LINUX系統(tǒng),需要的朋友可以參考下2014-04-04淺析php靜態(tài)方法與非靜態(tài)方法的用法區(qū)別
下面小編就為大家?guī)硪黄獪\析php靜態(tài)方法與非靜態(tài)方法的用法區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05