您的位置:首页 > 理论基础 > 数据结构算法

php语言_数据结构_树_学习笔记

2015-07-01 23:01 609 查看
使用PHP语言学习数据结构,对于我自己来说,觉得更加容易理解

直接上代码

树节点数据结构

class TreeNode{
public $data;
public $lchild = null;
public $rchild = null;
public function __construct($data='',$lchild=null,$rchild=null){
$this->data = $data;
$this->lchild = $lchild;
$this->rchild = $rchild;
}
}
树一些基本操作封装
class Tree{
public $root;

public function __construct($root){
$this->root = $root;
}

//树的深度
public function getDepth(){
return $this->getMaxDepth($this->root);
}

private function getMaxDepth($node){
if(null == $node) return 0;
else{
$left = $this->getMaxDepth($node->lchild);
$right = $this->getMaxDepth($node->rchild);
return max($left,$right)+1;
}
}

//得到树的宽度 逐层输出可以借鉴
public function getMaxWidth(){
if(null == $this->root){
return 0;
}
$queue = array();
$maxWidth =1;
array_push($queue,$this->root);
while(true){
$len = count($queue);
if($len == 0){
break;
}
while($len>0){
$temp =array_shift($queue);
$len--;
if($temp->lchild) array_push($queue,$temp->lchild);
if($temp->rchild) array_push($queue,$temp->rchild);
}
$maxWidth = max($maxWidth,count($queue));
}
return $maxWidth;
}

//逐层遍历树
public function printTree(){
if($this->root == null) return false;
$queue = array();
$queue[] = $this->root;
while(count($queue)){  //加一个中间变量len存一下队列的大小
$temp = array_shift($queue);
echo $temp->data.'--';  //然后这个地方长度减一,也就是上一层的一个节点被遍历了,直到见成0,说明上层入队列的节点完全被遍历了
if($temp->lchild) array_push($queue,$temp->lchild);
if($temp->rchild) array_push($queue,$temp->rchild);
}
}

//从下往上
public function printTree2(){
if($this->root == null) return false;
$queue = array();
$res = array();
$queue[] = $this->root;
$res[] = $this->root->data;
while(count($queue)){
$temp = array_shift($queue);
array_unshift($res,$temp->data);
if($temp->rchild) array_push($queue,$temp->rchild);
if($temp->lchild) array_push($queue,$temp->lchild);
}
return $res;
}

//判断一个二叉树是否是平衡二叉树
public function isAVL(){
return $this->isBalanceTree($this->root,$depth);
}

private function isBalanceTree($param,&$depth){
if(null == $param){
$depth = 0;
return true;
}
$bleft = $this->isBalanceTree($param->lchild,$nleftDepth);
$bright = $this->isBalanceTree($param->rchild,$nrightDepth);
if($bleft && $bright){
$diff = abs($nleftDepth - $nrightDepth);
if($diff<=1){
$depth = $nleftDepth>$nrightDepth?($nleftDepth+1):($nrightDepth+1);
return true;
}
}
return false;

}

//中根遍历
public function LDR(){
$stack = array();
$stack[] = $this->root;
while(count($stack)){
while($temp = $this->getTop($stack)){
array_push($stack,$temp->lchild);
}
array_pop($stack);
if(count($stack)){
$p = array_pop($stack);
echo $p->data.'----';
array_push($stack,$p->rchild);

}
}
}

//先根遍历
public function DLR(){
$stack = array();
$stack[] = $this->root;
while(count($stack)){
if($temp = $this->getTop($stack) ){
echo $temp->data.'--';
array_push($stack,$temp->lchild);
}else{
array_pop($stack);
if(count($stack)){
$p = array_pop($stack);
array_push($stack,$p->rchild);
}
}
}
}
//得到栈的栈顶元素,不pop
private function getTop($s){
return $s[count($s)-1];
}
}
测试代码
require 'tree.class.php';
require 'treenode.class.php';
$treeNode_0 = new TreeNode('0');
$treeNode_1 = new TreeNode('1');
$treeNode_2 = new TreeNode('2');
$treeNode_3 = new TreeNode('3');
$treeNode_4 = new TreeNode('4');
$treeNode_5 = new TreeNode('5');
$treeNode_6 = new TreeNode('6');
$treeNode_7 = new TreeNode('7');
$treeNode_8 = new TreeNode('8');
$treeNode_9 = new TreeNode('9');
$treeNode_10 = new TreeNode('10');

$treeNode_3->lchild = $treeNode_7;
$treeNode_7->rchild = $treeNode_8;

$treeNode_5->rchild = $treeNode_9;
$treeNode_6->lchild = $treeNode_10;

$treeNode_1->lchild = $treeNode_3;
$treeNode_1->rchild = $treeNode_4;

$treeNode_2->lchild = $treeNode_5;
$treeNode_2->rchild = $treeNode_6;
$treeNode_0->lchild = $treeNode_1;
$treeNode_0->rchild = $treeNode_2;

$tree = new Tree($treeNode_0);

//$tree->DLR();
//$tree->LDR();
//$tree->printTree();
//$res = $tree->isBalanceTree($treeNode_0,$depth);
$res = $tree->getMaxDepth($treeNode_0);
var_dump($res);
/*
$res = $tree->printTree2();
echo '<pre>';
var_dump($res);
echo '</pre>';
*/

Tree类缺少一个直接使用一对树节点,搭建一个树结构的方法createTree($param) 后期调查然后补上
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息