二叉树遍历

$arr = array(
	0=>array(
	    "lchild" => 1,
	    "data" => 'a',
	    "rchild" => 2
	),
	1=>array(
	    "lchild" => 3,
	    "data" => 'b',
	    "rchild" => 4
	),
	2=>array(
	    "lchild" => 5,
	    "data" => 'e',
	    "rchild" => 6
	),
	3=>array(
		"lchild" => null,
		"data" => 'c',
		"rchild" => null
	    ),
	4=>array(
		"lchild" => null,
		"data" => 'd',
		"rchild" => null
	),
	5=>array(
		"lchild" => null,
		"data" => 'f',
		"rchild" => null
	),
	6=>array(
		"lchild" => null,
		"data" => 'g',
		"rchild" => null
	),
    );

//二叉树的前序遍历
function preOrderByRec($arr, $i=0) {
	$node = $arr[$i];
	if($node) {
		print_r($node['data']);
		preOrderByRec($arr, $node['lchild']);
		preOrderByRec($arr, $node['rchild']);
	} 
	return;
}
//preOrderByRec($arr);

//二叉树的中序遍历
function inOrderByRec($arr, $i=0) {
	$node = $arr[$i];
	if($node) {
		inOrderByRec($arr, $node['lchild']);
		print_r($node['data']);
		inOrderByRec($arr, $node['rchild']);
	}
	return;
}
//inOrderByRec($arr, 0);

//二叉树的后序遍历
function sufOrderByRec($arr, $i=0) {
	$node = $arr[$i];
	if($node) {
		sufOrderByRec($arr, $node['lchild']);
		sufOrderByRec($arr, $node['rchild']);
		print_r($node['data']);
	}
	return;
}
//sufOrderByRec($arr);

//非递归前序
function preOrderByNonRec($arr) {
	$tmp = array();
	array_push($tmp, $arr[0]);
	while($tmp){
		$node = array_pop($tmp);
		print_r($node['data']);
		$l = $node['lchild'];
		$r = $node['rchild'];
		if($r) array_push ($tmp, $arr[$r]);
		if($l) array_push ($tmp, $arr[$l]);
	}
	return ;
}
//preOrderByNonRec($arr);

//非递归中序
function inOrderByNonRec($arr) {
	$tmp = array();
	$node = $arr[0];
	while(!empty($tmp) || $node) {
		while($node) {
			array_push($tmp, $node);
			$l = $node['lchild'];
			$node = $arr[$l];
		}
		$node = array_pop($tmp);
		print_r($node['data']);
		$r = $node['rchild'];
		$node = $r ? $arr[$r] : null;
	}
	return;
}
//inOrderByNonRec($arr);

//非递归后序
function sufOrderByNonRec($arr) {
	$pushstack = array();
	$visitstack = array();
	array_push($pushstack, $arr[0]);
	while($pushstack) {
		$node = array_pop($pushstack);
		array_push($visitstack, $node);
		$l = $node['lchild'];
		$r = $node['rchild'];
		if($l) array_push ($pushstack, $arr[$l]);
		if($r) array_push($pushstack, $arr[$r]);
	}
	
	while($visitstack) {
		$node = array_pop($visitstack);
		print_r($node['data']);
	}
	return;
}
sufOrderByNonRec($arr);
/**
 * 二叉树遍历
 */
class Node {
    public $value;
    public $left;
    public $right;
}
//前序遍历,访问根节点->遍历子左树->遍历右左树
function preorder($root){
    $stack = array();
    array_push($stack, $root);
    while(!empty($stack)){
	$center_node = array_pop($stack);
	echo $center_node->value.' ';

	if($center_node->right != null) array_push($stack, $center_node->right);
	if($center_node->left != null) array_push($stack, $center_node->left);
    }
}
//中序遍历,遍历子左树->访问根节点->遍历右右树
function inorder($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->left;
	     }

	     $center_node = array_pop($stack);
	     echo $center_node->value . " ";

	     $center_node = $center_node->right;
	 }
}
//后序遍历,遍历子左树->访问子右树->遍历根节点
function postorder($root){
	$pushstack = array();
	$visitstack = array();
	array_push($pushstack, $root);

	while (!empty($pushstack)) {
	    $center_node = array_pop($pushstack);
	    array_push($visitstack, $center_node);
	    if ($center_node->left != null) array_push($pushstack, $center_node->left);
	    if ($center_node->right != null) array_push($pushstack, $center_node->right);
	}

	while (!empty($visitstack)) {
	    $center_node = array_pop($visitstack);
	    echo $center_node->value. " ";
	}
}

//创建如上图所示的二叉树
$a = new Node();
$b = new Node();
$c = new Node();
$d = new Node();
$e = new Node();
$f = new Node();
$a->value = 'A';
$b->value = 'B';
$c->value = 'C';
$d->value = 'D';
$e->value = 'E';
$f->value = 'F';
$a->left = $b;
$a->right = $c;
$b->left = $d;
$c->left = $e;
$c->right = $f;

//前序遍历
preorder($a);  //结果是:A B D C E F
inorder($a);  //结果是: D B A E C F
postorder($a); //结果是:  D B E F C A

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值