php二叉树层序遍历 带层数和不带层数 需要用到队列

tree1.php

ch=$data;
		$this->lchild=null;
		$this->rchild=null;
	}
	
}

//封装自己数据类型
class mydata{
	public $BinNode;
	public function __construct($data){
		$this->BinNode=$data;
	}
}


class twofucktree{

	//初始化
	public function init_tree()
	{
	/*

	    A
	B		F
  C			   G
D   E	     H

	*/
		$nodeA= new BinNode('A');
		$nodeB= new BinNode('B');
		$nodeC= new BinNode('C');
		$nodeD= new BinNode('D');
		$nodeE= new BinNode('E');
		$nodeF= new BinNode('F');
		$nodeG= new BinNode('G');
		$nodeH= new BinNode('H');

		$nodeA->lchild = $nodeB;
		$nodeA->rchild = $nodeF;
		$nodeB->rchild = $nodeC;
		$nodeC->lchild = $nodeD;
		$nodeC->rchild = $nodeE;
		$nodeF->rchild = $nodeG;
		$nodeG->lchild = $nodeH;

		return $nodeA;
	}

	//求树的高度
	public function gao($root,&$height){
		if($root==null)
		{
			return;
		}

		$lheight = $this->gao($root->lchild,$height);
		$rheight = $this->gao($root->rchild,$height);

		$height = $lheight>$rheight?$lheight+1:$rheight+1;

		return $height;
	}
	//先序
	public function xianxu($root)
	{
		if($root==null)
		{
			return;
		}
		echo $root->ch." ";
		$this->xianxu($root->lchild);
		$this->xianxu($root->rchild);

	}
	//中序
	public function zhongxu($root)
	{
		if($root==null)
		{
			return;
		}
		$this->zhongxu($root->lchild);
		echo $root->ch." ";
		$this->zhongxu($root->rchild);

	}

	//后序
	public function houxu($root)
	{
		if($root==null)
		{
			return;
		}
		$this->houxu($root->lchild);
		$this->houxu($root->rchild);
		echo $root->ch." ";
	}

	//叶子节点数
	public function yezi($root,&$geshu)
	{
		if($root==null)
		{
			return;
		}

		if($root->lchild==null && $root->rchild==null)
		{
			$geshu++;
		}
		$this->yezi($root->lchild,$geshu);
		$this->yezi($root->rchild,$geshu);
	}

	//非递归遍历
	public function feidigui($root)
	{
		if(null==$root)
		{
			return;
		}

		// $mydata = new mydata($root,false);
		// echo $mydata->BinNode->ch;
		$stack = new my_stack();
		//接受返回的链表
		$lk=$stack->init_stack();
		//压入栈
		$stack->push_link($lk,new mydata($root,false));

		while($stack->get_size($lk)>0)
		{

			$nownode = $stack->get_top($lk);
			$stack->pop_stack($lk);
			if($nownode->status==true)
			{
				echo $nownode->BinNode->ch." ";
				$nownode=null;
				continue;
			}else{
				//先序遍历 根左右  押入顺序 右左根
				if($nownode->BinNode->rchild!=null)
				{
					$stack->push_link($lk,new mydata($nownode->BinNode->rchild,false));
				}

				if($nownode->BinNode->lchild!=null)
				{
					$stack->push_link($lk,new mydata($nownode->BinNode->lchild,false));
				}


				$nownode->status=true;
				$stack->push_link($lk,$nownode);


			}
		}



	}


	//层序遍历
	public function cengxu($root)
	{
		if($root==null)
		{
			return;
		}


		$duilie = new lianbiao();
		$lk = $duilie->init_linklist();

		$duilie->push_link($lk,$root);

		while($duilie->get_size($lk)>0)
		{
			$node=$duilie->get_top($lk);
			$duilie->pop_link($lk);
			echo $node->ch." ";

			//押入左右子树
			if($node->lchild!=null)
			{
				$duilie->push_link($lk,$node->lchild);
			}
			if($node->rchild!=null)
			{
				$duilie->push_link($lk,$node->rchild);
			}
			
		}


	}

	//层序遍历1 带层级的
	public function cengxu1($root)
	{
		$num = 1;
		if($root==null)
		{
			return;
		}


		$last=$root;
		$nlast=null;



		$duilie = new lianbiao();
		$lk = $duilie->init_linklist();

		$duilie->push_link($lk,$root);

		while($duilie->get_size($lk)>0)
		{
			$node=$duilie->get_top($lk);
			$duilie->pop_link($lk);
			echo "第{$num}层节点为 ".$node->ch." ";

			//押入左右子树
			if($node->lchild!=null)
			{
				$duilie->push_link($lk,$node->lchild);
				$nlast=$node->lchild;
			}
			if($node->rchild!=null)
			{
				$duilie->push_link($lk,$node->rchild);
				$nlast=$node->rchild;
			}

			if($last==$node)
			{
				$num++;
				$last = $nlast;
				echo "
"; } } } } $tree = new twofucktree(); $root = $tree->init_tree(); $tree->xianxu($root); echo "
"; $tree->zhongxu($root); echo "
"; $tree->houxu($root); echo "
"; $gao=0; $tree->gao($root,$gao); echo "树的高度height={$gao}
"; $geshu=0; $tree->yezi($root,$geshu); echo "叶子节点的个数{$geshu}个
"; echo "层序遍历
"; $tree->cengxu($root); echo "
"; echo "带层级层序遍历1
"; $tree->cengxu1($root); /* A B C D E F G H B D C E A F H G D E C B H G F A 树的高度height=4 叶子节点的个数3个 层序遍历 A B F C G D E H 带层级层序遍历1 第1层节点为 A 第2层节点为 B 第2层节点为 F 第3层节点为 C 第3层节点为 G 第4层节点为 D 第4层节点为 E 第4层节点为 H */ ?>

duilie.php

data=$data;
        $this->next=null;
    }

}


class linklist
{
	public $linknode;
	public $rear;
	public $size;
	
}


class lianbiao
{
	//初始化
	public function init_linklist()
	{
		$linklist =   new linklist();
		$header   =   new linknode(-1);
		//存放头结点
		$linklist->linknode = $header;
		$linklist->rear = $linklist->linknode;
		$linklist->size=0;
		return $linklist;

	}
	//入队
	public function push_link($lk,$data)
	{
		$new_node = new linknode($data);
		$lk->rear->next = $new_node;
		$lk->rear = $new_node;
		$lk->size++;
	}
	//获得队头元素
	public function get_top($lk)
	{
		 

		 $current = $lk->linknode->next;
		 return $current->data;
	}

	//获得队尾元素
	public function get_rear($lk)
	{
		 $current = $lk->rear;
		 return $current->data;
	}
	//获取大小
	public function get_size($lk)
	{
		return $lk->size;
	}
	//出队
	public function pop_link($lk)
	{
		if($lk->size<=0)
		{
				return;
		}
		if($lk->size==1)
		{
			$del = $lk->linknode->next;
			$lk->linknode->next=null;
			$lk->rear=$lk->linknode;
		}
		else{

			$del = $lk->linknode->next;
			//echo $del->data;exit;
			$lk->linknode->next = $del->next;
			$del = null;
		}

		$lk->size--;

	}

	//销毁
	public function destroy_link($lk)
	{
		$lk=null;
	}
}

/*
$lianbiao = new lianbiao();
$lk = $lianbiao->init_linklist();

$node1 = new linknode(10);
$node2 = new linknode(20);
$node3 = new linknode(30);
$lianbiao->push_link($lk,$node1);
$lianbiao->push_link($lk,$node2);
$lianbiao->push_link($lk,$node3);



//遍历
while($lianbiao->get_size($lk)>0)
{
	$data=$lianbiao->get_top($lk);
	echo $data->data."
"; $lianbiao->pop_link($lk); } $lianbiao->destroy_link($lk); */ ?>

 php二叉树层序遍历 带层数和不带层数 需要用到队列_第1张图片

 

你可能感兴趣的:(php)