php语言_数据结构_树_学习笔记
2015-07-01 23:01
609 查看
使用PHP语言学习数据结构,对于我自己来说,觉得更加容易理解
直接上代码
树节点数据结构
Tree类缺少一个直接使用一对树节点,搭建一个树结构的方法createTree($param) 后期调查然后补上
直接上代码
树节点数据结构
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) 后期调查然后补上
相关文章推荐
- 文件遍历排序函数
- Lua 学习笔记之C API 遍历 Table实现代码
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构揭秘一
- C#中遍历Hashtable的4种方法
- C#实现获取系统目录并以Tree树叉显示的方法
- Erlang中遍历取出某个位置的最大值代码
- 数据结构之Treap详解
- C++实现图的邻接矩阵存储和广度、深度优先遍历实例分析
- C语言实现输入一颗二元查找树并将该树转换为它的镜像
- C++实现图的邻接表存储和广度优先遍历实例分析
- php遍历目录方法小结
- 一个目录遍历函数
- php遍历删除整个目录及文件的方法
- PHP遍历文件夹与文件类及处理类用法实例
- PHP遍历XML文档所有节点的方法
- php中使用key,value,current,next和prev函数遍历数组的方法
- C#使用前序遍历、中序遍历和后序遍历打印二叉树的方法
- C#使用foreach遍历哈希表(hashtable)的方法