https://baijiahao.baidu.com/s?id=1609200503642486098&wfr=spider&for=pc
栈
后进先出
<?php
class Stack{
private $stack=array();
//入栈
public function in($item){
return array_push($this->stack,$item);
}
//出栈
public function out(){
return array_pop($this->stack);
}
}
?>
队列
先进先出
<?php
//单向队列
class Deque{
private $queue=array();
//入队 - 队尾
public function addLast($item){
return array_push($this->queue,$item);
}
//出队 - 队首
public function removeFirst(){
return array_shift($this->queue);
}
}
//双向队列
class Deque{
private $queue=array();
public function addFirst($item){
return array_unshift($this->queue,$item);
}
public function addLast($item){
return array_push($this->queue,$item);
}
public function removeFirst(){
return array_shift($this->queue);
}
public function removeLast(){
return array_pop($this->queue);
}
}
?>
二叉树
<?php
class Node {
public $value;
public $child_left;
public $child_right;
}
final class Ergodic {
//前序遍历:先访问根节点,再遍历左子树,最后遍历右子树;并且在遍历左右子树时,仍需先遍历根节点,然后访问左子树,最后遍历右子树
public static function preOrder($root){
$stack = array();
array_push($stack, $root);
while(!empty($stack)){
$center_node = array_pop($stack);
echo $center_node->value . ' ';
//先把右子树节点入栈,以确保左子树节点先出栈
if($center_node->child_right != null) array_push($stack, $center_node->child_right);
if($center_node->child_left != null) array_push($stack, $center_node->child_left);
}
}
//中序遍历:先遍历左子树、然后访问根节点,最后遍历右子树;并且在遍历左右子树的时候。仍然是先遍历左子树,然后访问根节点,最后遍历右子树
public static function midOrder($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->child_left;
}
$center_node = array_pop($stack);
echo $center_node->value . ' ';
if ($center_node->value != 'H') die;
$center_node = $center_node->child_right;
}
}
//后序遍历:先遍历左子树,然后遍历右子树,最后访问根节点;同样,在遍历左右子树的时候同样要先遍历左子树,然后遍历右子树,最后访问根节点
public static function endOrder($root){
$push_stack = array();
$visit_stack = array();
array_push($push_stack, $root);
while (!empty($push_stack)) {
$center_node = array_pop($push_stack);
array_push($visit_stack, $center_node);
//左子树节点先入$pushstack的栈,确保在$visitstack中先出栈
if ($center_node->child_left != null) array_push($push_stack, $center_node->child_left);
if ($center_node->child_right != null) array_push($push_stack, $center_node->child_right);
}
while (!empty($visit_stack)) {
$center_node = array_pop($visit_stack);
echo $center_node->value . ' ';
}
}
//二叉树的深度:输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
public static function TreeDepth($tree)
{
if(!$tree) return 0;
$queue = array();
array_push($queue, $tree);//在数组最后添加元素
$depth= -1;
//逐层遍历,每层深度 +1
while(!empty($queue)){
$depth ++;
$size = count($queue);
for($i=0; $i<$size; $i++){
$node=array_shift($queue);//非常重要 删除第一个元素
if($node->child_left){
array_push($queue,$node->child_left);
}
if($node->child_right){
array_push($queue,$node->child_right);
}
}
}
return $depth;
}
//求二叉树叶子节点个数
public static function getLeaf($root)
{
//左右结点都为空,判断当前节点为叶子节点
if(empty($root->child_left) && empty($root->child_right)){
return 1;
}else{ //递归处理
return self::getLeaf($root->child_left) + self::getLeaf($root->child_left);
}
}
}
//创建二叉树
$a = new Node();
$b = new Node();
$c = new Node();
$d = new Node();
$e = new Node();
$f = new Node();
$g = new Node();
$h = new Node();
$i = new Node();
$a->value = 'A';
$b->value = 'B';
$c->value = 'C';
$d->value = 'D';
$e->value = 'E';
$f->value = 'F';
$g->value = 'G';
$h->value = 'H';
$i->value = 'I';
$a->child_left = $b;
$a->child_right = $c;
$b->child_left = $d;
$b->child_right = $g;
$c->child_left = $e;
$c->child_right = $f;
$d->child_left = $h;
$d->child_right = $i;
//前序遍历
Ergodic::preOrder($a); //结果是:A B D H I G C E F
echo '<br/>';
//中序遍历
Ergodic::midOrder($a); //结果是: H D I B G A E C F
echo '<br/>';
//后序遍历
Ergodic::endOrder($a); //结果是: H I D G B E F C A
echo '<br/>';
//深度
$depth = Ergodic::TreeDepth($a);
echo $depth,'<br/>';
//叶子结点个数
$leaf = Ergodic::getLeaf($a);
echo $leaf,'<br/>';