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

使用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) 后期调查然后补上

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值